Skip to content

Commit d8b32bf

Browse files
committed
feat: move Coingecko URL to config file; enhance error handling in account component; keep current step between tabs
1 parent 16f601e commit d8b32bf

File tree

2 files changed

+65
-31
lines changed

2 files changed

+65
-31
lines changed

src/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const PREVIEW_TABLE_REFETCH_INTERVAL = 10_000;
99
export const TABLE_REFETCH_INTERVAL = 10_000;
1010

1111
export const IPFS_GATEWAY_URL = 'https://ipfs.iex.ec';
12+
export const API_COINGECKO_URL = 'https://api.coingecko.com/api/v3/simple/';
1213

1314
export const SUPPORTED_CHAINS = [
1415
{

src/routes/$chainSlug/_layout/account.tsx

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { SUPPORTED_CHAINS } from '@/config';
1+
import { API_COINGECKO_URL, SUPPORTED_CHAINS } from '@/config';
22
import { cn } from '@/lib/utils';
33
import { useMutation, useQuery } from '@tanstack/react-query';
44
import { createFileRoute } from '@tanstack/react-router';
@@ -10,6 +10,7 @@ import WalletIcon from '@/components/icons/WalletIcon';
1010
import IexecAccountIcon from '@/components/icons/iexecAccountIcon';
1111
import { ChainSelector } from '@/components/navbar/ChainSelector';
1212
import { getIExec } from '@/externals/iexecSdkClient';
13+
import { ErrorAlert } from '@/modules/ErrorAlert';
1314
import { Tabs } from '@/modules/Tabs';
1415
import { AccountBreadcrumbs } from '@/modules/account/AccountBreadcrumbs';
1516
import { getTabs } from '@/modules/account/getTabs';
@@ -25,9 +26,20 @@ export const Route = createFileRoute('/$chainSlug/_layout/account')({
2526
function RouteComponent() {
2627
const { address: userAddress, chainId } = useUserStore();
2728
const [currentTab, setCurrentTab] = useState(0);
28-
const [currentStep, setCurrentStep] = useState(0);
29+
const [depositStep, setDepositStep] = useState(0);
30+
const [withdrawStep, setWithdrawStep] = useState(0);
2931
const [depositAmount, setDepositAmount] = useState('0');
3032
const [withdrawAmount, setWithdrawAmount] = useState('0');
33+
const getStepState = (): [
34+
number,
35+
React.Dispatch<React.SetStateAction<number>>,
36+
] => {
37+
return currentTab === 1
38+
? [withdrawStep, setWithdrawStep]
39+
: [depositStep, setDepositStep];
40+
};
41+
42+
const [currentStep] = getStepState();
3143

3244
const disabledTabs: number[] = [];
3345
const disabledReasons: Record<number, string> = {};
@@ -37,11 +49,11 @@ function RouteComponent() {
3749
disabledReasons[2] = 'The selected chain has no bridge.';
3850
}
3951

40-
const { data: rlcPrice = 0 } = useQuery({
52+
const { data: rlcPrice = 0, isError: rlcPriceIsError } = useQuery({
4153
queryKey: ['rlcPrice'],
4254
queryFn: async () => {
4355
const resp = await fetch(
44-
'https://api.coingecko.com/api/v3/simple/price?ids=iexec-rlc&vs_currencies=usd'
56+
`${API_COINGECKO_URL}price?ids=iexec-rlc&vs_currencies=usd`
4557
);
4658
const json = await resp.json();
4759
return json['iexec-rlc']?.usd as number;
@@ -91,16 +103,16 @@ function RouteComponent() {
91103
const iexec = await getIExec();
92104
const account = iexec.account;
93105
if (!account) throw new Error('Account is not initialized');
94-
setCurrentStep(1);
106+
setDepositStep(1);
95107
await account.deposit(rlcToNrlc(depositAmount));
96108
},
97109
onSuccess: () => {
98110
refetchTotalToDeposit();
99111
setDepositAmount('0');
100-
setCurrentStep(2);
112+
setDepositStep(2);
101113
},
102114
onError: () => {
103-
setCurrentStep(0);
115+
setDepositStep(0);
104116
},
105117
});
106118

@@ -112,16 +124,16 @@ function RouteComponent() {
112124
const iexec = await getIExec();
113125
const account = iexec.account;
114126
if (!account) throw new Error('Account is not initialized');
115-
setCurrentStep(1);
127+
setWithdrawStep(1);
116128
await account.withdraw(rlcToNrlc(withdrawAmount));
117129
},
118130
onSuccess: () => {
119131
refetchTotalToWithdraw();
120132
setWithdrawAmount('0');
121-
setCurrentStep(2);
133+
setWithdrawStep(2);
122134
},
123135
onError: () => {
124-
setCurrentStep(0);
136+
setWithdrawStep(0);
125137
},
126138
});
127139

@@ -170,17 +182,27 @@ function RouteComponent() {
170182
</div>
171183
Your Wallet
172184
</p>
173-
<div className="text-right text-lg font-bold">
174-
{Number(formatRLC(totalToDeposit)).toLocaleString('en', {
175-
maximumFractionDigits: 8,
176-
})}{' '}
177-
xRLC
185+
<div className="text-center text-lg font-bold md:text-right">
186+
{totalToWithdrawIsError ? (
187+
<ErrorAlert message="Fail to get wallet xRLC" />
188+
) : (
189+
<>
190+
{Number(formatRLC(totalToDeposit)).toLocaleString('en', {
191+
maximumFractionDigits: 8,
192+
})}{' '}
193+
xRLC
194+
</>
195+
)}
178196
<br />
179-
{Intl.NumberFormat('en-US', {
180-
style: 'currency',
181-
currency: 'USD',
182-
maximumFractionDigits: 2,
183-
}).format(Number(formatRLC(totalToDeposit)) * rlcPrice)}
197+
{rlcPriceIsError ? (
198+
<ErrorAlert message="Fail to get current RLC price" />
199+
) : (
200+
Intl.NumberFormat('en-US', {
201+
style: 'currency',
202+
currency: 'USD',
203+
maximumFractionDigits: 2,
204+
}).format(Number(formatRLC(totalToDeposit)) * rlcPrice)
205+
)}
184206
</div>
185207
</div>
186208
<ArrowRight
@@ -203,17 +225,27 @@ function RouteComponent() {
203225
</div>
204226
Your iExec Account
205227
</p>
206-
<div className="text-right text-lg font-bold">
207-
{Number(formatRLC(totalToWithdraw)).toLocaleString('en', {
208-
maximumFractionDigits: 8,
209-
})}{' '}
210-
xRLC
228+
<div className="text-center text-lg font-bold md:text-right">
229+
{totalToDepositIsError ? (
230+
<ErrorAlert message="Fail to get iExec account xRLC" />
231+
) : (
232+
<>
233+
{Number(formatRLC(totalToWithdraw)).toLocaleString('en', {
234+
maximumFractionDigits: 8,
235+
})}{' '}
236+
xRLC
237+
</>
238+
)}
211239
<br />
212-
{Intl.NumberFormat('en-US', {
213-
style: 'currency',
214-
currency: 'USD',
215-
maximumFractionDigits: 2,
216-
}).format(Number(formatRLC(totalToWithdraw)) * rlcPrice)}
240+
{rlcPriceIsError ? (
241+
<ErrorAlert message="Fail to get get current RLC price" />
242+
) : (
243+
Intl.NumberFormat('en-US', {
244+
style: 'currency',
245+
currency: 'USD',
246+
maximumFractionDigits: 2,
247+
}).format(Number(formatRLC(totalToWithdraw)) * rlcPrice)
248+
)}
217249
</div>
218250
</div>
219251
</div>
@@ -222,7 +254,8 @@ function RouteComponent() {
222254
currentTab={currentTab}
223255
onTabChange={(tab) => {
224256
setCurrentTab(tab);
225-
setCurrentStep(0);
257+
if (tab === 0 && depositStep === 2) setDepositStep(0);
258+
if (tab === 1 && withdrawStep === 2) setWithdrawStep(0);
226259
}}
227260
tabLabels={tabs.map((tab) => tab.title)}
228261
disabledTabs={disabledTabs}

0 commit comments

Comments
 (0)