1
- import { BaseSyntheticEvent , useCallback , useRef } from 'react' ;
1
+ import { BaseSyntheticEvent , useCallback , useRef , useState } from 'react' ;
2
2
import { FieldErrors } from 'react-hook-form' ;
3
3
import { BrowserCheckSchema } from 'schemas/forms/BrowserCheckSchema' ;
4
4
import { DNSCheckSchema } from 'schemas/forms/DNSCheckSchema' ;
@@ -45,14 +45,19 @@ interface UseCheckFormProps {
45
45
}
46
46
47
47
export function useCheckForm ( { check, checkType, onTestSuccess } : UseCheckFormProps ) {
48
+ const [ submittingToApi , setSubmittingToApi ] = useState ( false ) ;
48
49
const navigate = useNavigation ( ) ;
49
50
const { updateCheck, createCheck, error } = useCUDChecks ( { eventInfo : { checkType } } ) ;
50
51
const testButtonRef = useRef < HTMLButtonElement > ( null ) ;
51
52
const { mutate : testCheck , isPending, error : testError } = useTestCheck ( { eventInfo : { checkType } } ) ;
52
53
53
54
const mutateCheck = useCallback (
54
55
( newCheck : Check ) => {
56
+ setSubmittingToApi ( true ) ;
55
57
const onSuccess = ( data : Check ) => navigate ( ROUTES . Checks ) ;
58
+ const onError = ( err : Error ) => {
59
+ setSubmittingToApi ( false ) ;
60
+ } ;
56
61
57
62
if ( check ?. id ) {
58
63
return updateCheck (
@@ -61,11 +66,11 @@ export function useCheckForm({ check, checkType, onTestSuccess }: UseCheckFormPr
61
66
tenantId : check . tenantId ,
62
67
...newCheck ,
63
68
} ,
64
- { onSuccess }
69
+ { onSuccess, onError }
65
70
) ;
66
71
}
67
72
68
- return createCheck ( newCheck , { onSuccess } ) ;
73
+ return createCheck ( newCheck , { onSuccess, onError } ) ;
69
74
} ,
70
75
[ check ?. id , check ?. tenantId , createCheck , navigate , updateCheck ]
71
76
) ;
@@ -79,7 +84,6 @@ export function useCheckForm({ check, checkType, onTestSuccess }: UseCheckFormPr
79
84
if ( submitter === testButtonRef . current ) {
80
85
return testCheck ( toSubmit , { onSuccess : onTestSuccess } ) ;
81
86
}
82
-
83
87
mutateCheck ( toSubmit ) ;
84
88
} ,
85
89
[ mutateCheck , onTestSuccess , testCheck ]
@@ -101,5 +105,6 @@ export function useCheckForm({ check, checkType, onTestSuccess }: UseCheckFormPr
101
105
testButtonRef,
102
106
handleValid,
103
107
handleInvalid,
108
+ submittingToApi,
104
109
} ;
105
110
}
0 commit comments