-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
65 additions
and
56 deletions.
There are no files selected for viewing
9 changes: 9 additions & 0 deletions
9
src/components/Elements/Selectors/AssetSelector/index.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.options { | ||
display: flex; | ||
justify-content: center; | ||
} | ||
|
||
.option { | ||
min-width: 250px; | ||
margin: 1em 5em; | ||
} |
46 changes: 20 additions & 26 deletions
46
src/components/Elements/Selectors/AssetSelector/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,42 @@ | ||
import { useTheme } from '@mui/material'; | ||
import { ToggleButton, ToggleButtonGroup, useTheme } from '@mui/material'; | ||
import FormControl from '@mui/material/FormControl'; | ||
import FormControlLabel from '@mui/material/FormControlLabel'; | ||
import FormLabel from '@mui/material/FormLabel'; | ||
import Radio from '@mui/material/Radio'; | ||
import RadioGroup from '@mui/material/RadioGroup'; | ||
|
||
import styles from './index.module.scss'; | ||
import { AssetType } from '@/models'; | ||
|
||
interface AssetSelectorProps { | ||
asset: AssetType; | ||
setAsset: (_: AssetType) => void; | ||
symbol: string; | ||
showRegion?: boolean; | ||
} | ||
|
||
export default function AssetSelector({ | ||
asset, | ||
setAsset, | ||
symbol, | ||
showRegion = true, | ||
}: AssetSelectorProps) { | ||
const theme = useTheme(); | ||
return ( | ||
<FormControl> | ||
<FormLabel sx={{ color: theme.palette.text.primary }}>Asset</FormLabel> | ||
<RadioGroup | ||
row | ||
<ToggleButtonGroup | ||
value={asset} | ||
onChange={(e) => setAsset(parseInt(e.target.value) as AssetType)} | ||
sx={{ color: theme.palette.text.primary }} | ||
exclusive // This ensures only one can be selected at a time | ||
onChange={(e: any) => { | ||
console.log(e.target.value); | ||
setAsset(parseInt(e.target.value) as AssetType) | ||
}} | ||
className={styles.options} | ||
> | ||
<FormControlLabel | ||
value={AssetType.TOKEN} | ||
control={<Radio />} | ||
label={`${symbol} token`} | ||
/> | ||
{showRegion && ( | ||
<FormControlLabel | ||
value={AssetType.REGION} | ||
control={<Radio />} | ||
label='Region' | ||
/> | ||
)} | ||
</RadioGroup> | ||
<ToggleButton className={styles.option} | ||
sx={{ color: theme.palette.text.primary }} | ||
value={AssetType.TOKEN}> | ||
{symbol} | ||
</ToggleButton> | ||
<ToggleButton className={styles.option} | ||
sx={{ color: theme.palette.text.primary }} | ||
value={AssetType.REGION}> | ||
Region | ||
</ToggleButton> | ||
</ToggleButtonGroup> | ||
</FormControl> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,53 @@ | ||
import { useInkathon } from '@scio-labs/use-inkathon'; | ||
import { useCallback, useEffect, useState } from 'react'; | ||
|
||
import { useCoretimeApi } from '@/contexts/apis'; | ||
import { useCoretimeApi, useRelayApi } from '@/contexts/apis'; | ||
import { ApiState } from '@/contexts/apis/types'; | ||
import { useToast } from '@/contexts/toast'; | ||
import { ApiPromise } from '@polkadot/api'; | ||
import { parseHNString } from '@/utils/functions'; | ||
|
||
// React hook for fetching balance. | ||
const useBalance = () => { | ||
const { | ||
state: { api, apiState, symbol }, | ||
state: { api: coretimeApi, apiState: coretimeApiState, symbol }, | ||
} = useCoretimeApi(); | ||
const { state: { api: relayApi, apiState: relayApiState } } = useRelayApi(); | ||
|
||
const { activeAccount } = useInkathon(); | ||
|
||
const [balance, setBalance] = useState(0); | ||
const [coretimeBalance, setCoretimeBalance] = useState(0); | ||
const [relayBalance, setRelayBalance] = useState(0); | ||
|
||
const { toastWarning } = useToast(); | ||
|
||
const fetchBalance = useCallback(async () => { | ||
if (api && apiState == ApiState.READY && activeAccount) { | ||
const accountData: any = ( | ||
await api.query.system.account(activeAccount.address) | ||
).toHuman(); | ||
const balance = parseFloat(accountData.data.free.toString()); | ||
setBalance(balance); | ||
|
||
if (balance === 0) { | ||
toastWarning( | ||
`The selected account does not have any ${symbol} tokens on the Coretime chain.` | ||
); | ||
} | ||
} | ||
}, [api, apiState, activeAccount, toastWarning, symbol]); | ||
const fetchBalance = useCallback(async (api: ApiPromise): Promise<number | undefined> => { | ||
if (!activeAccount) return; | ||
|
||
const accountData: any = ( | ||
await api.query.system.account(activeAccount.address) | ||
).toHuman(); | ||
const balance = parseHNString(accountData.data.free.toString()); | ||
|
||
return balance; | ||
}, [activeAccount, toastWarning, symbol]); | ||
|
||
useEffect(() => { | ||
fetchBalance(); | ||
}, [fetchBalance]); | ||
if (coretimeApi && coretimeApiState == ApiState.READY) { | ||
fetchBalance(coretimeApi).then((balance) => { | ||
balance !== undefined && setCoretimeBalance(balance); | ||
balance === 0 && toastWarning(`The selected account does not have any ${symbol} tokens on the Coretime chain.`) | ||
}); | ||
} | ||
if (relayApi && relayApiState == ApiState.READY) { | ||
fetchBalance(relayApi).then((balance) => { | ||
balance !== undefined && setRelayBalance(balance); | ||
}) | ||
} | ||
|
||
}, [fetchBalance, coretimeApi, coretimeApiState, relayApi, relayApiState]); | ||
|
||
return balance; | ||
return { coretimeBalance, relayBalance }; | ||
}; | ||
|
||
export default useBalance; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters