Skip to content

Commit

Permalink
fix(trade): unnecessary requesting in buy/sell/swap
Browse files Browse the repository at this point in the history
  • Loading branch information
adderpositive committed Jan 17, 2025
1 parent 9c98465 commit d965de9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {

export const useCoinmarketInitializer = ({
selectedAccount,
pageType,
}: UseCoinmarketCommonProps): UseCoinmarketCommonReturnProps => {
const timer = useTimer();
const { account } = selectedAccount;
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CoinmarketTradeBuyType>();
const { navigateToBuyForm, navigateToBuyOffers, navigateToBuyConfirm } =
Expand Down Expand Up @@ -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)
Expand All @@ -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) ||
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CoinmarketTradeSellType>();
const {
Expand Down
5 changes: 3 additions & 2 deletions packages/suite/src/types/coinmarket/coinmarket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -54,7 +56,6 @@ export interface UseCoinmarketCommonReturnProps {
setCallInProgress: (state: boolean) => void;
checkQuotesTimer: (callback: () => Promise<void>) => 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
Expand Down

0 comments on commit d965de9

Please sign in to comment.