Skip to content

Commit

Permalink
fix(coinmarket): buy form - set default fiat amount if empty
Browse files Browse the repository at this point in the history
  • Loading branch information
adderpositive committed Jun 20, 2024
1 parent 29bde5b commit d3d3755
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { FiatCurrencyCode } from '@suite-common/suite-config';

const defaultFiatCurrencies = new Map<FiatCurrencyCode, string>([['czk', '3000']]);

export default defaultFiatCurrencies;
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import * as routerActions from 'src/actions/suite/routerActions';
import { notificationsActions } from '@suite-common/toast-notifications';
import { isDesktop } from '@trezor/env-utils';
import useCoinmarketPaymentMethod from './useCoinmarketPaymentMethod';
import defaultFiatCurrencies from 'src/constants/wallet/coinmarket/fiatCurrencies';

const useCoinmarketBuyForm = ({
selectedAccount,
Expand Down Expand Up @@ -108,17 +109,26 @@ const useCoinmarketBuyForm = ({
const { saveDraft, getDraft, removeDraft } =
useFormDraft<CoinmarketBuyFormProps>('coinmarket-buy');
const draft = getDraft(account.key);
const isDraft = !!draft || !!offFirstRequest;
const draftUpdated: CoinmarketBuyFormProps | null = draft
? {
...draft,
fiatInput:
draft.fiatInput && draft.fiatInput !== ''
? draft.fiatInput
: defaultFiatCurrencies.get('czk'),
}
: null;
const isDraft = !!draftUpdated || !!offFirstRequest;
const { defaultValues, defaultCountry, defaultCurrency, defaultPaymentMethod } =
useCoinmarketBuyFormDefaultValues(account.symbol, buyInfo, paymentMethods);
const methods = useForm<CoinmarketBuyFormProps>({
mode: 'onChange',
defaultValues: isDraft ? draft : defaultValues,
defaultValues: isDraft && draftUpdated ? draftUpdated : defaultValues,
});
const { register, control, formState, reset, setValue, getValues, handleSubmit } = methods;
const values = useWatch<CoinmarketBuyFormProps>({ control }) as CoinmarketBuyFormProps;
const previousValues = useRef<CoinmarketBuyFormProps | null>(
offFirstRequest ? (draft as CoinmarketBuyFormProps) : null,
offFirstRequest ? draftUpdated : null,
);

// form states
Expand Down Expand Up @@ -407,7 +417,13 @@ const useCoinmarketBuyForm = ({
useDebounce(
() => {
if (!formState.isValidating && Object.keys(formState.errors).length === 0) {
saveDraft(account.key, values as CoinmarketBuyFormProps);
saveDraft(account.key, {
...values,
fiatInput:
values.fiatInput !== ''
? values.fiatInput
: defaultFiatCurrencies.get('czk'),
});
}
},
200,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from 'src/utils/wallet/coinmarket/coinmarketUtils';
import { CoinmarketBuyFormDefaultValuesProps } from 'src/types/coinmarket/coinmarketForm';
import { CoinmarketPaymentMethodListProps } from 'src/types/coinmarket/coinmarket';
import defaultFiatCurrencies from 'src/constants/wallet/coinmarket/fiatCurrencies';

export const useCoinmarketBuyFormDefaultValues = (
accountSymbol: Account['symbol'],
Expand Down Expand Up @@ -37,7 +38,7 @@ export const useCoinmarketBuyFormDefaultValues = (
() =>
buyInfo
? {
fiatInput: '3000',
fiatInput: defaultFiatCurrencies.get('czk'),
cryptoInput: undefined,
currencySelect: defaultCurrency,
cryptoSelect: defaultCrypto,
Expand Down

0 comments on commit d3d3755

Please sign in to comment.