Skip to content

Commit e2281ac

Browse files
benmathisconnect
andcommitted
frontend/feetargets: load fee targets only once
Due to the useEffect fetching the fee targets depending on config, it was executed many times when going to the send screen, while it should only be called once. This commit fixes this. While not strictly necessary, I also moved the fee target fetching its own `useLoad()`, which should decrease the chance that we will accidentally increase the number of requests again. Co-Authored-By: thisconnect <this@shiftcrypto.ch>
1 parent 7deabeb commit e2281ac

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

frontends/web/src/routes/account/send/feetargets.test.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ vi.mock('../../../i18n/i18n');
2525
import { render, waitFor } from '@testing-library/react';
2626
import { FeeTargets } from './feetargets';
2727
import { apiGet } from '../../../utils/request';
28-
import * as apiHooks from '../../../hooks/api';
2928

30-
const useLoadSpy = vi.spyOn(apiHooks, 'useLoad');
29+
import * as utilsConfig from '../../../utils/config';
30+
const getConfig = vi.spyOn(utilsConfig, 'getConfig');
3131

3232
describe('routes/account/send/feetargets', () => {
3333

3434
it('should match the snapshot', async () => {
35-
useLoadSpy.mockReturnValue({ frontend: { expertFee: false } });
35+
getConfig.mockReturnValue(Promise.resolve({
36+
frontend: { expertFee: false }
37+
}));
3638
(apiGet as Mock).mockResolvedValue({
3739
defaultFeeTarget: 'economy',
3840
feeTargets: [
@@ -80,7 +82,7 @@ describe('routes/account/send/feetargets', () => {
8082
});
8183

8284
it('should match the snapshot with empty feetargets', async () => {
83-
useLoadSpy.mockReturnValue({ frontend: { expertFee: false } });
85+
getConfig.mockReturnValue(Promise.resolve({ frontend: { expertFee: false } }));
8486
(apiGet as Mock).mockResolvedValue({
8587
defaultFeeTarget: '',
8688
feeTargets: [],
@@ -100,7 +102,7 @@ describe('routes/account/send/feetargets', () => {
100102
});
101103

102104
it('should call onFeeTargetChange with default', () => new Promise<void>(async done => {
103-
useLoadSpy.mockReturnValue({ frontend: { expertFee: false } });
105+
getConfig.mockReturnValue(Promise.resolve({ frontend: { expertFee: false } }));
104106
const apiGetMock = (apiGet as Mock).mockResolvedValue({
105107
defaultFeeTarget: 'normal',
106108
feeTargets: [

frontends/web/src/routes/account/send/feetargets.tsx

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ export const FeeTargets = ({
6262
const [options, setOptions] = useState<TOptions[] | null>(null);
6363
const [noFeeTargets, setNoFeeTargets] = useState<boolean>(false);
6464

65+
const feeTargets = useLoad(() => accountApi.getFeeTargetList(accountCode));
66+
6567
const inputRef = useRef<HTMLInputElement & { autofocus: boolean }>(null);
6668

6769
const focusInput = useCallback(() => {
@@ -70,32 +72,30 @@ export const FeeTargets = ({
7072
}
7173
}, [disabled]);
7274

75+
7376
useEffect(() => {
74-
accountApi.getFeeTargetList(accountCode)
75-
.then(({ feeTargets, defaultFeeTarget }) => {
76-
if (config) {
77-
const expert = config.frontend.expertFee || feeTargets.length === 0;
78-
const options = feeTargets.map(({ code, feeRateInfo }) => ({
79-
value: code,
80-
text: t(`send.feeTarget.label.${code}`) + (expert && feeRateInfo ? ` (${feeRateInfo})` : ''),
81-
}));
82-
if (expert) {
83-
options.push({
84-
value: 'custom',
85-
text: t('send.feeTarget.label.custom'),
86-
});
87-
}
88-
setOptions(options);
89-
setFeeTarget(defaultFeeTarget);
90-
onFeeTargetChange(defaultFeeTarget);
91-
if (feeTargets.length === 0) {
92-
setNoFeeTargets(true);
93-
}
94-
}
95-
})
96-
.catch(console.error);
77+
if (!config || !feeTargets) {
78+
return;
79+
}
80+
const expert = config.frontend.expertFee || feeTargets.feeTargets.length === 0;
81+
const options = feeTargets.feeTargets.map(({ code, feeRateInfo }) => ({
82+
value: code,
83+
text: t(`send.feeTarget.label.${code}`) + (expert && feeRateInfo ? ` (${feeRateInfo})` : ''),
84+
}));
85+
if (expert) {
86+
options.push({
87+
value: 'custom',
88+
text: t('send.feeTarget.label.custom'),
89+
});
90+
}
91+
setOptions(options);
92+
setFeeTarget(feeTargets.defaultFeeTarget);
93+
onFeeTargetChange(feeTargets.defaultFeeTarget);
94+
if (feeTargets.feeTargets.length === 0) {
95+
setNoFeeTargets(true);
96+
}
9797
focusInput();
98-
}, [t, focusInput, accountCode, config, onFeeTargetChange]);
98+
}, [t, feeTargets, focusInput, accountCode, config, onFeeTargetChange]);
9999

100100
const handleFeeTargetChange = (event: React.SyntheticEvent) => {
101101
const target = event.target as HTMLSelectElement;
@@ -232,4 +232,3 @@ export const FeeTargets = ({
232232
)
233233
);
234234
};
235-

0 commit comments

Comments
 (0)