Skip to content

Commit

Permalink
fix(suite-native): validate send form if there is already existing draft
Browse files Browse the repository at this point in the history
  • Loading branch information
PeKne committed Oct 1, 2024
1 parent 56abb30 commit a4e23ec
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions suite-native/module-send/src/screens/SendOutputsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,16 @@ export const SendOutputsScreen = ({
decimals: network?.decimals,
},
defaultValues: {
outputs: sendFormDraft?.outputs ?? DEFAULT_VALUES,
outputs: DEFAULT_VALUES,
},
});

const {
handleSubmit,
control,
getValues,
setValue,
trigger,
formState: { isValid, isSubmitting },
} = form;
const watchedFormValues = useWatch({ control });
Expand All @@ -131,11 +133,6 @@ export const SendOutputsScreen = ({
);
}, [accountKey, dispatch, getValues]);

// Triggered for every change of watchedFormValues.
useEffect(() => {
if (isValid) debounce(storeFormDraftIfValid);
}, [storeFormDraftIfValid, watchedFormValues, debounce, isValid]);

const calculateNormalFeeMaxAmount = useCallback(async () => {
const response = await dispatch(
calculateMaxAmountWithNormalFeeThunk({
Expand All @@ -149,6 +146,25 @@ export const SendOutputsScreen = ({
}
}, [getValues, accountKey, dispatch]);

useEffect(() => {
const prefillValuesFromStoredDraft = async () => {
if (sendFormDraft?.outputs) {
setValue('outputs', sendFormDraft.outputs);
await calculateNormalFeeMaxAmount();
trigger();
}
};

prefillValuesFromStoredDraft();
// this effect should be triggered only for the first render to fill the form with the stored draft on entry.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

// Triggered for every change of watchedFormValues.
useEffect(() => {
if (isValid) debounce(storeFormDraftIfValid);
}, [storeFormDraftIfValid, watchedFormValues, debounce, isValid]);

useEffect(() => {
calculateNormalFeeMaxAmount();
}, [watchedAddress, calculateNormalFeeMaxAmount, networkFeeInfo]);
Expand Down Expand Up @@ -210,7 +226,7 @@ export const SendOutputsScreen = ({
)
}
>
<Box marginTop="extraLarge" paddingBottom="xxl">
<Box marginTop="extraLarge" marginBottom="extraLarge">
<Form form={form}>
<Box flex={1} justifyContent="space-between">
<SendOutputFields accountKey={accountKey} />
Expand Down

0 comments on commit a4e23ec

Please sign in to comment.