-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbalance.tsx
77 lines (71 loc) · 2.6 KB
/
balance.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { useSwapCanisterBalances, useSwapCanisterLists } from '@/hooks';
import { selectPlugState, useAppSelector } from '@/store';
import { useEffect, useState } from 'react';
/**
* Balance Section React Component
* Example of a component that displays balances
*/
export const BalanceSection = () => {
// Use custom hooks and the redux store
const { balanceList, isLoading, updateBalanceList, balanceOf } =
useSwapCanisterBalances();
const { tokenList } = useSwapCanisterLists();
const { principal } = useAppSelector(selectPlugState);
// Create a state to manage the principal id shown and searched
const [findingPrincipalId, setFindingPrincipalId] = useState<string>();
// If the principal from our Plug state changes we update the balance list
useEffect(() => {
if (principal) {
updateBalanceList(principal.toString());
}
}, [principal]);
// Update the shown principal id if the searched from store changes
useEffect(() => {
setFindingPrincipalId(balanceOf);
}, [balanceOf]);
const handleGetBalance = () => {
updateBalanceList(findingPrincipalId);
};
return (
<section>
<h1>Balances</h1>
{/** Render a form to search for other a principal id balance */}
<div>
Principal Id:
<input
value={findingPrincipalId}
onChange={(e) => setFindingPrincipalId(e.currentTarget.value)}
/>
<button onClick={handleGetBalance}>Get Balances</button>
</div>
<div className="card-list">
{/** Render the balance list if is searching a balance for balanceOf from store */}
{balanceOf &&
(isLoading || !tokenList || !balanceList ? (
// Render loading if is loading
<span>Loading...</span>
) : (
// Render the balance list
Object.entries(balanceList).map(([tokenId, balance]) => (
<div className="token-card" key={tokenId}>
<h2>{tokenList[tokenId].symbol}</h2>
<span className="data-row">
<label>Wallet: </label>
{balance.token.toString()}
</span>
<span className="data-row">
<label>Sonic: </label>
{balance.sonic.toString()}
</span>
<span className="data-row">
<label>Total: </label>
{balance.total.toString()}
</span>
</div>
))
))}
</div>
</section>
);
};