Skip to content

Commit bd91a18

Browse files
authored
fix: keep reference of submitting form internally (#1039)
1 parent 23cf761 commit bd91a18

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

‎src/components/CheckForm/CheckForm.tsx

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,15 @@ export const CheckForm = ({ check, disabled }: CheckFormProps) => {
102102
resolver: zodResolver(schema),
103103
});
104104

105-
const { error, handleInvalid, handleValid, testButtonRef, testCheckError, testCheckPending } = useCheckForm({
106-
check,
107-
checkType,
108-
onTestSuccess: (data) => {
109-
setAdhocTestData(data);
110-
setOpenTestCheckModal(true);
111-
},
112-
});
105+
const { error, handleInvalid, handleValid, submittingToApi, testButtonRef, testCheckError, testCheckPending } =
106+
useCheckForm({
107+
check,
108+
checkType,
109+
onTestSuccess: (data) => {
110+
setAdhocTestData(data);
111+
setOpenTestCheckModal(true);
112+
},
113+
});
113114

114115
const handleSubmit = (onValid: SubmitHandler<CheckFormValues>, onInvalid: SubmitErrorHandler<CheckFormValues>) =>
115116
formMethods.handleSubmit(onValid, onInvalid);
@@ -160,9 +161,7 @@ export const CheckForm = ({ check, disabled }: CheckFormProps) => {
160161
</Stack>
161162
);
162163

163-
const hasUnsavedChanges = error
164-
? true
165-
: checkHasChanges(defaultValues, formMethods.getValues()) && !formMethods.formState.isSubmitSuccessful;
164+
const hasUnsavedChanges = error ? true : checkHasChanges(defaultValues, formMethods.getValues()) && !submittingToApi;
166165

167166
const navModel = useMemo(() => {
168167
return isExistingCheck

‎src/components/CheckForm/checkForm.hooks.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BaseSyntheticEvent, useCallback, useRef } from 'react';
1+
import { BaseSyntheticEvent, useCallback, useRef, useState } from 'react';
22
import { FieldErrors } from 'react-hook-form';
33
import { BrowserCheckSchema } from 'schemas/forms/BrowserCheckSchema';
44
import { DNSCheckSchema } from 'schemas/forms/DNSCheckSchema';
@@ -45,14 +45,19 @@ interface UseCheckFormProps {
4545
}
4646

4747
export function useCheckForm({ check, checkType, onTestSuccess }: UseCheckFormProps) {
48+
const [submittingToApi, setSubmittingToApi] = useState(false);
4849
const navigate = useNavigation();
4950
const { updateCheck, createCheck, error } = useCUDChecks({ eventInfo: { checkType } });
5051
const testButtonRef = useRef<HTMLButtonElement>(null);
5152
const { mutate: testCheck, isPending, error: testError } = useTestCheck({ eventInfo: { checkType } });
5253

5354
const mutateCheck = useCallback(
5455
(newCheck: Check) => {
56+
setSubmittingToApi(true);
5557
const onSuccess = (data: Check) => navigate(ROUTES.Checks);
58+
const onError = (err: Error) => {
59+
setSubmittingToApi(false);
60+
};
5661

5762
if (check?.id) {
5863
return updateCheck(
@@ -61,11 +66,11 @@ export function useCheckForm({ check, checkType, onTestSuccess }: UseCheckFormPr
6166
tenantId: check.tenantId,
6267
...newCheck,
6368
},
64-
{ onSuccess }
69+
{ onSuccess, onError }
6570
);
6671
}
6772

68-
return createCheck(newCheck, { onSuccess });
73+
return createCheck(newCheck, { onSuccess, onError });
6974
},
7075
[check?.id, check?.tenantId, createCheck, navigate, updateCheck]
7176
);
@@ -79,7 +84,6 @@ export function useCheckForm({ check, checkType, onTestSuccess }: UseCheckFormPr
7984
if (submitter === testButtonRef.current) {
8085
return testCheck(toSubmit, { onSuccess: onTestSuccess });
8186
}
82-
8387
mutateCheck(toSubmit);
8488
},
8589
[mutateCheck, onTestSuccess, testCheck]
@@ -101,5 +105,6 @@ export function useCheckForm({ check, checkType, onTestSuccess }: UseCheckFormPr
101105
testButtonRef,
102106
handleValid,
103107
handleInvalid,
108+
submittingToApi,
104109
};
105110
}

0 commit comments

Comments
 (0)