feat(currency-dropdown): add loaders while fetching

This commit is contained in:
RadoslavDimchev 2024-04-19 20:06:29 +03:00 committed by Emil Ivanichkov
parent c55609b331
commit a6d537b677

View File

@ -6,6 +6,7 @@ import { XStack, YStack } from 'tamagui'
import { RootState } from '../../redux/store' import { RootState } from '../../redux/store'
import { formatNumbersWithComa } from '../../utilities' import { formatNumbersWithComa } from '../../utilities'
import ChevronIcon from './ChevronIcon' import ChevronIcon from './ChevronIcon'
import { LOADING } from '../../constants'
type CurrencyDropdownProps = { type CurrencyDropdownProps = {
depositAmount: number depositAmount: number
@ -15,6 +16,9 @@ const CurrencyDropdown = ({ depositAmount }: CurrencyDropdownProps) => {
const [isOpen, setIsOpen] = useState(false) const [isOpen, setIsOpen] = useState(false)
const [supportedCurrencies, setSupportedCurrencies] = useState([]) const [supportedCurrencies, setSupportedCurrencies] = useState([])
const [currentCurrencyAmount, setCurrentCurrencyAmount] = useState(0) const [currentCurrencyAmount, setCurrentCurrencyAmount] = useState(0)
const [isCurrencyLoading, setIsCurrencyLoading] = useState(false)
const [isSupportedCurrenciesLoading, setIsSupportedCurrenciesLoading] =
useState(false)
const currency = useSelector((state: RootState) => state.currency) const currency = useSelector((state: RootState) => state.currency)
const dispatch = useDispatch() const dispatch = useDispatch()
@ -30,6 +34,7 @@ const CurrencyDropdown = ({ depositAmount }: CurrencyDropdownProps) => {
const fetchCurrencyPrice = async () => { const fetchCurrencyPrice = async () => {
try { try {
setIsCurrencyLoading(true)
const response = await fetch( const response = await fetch(
`https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=${currency}`, `https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=${currency}`,
{ {
@ -43,6 +48,8 @@ const CurrencyDropdown = ({ depositAmount }: CurrencyDropdownProps) => {
setCurrentCurrencyAmount(data.ethereum[currency]) setCurrentCurrencyAmount(data.ethereum[currency])
} catch (error) { } catch (error) {
console.error(error) console.error(error)
} finally {
setIsCurrencyLoading(false)
} }
} }
@ -62,6 +69,8 @@ const CurrencyDropdown = ({ depositAmount }: CurrencyDropdownProps) => {
setSupportedCurrencies(newSupportedCurrencies) setSupportedCurrencies(newSupportedCurrencies)
} catch (error) { } catch (error) {
console.error(error) console.error(error)
} finally {
setIsSupportedCurrenciesLoading(false)
} }
} }
@ -77,7 +86,7 @@ const CurrencyDropdown = ({ depositAmount }: CurrencyDropdownProps) => {
<YStack space={'$2'}> <YStack space={'$2'}>
<XStack style={{ justifyContent: 'space-between' }}> <XStack style={{ justifyContent: 'space-between' }}>
<Text size={15} weight={'semibold'}> <Text size={15} weight={'semibold'}>
{currency} {isCurrencyLoading ? LOADING : currency}
</Text> </Text>
<DropdownMenu onOpenChange={changeIsOpenHandler}> <DropdownMenu onOpenChange={changeIsOpenHandler}>
<Button <Button
@ -86,18 +95,24 @@ const CurrencyDropdown = ({ depositAmount }: CurrencyDropdownProps) => {
icon={<ChevronIcon isOpen={isOpen} />} icon={<ChevronIcon isOpen={isOpen} />}
/> />
<DropdownMenu.Content width={63} side="bottom" zIndex={999}> <DropdownMenu.Content width={63} side="bottom" zIndex={999}>
{supportedCurrencies.map(currency => ( {isSupportedCurrenciesLoading ? (
<DropdownMenu.Item <DropdownMenu.Item label={LOADING} onSelect={() => {}} />
key={currency} ) : (
label={currency} supportedCurrencies.map(currency => (
onSelect={() => changeCurrencyHandler(currency)} <DropdownMenu.Item
/> key={currency}
))} label={currency}
onSelect={() => changeCurrencyHandler(currency)}
/>
))
)}
</DropdownMenu.Content> </DropdownMenu.Content>
</DropdownMenu> </DropdownMenu>
</XStack> </XStack>
<Text size={27} weight={'semibold'}> <Text size={27} weight={'semibold'}>
{formatNumbersWithComa(totalPrice)} {currency} {isCurrencyLoading
? LOADING
: `${formatNumbersWithComa(totalPrice)} ${currency}`}
</Text> </Text>
</YStack> </YStack>
) )