diff --git a/src/hooks/salePrice.tsx b/src/hooks/salePrice.tsx index 60905b2d..74a0bb21 100644 --- a/src/hooks/salePrice.tsx +++ b/src/hooks/salePrice.tsx @@ -8,29 +8,27 @@ import { useSaleInfo } from '@/contexts/sales'; import { useRouter } from 'next/router'; interface SalePriceProps { - at: number; + at?: number; } const useSalePrice = ({ at }: SalePriceProps) => { - const { - state: { api, apiState }, - } = useCoretimeApi(); const { saleInfo } = useSaleInfo(); const [currentPrice, setCurrentPrice] = useState(0); const router = useRouter(); const { network } = router.query; - const fetchCurrentPrice = useCallback(async () => { - if (api && apiState === ApiState.READY) { + const fetchCurrentPrice = async (at: number) => { + if (at) { const price = getCurrentPrice(saleInfo, at, network); setCurrentPrice(price); } - }, [api, apiState, saleInfo]); + }; useEffect(() => { - fetchCurrentPrice(); - }, [fetchCurrentPrice]); + if (!at) return; + fetchCurrentPrice(at); + }, [at]); return currentPrice; }; diff --git a/src/pages/purchase.tsx b/src/pages/purchase.tsx index 1c50091b..510c2a37 100644 --- a/src/pages/purchase.tsx +++ b/src/pages/purchase.tsx @@ -48,7 +48,7 @@ const Purchase = () => { const { fetchRegions } = useRegions(); const { balance } = useBalances(); - const currentPrice = useSalePrice({ at }); + let currentPrice = useSalePrice({ at }); const { saleStart, currentPhase, @@ -59,16 +59,16 @@ const Purchase = () => { } = useSalePhase(); useEffect(() => { - if (!api || apiState !== ApiState.READY) return; + if (!currentPhase) return; - api.query.system.number().then((height) => { - if (currentPhase === SalePhase.Interlude) { - console.log(saleStart); - setAt(saleStart); - } else { + if ((currentPhase as SalePhase) === SalePhase.Interlude) { + setAt(saleStart); + } else { + if (!api || apiState !== ApiState.READY) return; + api.query.system.number().then((height) => { setAt(parseHNString(height.toHuman() as string)); - } - }); + }); + } }, [saleStart, currentPhase]); const purchase = async () => {