From 058075e7d2bd9c48a3d5e6044257b61a4b51d234 Mon Sep 17 00:00:00 2001 From: Albina Nikiforova Date: Wed, 18 Dec 2024 14:17:53 +0100 Subject: [PATCH] fix(suite): reset amount, send max and draft on token change --- packages/suite/src/hooks/wallet/useSendForm.ts | 5 +++++ packages/suite/src/types/wallet/sendForm.ts | 1 + .../wallet/send/Outputs/TokenSelect/TokenSelect.tsx | 13 +++++++++++++ 3 files changed, 19 insertions(+) diff --git a/packages/suite/src/hooks/wallet/useSendForm.ts b/packages/suite/src/hooks/wallet/useSendForm.ts index 3f386dc6b3d..7350db410b2 100644 --- a/packages/suite/src/hooks/wallet/useSendForm.ts +++ b/packages/suite/src/hooks/wallet/useSendForm.ts @@ -226,6 +226,10 @@ export const useSendForm = (props: UseSendFormProps): SendContextValues => { setState(getStateFromProps(props)); // resetting state will trigger "loadDraft" useEffect block, which will reset FormState to default }, [dispatch, props, setComposedLevels]); + const resetDraft = useCallback(() => { + dispatch(removeSendFormDraftThunk()); + }, [dispatch]); + // declare useSendFormImport, sub-hook of useSendForm const { importTransaction, validateImportedTransaction } = useSendFormImport({ network: state.network, @@ -393,6 +397,7 @@ export const useSendForm = (props: UseSendFormProps): SendContextValues => { loadTransaction, signTransaction: sign, setDraftSaveRequest, + resetDraft, utxoSelection, ...sendFormUtils, ...sendFormOutputs, diff --git a/packages/suite/src/types/wallet/sendForm.ts b/packages/suite/src/types/wallet/sendForm.ts index c65242c6e84..292fa862b9d 100644 --- a/packages/suite/src/types/wallet/sendForm.ts +++ b/packages/suite/src/types/wallet/sendForm.ts @@ -76,6 +76,7 @@ export type SendContextValues = outputs: Partial[]; // useFieldArray fields updateContext: (value: Partial) => void; resetContext: () => void; + resetDraft: () => void; composeTransaction: (field?: FieldPath) => void; loadTransaction: () => Promise; signTransaction: () => void; diff --git a/packages/suite/src/views/wallet/send/Outputs/TokenSelect/TokenSelect.tsx b/packages/suite/src/views/wallet/send/Outputs/TokenSelect/TokenSelect.tsx index 4e88817877a..f3210c6744e 100644 --- a/packages/suite/src/views/wallet/send/Outputs/TokenSelect/TokenSelect.tsx +++ b/packages/suite/src/views/wallet/send/Outputs/TokenSelect/TokenSelect.tsx @@ -124,6 +124,8 @@ export const TokenSelect = ({ outputId }: TokenSelectProps) => { watch, setValue, setDraftSaveRequest, + resetDraft, + setMax, } = useSendFormContext(); const dispatch = useDispatch(); @@ -215,9 +217,20 @@ export const TokenSelect = ({ outputId }: TokenSelectProps) => { token => token.contract === selectedAsset.contractAddress, ); + resetDraft(); + setValue(tokenInputName, newlySelectedToken?.contract || account.symbol, { shouldDirty: true, }); + setValue(amountInputName, '', { + shouldDirty: true, + }); + + const isSetMaxActive = getDefaultValue('setMaxOutputId') === outputId; + + if (isSetMaxActive) { + setMax(outputId, isSetMaxActive); + } setIsTokensModalActive(false);