@@ -5,11 +5,8 @@ import {
55 Form ,
66 FormAction ,
77 FormDefinition ,
8- formGetInputFields ,
98 formGetInputModel ,
10- FormGroup ,
119 FormInputModel ,
12- formOnReset ,
1310 IconOutline ,
1411 InfoCard ,
1512 LoadingSpinner ,
@@ -48,6 +45,8 @@ const BugHuntIntakeForm: React.FC = () => {
4845 const { isLoggedIn } : ProfileContextData = useContext < ProfileContextData > ( profileContext )
4946
5047 const [ action , setAction ] : [ FormAction , Dispatch < SetStateAction < FormAction > > ] = useState ( )
48+ const [ loading , setLoading ] : [ boolean , Dispatch < SetStateAction < boolean > > ] = useState ( false )
49+ const [ saveSuccess , setSaveSuccess ] : [ boolean , Dispatch < SetStateAction < boolean > > ] = useState ( false )
5150
5251 BugHuntFormConfig . buttons . primaryGroup [ 0 ] . onClick = ( ) => { setAction ( 'save' ) }
5352 BugHuntFormConfig . buttons . primaryGroup [ 0 ] . hidden = ! isLoggedIn
@@ -68,17 +67,6 @@ const BugHuntIntakeForm: React.FC = () => {
6867 const [ selectedPackage , setSelectedPackage ] : [ PricePackageName , Dispatch < SetStateAction < PricePackageName > > ]
6968 = useState < PricePackageName > ( formValues ?. packageType )
7069
71- const formInputs : Array < FormInputModel > = formGetInputFields ( formDef . groups as Array < FormGroup > )
72-
73- useEffect ( ( ) => {
74- if ( ! workId && ! challenge ) {
75- formOnReset ( formInputs , formValues )
76- }
77- // Disabling lint rule as we only want this to run one time when component mounts, otherwise it resets
78- // the form for a user that is not logged in and has no challenge created yet
79- // eslint-disable-next-line react-hooks/exhaustive-deps
80- } , [ ] )
81-
8270 useEffect ( ( ) => {
8371
8472 async function getAndSetWork ( ) : Promise < void > {
@@ -113,13 +101,20 @@ const BugHuntIntakeForm: React.FC = () => {
113101 }
114102 }
115103
116- getAndSetWork ( )
104+ setLoading ( true )
105+ getAndSetWork ( ) . finally ( ( ) => setLoading ( false ) )
117106 } , [
118107 isLoggedIn ,
119108 selectedPackage ,
120109 workId ,
121110 ] )
122111
112+ useEffect ( ( ) => {
113+ if ( ! loading && saveSuccess ) {
114+ handleSaveSuccess ( )
115+ }
116+ } , [ loading , saveSuccess ] )
117+
123118 const requestGenerator : ( inputs : ReadonlyArray < FormInputModel > ) => void = ( inputs ) => {
124119 const projectTitle : string = formGetInputModel ( inputs , ChallengeMetadataName . projectTitle ) . value as string
125120 const featuresToTest : string = formGetInputModel ( inputs , ChallengeMetadataName . featuresToTest ) . value as string
@@ -161,16 +156,21 @@ const BugHuntIntakeForm: React.FC = () => {
161156 val . currentStep = 'review'
162157 }
163158
164- return workUpdateAsync ( WorkType . bugHunt , challenge , val )
159+ setLoading ( true )
160+ return workUpdateAsync ( WorkType . bugHunt , challenge , val ) . finally ( ( ) => setLoading ( false ) )
161+ }
162+
163+ const handleSaveSuccess : ( ) => void = ( ) => {
164+ if ( action === 'save' ) {
165+ navigate ( `${ dashboardRoute } /draft` )
166+ } else if ( action === 'submit' ) {
167+ const nextUrl : string = `${ WorkIntakeFormRoutes [ WorkType . bugHunt ] [ 'review' ] } /${ workId || challenge ?. id } `
168+ navigate ( nextUrl )
169+ }
165170 }
166171
167172 const onSaveSuccess : ( ) => void = ( ) => {
168- if ( action === 'save' ) {
169- navigate ( `${ dashboardRoute } /draft` )
170- } else if ( action === 'submit' ) {
171- const nextUrl : string = `${ WorkIntakeFormRoutes [ WorkType . bugHunt ] [ 'review' ] } /${ workId || challenge ?. id } `
172- navigate ( nextUrl )
173- }
173+ setSaveSuccess ( true )
174174 }
175175
176176 const goToLoginStep : ( formData : any ) => void = ( formData : any ) => {
@@ -183,12 +183,9 @@ const BugHuntIntakeForm: React.FC = () => {
183183 navigate ( loginPromptUrl )
184184 }
185185
186- if ( ! challenge && workId ) {
187- return < LoadingSpinner />
188- }
189-
190186 return (
191187 < >
188+ < LoadingSpinner hide = { ! loading } type = 'Overlay' />
192189 < IntakeFormsBreadcrumb
193190 basicInfoRoute = { WorkIntakeFormRoutes [ WorkType . bugHunt ] [ 'basicInfo' ] }
194191 workType = { workBugHuntConfig . type }
0 commit comments