1
+ /* eslint-disable @typescript-eslint/no-explicit-any */
1
2
import { NavigateFunction , useNavigate , useParams } from 'react-router-dom'
2
- import React , { Dispatch , SetStateAction , useContext , useEffect , useState } from 'react'
3
+ import React , { Dispatch , SetStateAction , useCallback , useContext , useEffect , useState } from 'react'
3
4
4
5
import {
5
6
Form ,
@@ -68,7 +69,8 @@ const BugHuntIntakeForm: React.FC = () => {
68
69
BugHuntFormConfig . buttons . primaryGroup [ 1 ] . label = 'Complete and pay'
69
70
}
70
71
71
- const [ challenge , setChallenge ] : [ Challenge | undefined , Dispatch < SetStateAction < Challenge | undefined > > ] = useState ( )
72
+ const [ challenge , setChallenge ] : [ Challenge | undefined , Dispatch < SetStateAction < Challenge | undefined > > ]
73
+ = useState ( )
72
74
const [ formDef ] : [ FormDefinition , Dispatch < SetStateAction < FormDefinition > > ]
73
75
= useState < FormDefinition > ( { ...BugHuntFormConfig } )
74
76
@@ -80,7 +82,8 @@ const BugHuntIntakeForm: React.FC = () => {
80
82
const [ selectedPackage , setSelectedPackage ] : [ PricePackageName , Dispatch < SetStateAction < PricePackageName > > ]
81
83
= useState < PricePackageName > ( formValues ?. packageType )
82
84
83
- const [ disableSaveForLater , setDisableSaveForLater ] : [ boolean , Dispatch < SetStateAction < boolean > > ] = useState < boolean > ( true )
85
+ const [ disableSaveForLater , setDisableSaveForLater ] : [ boolean , Dispatch < SetStateAction < boolean > > ]
86
+ = useState < boolean > ( true )
84
87
85
88
useEffect ( ( ) => {
86
89
@@ -124,38 +127,53 @@ const BugHuntIntakeForm: React.FC = () => {
124
127
setLoading ( true )
125
128
getAndSetWork ( )
126
129
. finally ( ( ) => setLoading ( false ) )
130
+ // eslint-disable-next-line react-hooks/exhaustive-deps
127
131
} , [
128
132
isLoggedIn ,
129
133
workId ,
130
134
] )
131
135
136
+ const handleSaveSuccess : ( ) => void = ( ) => {
137
+ if ( action === 'save' ) {
138
+ navigate ( `${ dashboardRoute } /draft` )
139
+ } else if ( action === 'submit' ) {
140
+ const nextUrl : string = `${ WorkIntakeFormRoutes [ WorkType . bugHunt ] . review } /${ workId || challenge ?. id } `
141
+ navigate ( nextUrl )
142
+ }
143
+ }
144
+
132
145
useEffect ( ( ) => {
133
146
if ( ! loading && saveSuccess ) {
134
147
handleSaveSuccess ( )
135
148
}
149
+ // eslint-disable-next-line react-hooks/exhaustive-deps
136
150
} , [ loading , saveSuccess ] )
137
151
138
- const requestGenerator : ( inputs : ReadonlyArray < FormInputModel > ) => void = inputs => {
139
- const projectTitle : string = formGetInputModel ( inputs , ChallengeMetadataName . projectTitle ) . value as string
140
- const featuresToTest : string = formGetInputModel ( inputs , ChallengeMetadataName . featuresToTest ) . value as string
141
- const deliveryType : string = formGetInputModel ( inputs , ChallengeMetadataName . deliveryType ) . value as string
142
- const repositoryLink : string = formGetInputModel ( inputs , ChallengeMetadataName . repositoryLink ) . value as string
143
- const websiteURL : string = formGetInputModel ( inputs , ChallengeMetadataName . websiteURL ) . value as string
144
- const goals : string = formGetInputModel ( inputs , ChallengeMetadataName . goals ) . value as string
145
- const packageType : string = formGetInputModel ( inputs , ChallengeMetadataName . packageType ) . value as string
146
- return {
147
- deliveryType,
148
- featuresToTest,
149
- goals,
150
- packageType,
151
- projectTitle,
152
- repositoryLink,
153
- websiteURL,
154
- }
155
- }
152
+ const requestGenerator : ( inputs : ReadonlyArray < FormInputModel > ) => any
153
+ = useCallback ( ( inputs : ReadonlyArray < FormInputModel > ) => {
154
+ const projectTitle : string = formGetInputModel ( inputs , ChallengeMetadataName . projectTitle ) . value as string
155
+ const featuresToTest : string
156
+ = formGetInputModel ( inputs , ChallengeMetadataName . featuresToTest ) . value as string
157
+ const deliveryType : string = formGetInputModel ( inputs , ChallengeMetadataName . deliveryType ) . value as string
158
+ const repositoryLink : string
159
+ = formGetInputModel ( inputs , ChallengeMetadataName . repositoryLink ) . value as string
160
+ const websiteURL : string = formGetInputModel ( inputs , ChallengeMetadataName . websiteURL ) . value as string
161
+ const goals : string = formGetInputModel ( inputs , ChallengeMetadataName . goals ) . value as string
162
+ const packageType : string = formGetInputModel ( inputs , ChallengeMetadataName . packageType ) . value as string
163
+ return {
164
+ deliveryType,
165
+ featuresToTest,
166
+ goals,
167
+ packageType,
168
+ projectTitle,
169
+ repositoryLink,
170
+ websiteURL,
171
+ }
172
+ } , [ ] )
156
173
157
174
const onChange : ( inputs : ReadonlyArray < FormInputModel > ) => void = inputs => {
158
- const packageType : PricePackageName = formGetInputModel ( inputs , ChallengeMetadataName . packageType ) . value as PricePackageName
175
+ const packageType : PricePackageName
176
+ = formGetInputModel ( inputs , ChallengeMetadataName . packageType ) . value as PricePackageName
159
177
160
178
if ( packageType !== selectedPackage ) {
161
179
setSelectedPackage ( packageType )
@@ -166,6 +184,18 @@ const BugHuntIntakeForm: React.FC = () => {
166
184
setDisableSaveForLater ( ! title ?. trim ( ) )
167
185
}
168
186
187
+ const goToLoginStep : ( formData : any ) => void = ( formData : any ) => {
188
+ if ( localStorage ) {
189
+ localStorage . setItem ( 'challengeInProgress' , JSON . stringify ( formData ) )
190
+ localStorage . setItem ( 'challengeInProgressType' , WorkType . bugHunt )
191
+ }
192
+
193
+ const returnUrl : string
194
+ = encodeURIComponent ( `${ window . location . origin } ${ WorkIntakeFormRoutes [ WorkType . bugHunt ] . saveAfterLogin } ` )
195
+ const loginPromptUrl : string = `${ WorkIntakeFormRoutes [ WorkType . bugHunt ] . loginPrompt } /${ returnUrl } `
196
+ navigate ( loginPromptUrl )
197
+ }
198
+
169
199
const onSave : ( val : any ) => Promise < void > = val => {
170
200
if ( ! isLoggedIn ) {
171
201
goToLoginStep ( val )
@@ -185,45 +215,28 @@ const BugHuntIntakeForm: React.FC = () => {
185
215
. finally ( ( ) => setLoading ( false ) )
186
216
}
187
217
188
- const handleSaveSuccess : ( ) => void = ( ) => {
189
- if ( action === 'save' ) {
190
- navigate ( `${ dashboardRoute } /draft` )
191
- } else if ( action === 'submit' ) {
192
- const nextUrl : string = `${ WorkIntakeFormRoutes [ WorkType . bugHunt ] . review } /${ workId || challenge ?. id } `
193
- navigate ( nextUrl )
194
- }
195
- }
196
-
197
218
const onSaveSuccess : ( ) => void = ( ) => {
198
219
setSaveSuccess ( true )
199
220
}
200
221
201
- const goToLoginStep : ( formData : any ) => void = ( formData : any ) => {
202
- if ( localStorage ) {
203
- localStorage . setItem ( 'challengeInProgress' , JSON . stringify ( formData ) )
204
- localStorage . setItem ( 'challengeInProgressType' , WorkType . bugHunt )
205
- }
206
-
207
- const returnUrl : string = encodeURIComponent ( `${ window . location . origin } ${ WorkIntakeFormRoutes [ WorkType . bugHunt ] . saveAfterLogin } ` )
208
- const loginPromptUrl : string = `${ WorkIntakeFormRoutes [ WorkType . bugHunt ] . loginPrompt } /${ returnUrl } `
209
- navigate ( loginPromptUrl )
210
- }
211
-
212
222
/**
213
223
* This function is used to decide whether SAVE FOR LATER button should be enabled or not
214
224
* @param isPrimaryGroup whether its a primary group or not
215
225
* @param index the index of the button
216
226
* @returns true or false depending on whether its SAVE FOR LATER
217
227
*/
218
- function shouldDisableButton ( isPrimaryGroup : boolean , index : number ) : boolean {
219
- // SAVE FOR LATER belongs to primary group and its index is 0, we are interested only for that particular case
220
- // else return false which means not disabled from this function
221
- if ( isPrimaryGroup && index === 0 ) {
222
- return disableSaveForLater
223
- }
228
+ const shouldDisableButton : ( isPrimaryGroup : boolean , index : number ) => boolean
229
+ = useCallback ( ( isPrimaryGroup : boolean , index : number ) => {
230
+
231
+ // SAVE FOR LATER belongs to primary group and its index is 0,
232
+ // we are interested only for that particular case
233
+ // else return false which means not disabled from this function
234
+ if ( isPrimaryGroup && index === 0 ) {
235
+ return disableSaveForLater
236
+ }
224
237
225
- return false
226
- }
238
+ return false
239
+ } , [ disableSaveForLater ] )
227
240
228
241
return (
229
242
< >
0 commit comments