mirror of
https://github.com/kremalicious/blog.git
synced 2024-11-22 01:46:51 +01:00
refactor
This commit is contained in:
parent
0ca6558def
commit
2f6e4f0b3c
@ -2,35 +2,13 @@ import * as Select from '@radix-ui/react-select'
|
||||
import './Select.css'
|
||||
import { SelectItem } from './SelectItem'
|
||||
import { ChevronDown, ChevronsDown, ChevronsUp } from '@images/components/react'
|
||||
import { useEffect, useState } from 'react'
|
||||
import { getBalance } from '../../api/getBalance'
|
||||
import { useAccount, useNetwork } from 'wagmi'
|
||||
import { useTokens } from '../../hooks/useTokens'
|
||||
|
||||
export function TokenSelect() {
|
||||
const { address } = useAccount()
|
||||
const { chain } = useNetwork()
|
||||
|
||||
const [tokens, setTokens] = useState()
|
||||
|
||||
useEffect(() => {
|
||||
if (!address || !chain) return
|
||||
|
||||
async function getTokens() {
|
||||
try {
|
||||
const response = await getBalance(address)
|
||||
const tokens = response.filter(
|
||||
(token: any) => parseInt(token.chainId) === chain?.id
|
||||
)
|
||||
setTokens(tokens)
|
||||
} catch (error) {
|
||||
console.error((error as Error).message)
|
||||
}
|
||||
}
|
||||
getTokens()
|
||||
}, [address, chain])
|
||||
const { data: tokens } = useTokens()
|
||||
|
||||
return (
|
||||
<Select.Root disabled={!address}>
|
||||
<Select.Root disabled={!tokens}>
|
||||
<Select.Trigger className="SelectTrigger" aria-label="Token">
|
||||
<Select.Value placeholder="…" />
|
||||
<Select.Icon>
|
||||
|
36
src/components/Sponsor/Web3Donation/hooks/useTokens.tsx
Normal file
36
src/components/Sponsor/Web3Donation/hooks/useTokens.tsx
Normal file
@ -0,0 +1,36 @@
|
||||
import { useState, useEffect } from 'react'
|
||||
import { useAccount, useNetwork } from 'wagmi'
|
||||
import { getBalance } from '../api/getBalance'
|
||||
|
||||
export function useTokens() {
|
||||
const { address } = useAccount()
|
||||
const { chain } = useNetwork()
|
||||
|
||||
const [data, setData] = useState()
|
||||
const [isLoading, setIsLoading] = useState<boolean>()
|
||||
const [isError, setIsError] = useState<boolean>()
|
||||
|
||||
useEffect(() => {
|
||||
if (!address || !chain) return
|
||||
|
||||
async function getTokens() {
|
||||
setIsLoading(true)
|
||||
|
||||
try {
|
||||
const response = await getBalance(address)
|
||||
const tokens = response.filter(
|
||||
(token: any) => parseInt(token.chainId) === chain?.id
|
||||
)
|
||||
setData(tokens)
|
||||
setIsLoading(false)
|
||||
} catch (error) {
|
||||
setIsError(true)
|
||||
setIsLoading(false)
|
||||
console.error((error as Error).message)
|
||||
}
|
||||
}
|
||||
getTokens()
|
||||
}, [address, chain])
|
||||
|
||||
return { data, isLoading, isError }
|
||||
}
|
Loading…
Reference in New Issue
Block a user