From f7ba4501e6c0380e8a1b64ef5b738bdacad853ad Mon Sep 17 00:00:00 2001 From: Martin Homola Date: Fri, 17 Jan 2025 16:19:22 +0100 Subject: [PATCH] fix(trade): unnecessary requesting in buy/sell/swap --- .../form/common/useCoinmarketInitializer.ts | 7 +++++++ .../coinmarket/form/useCoinmarketBuyForm.tsx | 21 ++++++++++++++++--- .../form/useCoinmarketExchangeForm.ts | 2 +- .../coinmarket/form/useCoinmarketSellForm.ts | 2 +- .../suite/src/types/coinmarket/coinmarket.ts | 5 +++-- 5 files changed, 30 insertions(+), 7 deletions(-) diff --git a/packages/suite/src/hooks/wallet/coinmarket/form/common/useCoinmarketInitializer.ts b/packages/suite/src/hooks/wallet/coinmarket/form/common/useCoinmarketInitializer.ts index 34292879b76..65617fcac75 100644 --- a/packages/suite/src/hooks/wallet/coinmarket/form/common/useCoinmarketInitializer.ts +++ b/packages/suite/src/hooks/wallet/coinmarket/form/common/useCoinmarketInitializer.ts @@ -12,6 +12,7 @@ import { export const useCoinmarketInitializer = ({ selectedAccount, + pageType, }: UseCoinmarketCommonProps): UseCoinmarketCommonReturnProps => { const timer = useTimer(); const { account } = selectedAccount; @@ -24,6 +25,12 @@ export const useCoinmarketInitializer = ({ timer.stop(); } + if (pageType === 'confirm') { + timer.stop(); + + return; + } + if (timer.timeSpend.seconds === INVITY_API_RELOAD_QUOTES_AFTER_SECONDS) { callback(); } diff --git a/packages/suite/src/hooks/wallet/coinmarket/form/useCoinmarketBuyForm.tsx b/packages/suite/src/hooks/wallet/coinmarket/form/useCoinmarketBuyForm.tsx index 3dfa4468738..b453bfc218d 100644 --- a/packages/suite/src/hooks/wallet/coinmarket/form/useCoinmarketBuyForm.tsx +++ b/packages/suite/src/hooks/wallet/coinmarket/form/useCoinmarketBuyForm.tsx @@ -64,7 +64,7 @@ export const useCoinmarketBuyForm = ({ useSelector(state => state.wallet.coinmarket.buy); const { cryptoIdToCoinSymbol } = useCoinmarketInfo(); const { callInProgress, account, timer, device, setCallInProgress, checkQuotesTimer } = - useCoinmarketInitializer({ selectedAccount, type }); + useCoinmarketInitializer({ selectedAccount, pageType }); const { paymentMethods, getPaymentMethods, getQuotesByPaymentMethod } = useCoinmarketPaymentMethod(); const { navigateToBuyForm, navigateToBuyOffers, navigateToBuyConfirm } = @@ -386,6 +386,10 @@ export const useCoinmarketBuyForm = ({ // call change handler on every change of text inputs with debounce useDebounce( () => { + if (pageType === 'confirm') { + return; + } + if ( isChanged(previousValues.current?.fiatInput, values.fiatInput) || isChanged(previousValues.current?.cryptoInput, values.cryptoInput) @@ -398,11 +402,22 @@ export const useCoinmarketBuyForm = ({ } }, 500, - [previousValues, values.fiatInput, values.cryptoInput, handleChange, handleSubmit], + [ + previousValues, + values.fiatInput, + values.cryptoInput, + pageType, + handleChange, + handleSubmit, + ], ); // call change handler on every change of select inputs useEffect(() => { + if (pageType === 'confirm') { + return; + } + if ( isChanged(previousValues.current?.countrySelect, values.countrySelect) || isChanged(previousValues.current?.currencySelect, values.currencySelect) || @@ -414,7 +429,7 @@ export const useCoinmarketBuyForm = ({ previousValues.current = values; } - }, [previousValues, values, handleChange, handleSubmit, isNotFormPage]); + }, [previousValues, values, isNotFormPage, pageType, handleChange, handleSubmit]); useEffect(() => { // when draft doesn't exist, we need to bind actual default values - that happens when we've got buyInfo from Invity API server diff --git a/packages/suite/src/hooks/wallet/coinmarket/form/useCoinmarketExchangeForm.ts b/packages/suite/src/hooks/wallet/coinmarket/form/useCoinmarketExchangeForm.ts index 4d141276564..988922acbfb 100644 --- a/packages/suite/src/hooks/wallet/coinmarket/form/useCoinmarketExchangeForm.ts +++ b/packages/suite/src/hooks/wallet/coinmarket/form/useCoinmarketExchangeForm.ts @@ -93,7 +93,7 @@ export const useCoinmarketExchangeForm = ({ isNotFormPage, }); const { callInProgress, timer, device, setCallInProgress, checkQuotesTimer } = - useCoinmarketInitializer({ selectedAccount, type }); + useCoinmarketInitializer({ selectedAccount, pageType }); const { buildDefaultCryptoOption } = useCoinmarketInfo(); const dispatch = useDispatch(); diff --git a/packages/suite/src/hooks/wallet/coinmarket/form/useCoinmarketSellForm.ts b/packages/suite/src/hooks/wallet/coinmarket/form/useCoinmarketSellForm.ts index 6bf0c27cc32..3fdc4a9d54b 100644 --- a/packages/suite/src/hooks/wallet/coinmarket/form/useCoinmarketSellForm.ts +++ b/packages/suite/src/hooks/wallet/coinmarket/form/useCoinmarketSellForm.ts @@ -82,7 +82,7 @@ export const useCoinmarketSellForm = ({ isNotFormPage, }); const { callInProgress, timer, device, setCallInProgress, checkQuotesTimer } = - useCoinmarketInitializer({ selectedAccount, type }); + useCoinmarketInitializer({ selectedAccount, pageType }); const { paymentMethods, getPaymentMethods, getQuotesByPaymentMethod } = useCoinmarketPaymentMethod(); const { diff --git a/packages/suite/src/types/coinmarket/coinmarket.ts b/packages/suite/src/types/coinmarket/coinmarket.ts index c82060a8f05..11e134b5607 100644 --- a/packages/suite/src/types/coinmarket/coinmarket.ts +++ b/packages/suite/src/types/coinmarket/coinmarket.ts @@ -42,9 +42,11 @@ import type { SellInfo } from 'src/actions/wallet/coinmarketSellActions'; import type { ExchangeInfo } from 'src/actions/wallet/coinmarketExchangeActions'; import type { BuyInfo } from 'src/actions/wallet/coinmarketBuyActions'; +type CoinmarketPageType = 'form' | 'offers' | 'confirm'; + export type UseCoinmarketProps = { selectedAccount: SelectedAccountLoaded }; export type UseCoinmarketCommonProps = UseCoinmarketProps & { - type: CoinmarketTradeType; + pageType: CoinmarketPageType; }; export interface UseCoinmarketCommonReturnProps { callInProgress: boolean; @@ -54,7 +56,6 @@ export interface UseCoinmarketCommonReturnProps { setCallInProgress: (state: boolean) => void; checkQuotesTimer: (callback: () => Promise) => void; } -type CoinmarketPageType = 'form' | 'offers' | 'confirm'; export type UseCoinmarketFormProps = UseCoinmarketProps & { /** * Difference between form and offers is that on the offers page are used all data filled in the form