Skip to content

Commit

Permalink
fix(suite): reset amount, send max and draft on token change
Browse files Browse the repository at this point in the history
  • Loading branch information
enjojoy committed Dec 18, 2024
1 parent 280c7d1 commit 058075e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/suite/src/hooks/wallet/useSendForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -393,6 +397,7 @@ export const useSendForm = (props: UseSendFormProps): SendContextValues => {
loadTransaction,
signTransaction: sign,
setDraftSaveRequest,
resetDraft,
utxoSelection,
...sendFormUtils,
...sendFormOutputs,
Expand Down
1 change: 1 addition & 0 deletions packages/suite/src/types/wallet/sendForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export type SendContextValues<TFormValues extends FormState = FormState> =
outputs: Partial<Output & { id: string }>[]; // useFieldArray fields
updateContext: (value: Partial<UseSendFormState>) => void;
resetContext: () => void;
resetDraft: () => void;
composeTransaction: (field?: FieldPath<TFormValues>) => void;
loadTransaction: () => Promise<void>;
signTransaction: () => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ export const TokenSelect = ({ outputId }: TokenSelectProps) => {
watch,
setValue,
setDraftSaveRequest,
resetDraft,
setMax,
} = useSendFormContext();

const dispatch = useDispatch();
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 058075e

Please sign in to comment.