1- import { FC , useContext } from 'react'
1+ import { Dispatch , FC , SetStateAction , useContext , useEffect , useState } from 'react'
22
33import { Form , FormDefinition , formGetInputModel , FormInputModel } from '../form'
4+ import { LoadingSpinner } from '../loading-spinner'
45import { profileContext , ProfileContextData } from '../profile-provider'
56
67import { ContactSupportFormField } from './contact-support-form.config'
@@ -18,6 +19,15 @@ const ContactSupportForm: FC<ContactSupportFormProps> = (props: ContactSupportFo
1819
1920 const { profile } : ProfileContextData = useContext ( profileContext )
2021
22+ const [ loading , setLoading ] : [ boolean , Dispatch < SetStateAction < boolean > > ] = useState < boolean > ( false )
23+ const [ saveOnSuccess , setSaveOnSuccess ] : [ boolean , Dispatch < SetStateAction < boolean > > ] = useState < boolean > ( false )
24+
25+ useEffect ( ( ) => {
26+ if ( ! loading && saveOnSuccess ) {
27+ props . onSave ( )
28+ }
29+ } , [ loading , saveOnSuccess ] )
30+
2131 function generateRequest ( inputs : ReadonlyArray < FormInputModel > ) : ContactSupportRequest {
2232 const firstName : string = formGetInputModel ( inputs , ContactSupportFormField . first ) . value as string
2333 const lastName : string = formGetInputModel ( inputs , ContactSupportFormField . last ) . value as string
@@ -34,10 +44,11 @@ const ContactSupportForm: FC<ContactSupportFormProps> = (props: ContactSupportFo
3444 }
3545
3646 async function saveAsync ( request : ContactSupportRequest ) : Promise < void > {
47+ setLoading ( true )
3748 return contactSupportSubmitRequestAsync ( request )
3849 . then ( ( ) => {
39- props . onSave ( )
40- } )
50+ setSaveOnSuccess ( true )
51+ } ) . finally ( ( ) => setLoading ( false ) )
4152 }
4253
4354 const emailElement : JSX . Element | undefined = ! ! profile ?. email
@@ -50,6 +61,7 @@ const ContactSupportForm: FC<ContactSupportFormProps> = (props: ContactSupportFo
5061
5162 return (
5263 < >
64+ < LoadingSpinner hide = { ! loading } type = 'Overlay' />
5365 < div className = { styles [ 'contact-support-intro' ] } >
5466 < p >
5567 Hi { profile ?. firstName || 'there' } , we're here to help.
0 commit comments