You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
5.7 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. import { useMemo, useRef, useState } from "react"
  2. import { Button, Radio, Toast } from "react-vant"
  3. import { MarketNFTData } from "~/types/store"
  4. import '~/styles/detail.scss'
  5. import Modal from "~/components/Modal"
  6. import { toThousands } from "~/utils"
  7. import { shelves_nft } from "~/api"
  8. import store from "~/store"
  9. import { useRouter } from "~/hooks/useRouter"
  10. interface OnShelvesNFTProps {
  11. data: MarketNFTData
  12. }
  13. const OnShelvesNFT = (props: OnShelvesNFTProps) => {
  14. const { data } = props
  15. const { push } = useRouter()
  16. const [defaultValue, setDefaultValue] = useState("") //1.拍賣 2.售賣
  17. const symbols = useMemo(() => ['FIL', 'USDT'], [])
  18. const markups = useMemo(() => [1, 5, 10], []) // 拍賣加價
  19. const auctionTimes = useMemo(() => [24, 48, 72], []) // 拍賣時間
  20. const [currentMarkeup, setCurrentMarkeup] = useState(5) //當前拍賣加價
  21. const [currentSymbol, setCurrentSymbol] = useState('') //當前代筆
  22. const [currentAuctionTime, setCurrentAuctionTime] = useState(24) //當前拍賣時間
  23. const [visible, setVisible] = useState(false)//拍賣
  24. const [sellVisible, setSellVisible] = useState(false)//售賣
  25. const amountRef = useRef<HTMLInputElement>(null)
  26. const openModal = () => {
  27. if (!store.state.token) return store.setVisibleUnLogin(true)
  28. if (!defaultValue) return Toast.fail('請選擇拍賣或售賣')
  29. if (!currentSymbol) return Toast.fail('請選擇代筆')
  30. if (!amountRef.current!.value) return Toast.fail('請輸入上架金額');
  31. if (defaultValue === "1") return setVisible(true)
  32. setSellVisible(true)
  33. }
  34. // 確認拍賣
  35. const confirmAuction = async () => {
  36. setVisible(false)
  37. const params = {
  38. id: data.id,
  39. amount: amountRef.current?.value,
  40. symbol: currentSymbol,
  41. mark: currentMarkeup,
  42. time: currentAuctionTime,
  43. type: Number(defaultValue)
  44. }
  45. const res: any = await shelves_nft(params)
  46. if (res && res.code === 0) {
  47. Toast.success('上架成功')
  48. store.getMarketNft("auctionNft")
  49. store.getMyNft("myNft");
  50. store.getUserInfo()
  51. push(-1)
  52. }
  53. }
  54. // 確認售賣
  55. const confirmSell = async () => {
  56. setSellVisible(false)
  57. const params = {
  58. id: data.id,
  59. amount: amountRef.current?.value,
  60. symbol: currentSymbol,
  61. type: Number(defaultValue)
  62. }
  63. const res: any = await shelves_nft(params)
  64. if (res && res.code === 0) {
  65. Toast.success('上架成功')
  66. store.getMarketNft("sellNft")
  67. store.getMyNft("myNft");
  68. store.getUserInfo()
  69. push(-1)
  70. }
  71. }
  72. return (
  73. <div className="mt-2 on-shelves-nft">
  74. <Radio.Group defaultValue={defaultValue} onChange={setDefaultValue} direction="horizontal" checkedColor="#0FC6D4">
  75. <Radio name="1"></Radio>
  76. <Radio name="2" className="ml-2"></Radio>
  77. </Radio.Group>
  78. <div className="mt-2 row-items">
  79. {
  80. symbols.map(item => (
  81. <div key={item} className={`row-center tag ${item}-bg mr-2 ${currentSymbol === item && 'tag-active'}`} onClick={() => {
  82. setCurrentSymbol(item)
  83. }}>
  84. <img src={require(`~/assets/${item}.png`)} alt="" />
  85. <div>{item}</div>
  86. </div>
  87. ))
  88. }
  89. </div>
  90. <div className="mt-2">
  91. <input placeholder="請輸入上架金額" className="input" ref={amountRef} />
  92. </div>
  93. <div className="mtb-2">
  94. <Button className="on-button" onClick={openModal}></Button>
  95. </div>
  96. {/* 拍賣 */}
  97. <Modal
  98. width="90%"
  99. title={data.name}
  100. visible={visible}
  101. setVisible={setVisible}
  102. showConfirmButton
  103. >
  104. <div className="mt-2">
  105. <div className="tac fz-wb-550"> "<span className={`${currentSymbol}-text`}>{toThousands(amountRef.current?.value || '0')}</span>" {currentSymbol}</div>
  106. <div className="tac mt-5px fz-wb-550"> <span className={`${currentSymbol}-text`}>{currentMarkeup}%</span></div>
  107. <div className="row-center mt-1">
  108. {
  109. markups.map((item, index) => (
  110. <div key={item} className={`row-center ${index === 1 && 'mlr-3'} fz-14 auction-tag ${currentSymbol}-bg`} onClick={() => setCurrentMarkeup(item)}>+{item}%</div>
  111. ))
  112. }
  113. </div>
  114. <div className="tac mt-3 fz-wb-550"> "<span className={`${currentSymbol}-text`}>{currentAuctionTime}</span>" </div>
  115. <div className="row-center mt-2">
  116. {
  117. auctionTimes.map((item, index) => (
  118. <div key={item} className={`row-center ${index === 1 && 'mlr-3'} fz-14 auction-tag ${currentSymbol}-bg`} onClick={() => setCurrentAuctionTime(item)}>{item}</div>
  119. ))
  120. }
  121. </div>
  122. <div className="row-center mt-3">
  123. <Button onClick={confirmAuction} className={`markeup-button ${currentSymbol}-bg`}></Button>
  124. </div>
  125. </div>
  126. </Modal>
  127. {/* 售賣 */}
  128. <Modal
  129. width="90%"
  130. title={data.name}
  131. visible={sellVisible}
  132. setVisible={setSellVisible}
  133. showConfirmButton
  134. >
  135. <div className="mt-2">
  136. <div className="tac fz-wb-550"> "<span className={`${currentSymbol}-text`}>{toThousands(amountRef.current?.value || '0')}</span>" {currentSymbol}</div>
  137. <div className="row-center mt-3">
  138. <Button onClick={confirmSell} className={`markeup-button ${currentSymbol}-bg`}></Button>
  139. </div>
  140. </div>
  141. </Modal>
  142. </div>
  143. )
  144. }
  145. export default OnShelvesNFT