@@ -18,10 +18,17 @@ import { scrollOffset } from '../util/dom';
18
18
import dispatcher from '../util/dispatcher' ;
19
19
20
20
function FoiaRequestForm ( {
21
- formData, upload, onSubmit, requestForm, submissionResult, refreshReCaptcha , setRefreshReCaptcha , token , setTokenFunc ,
21
+ formData, upload, onSubmit, requestForm, submissionResult,
22
22
} ) {
23
+ const [ settingsdata , setSettingsdata ] = useState ( null ) ;
24
+ const [ token , setToken ] = useState ( '' ) ;
25
+ const [ refreshReCaptcha , setRefreshReCaptcha ] = useState ( false ) ;
23
26
const { executeRecaptcha } = useGoogleReCaptcha ( ) ;
24
27
28
+ const setTokenFunc = ( getToken ) => {
29
+ setToken ( getToken ) ;
30
+ } ;
31
+
25
32
const handleReCaptchaVerify = useCallback ( async ( ) => {
26
33
if ( ! executeRecaptcha ) {
27
34
console . log ( 'Execute recaptcha not yet available' ) ;
@@ -32,11 +39,15 @@ function FoiaRequestForm({
32
39
setTokenFunc ( newtoken ) ;
33
40
console . log ( 'newtoken:' ) ;
34
41
console . log ( newtoken ) ;
35
- } , [ ] ) ;
42
+ } , [ executeRecaptcha ] ) ;
36
43
37
44
useEffect ( ( ) => {
45
+ fetch ( '/files/settings.json' )
46
+ . then ( ( response ) => response . json ( ) )
47
+ . then ( ( result ) => setSettingsdata ( result ) )
48
+ . catch ( ( error ) => console . error ( 'Error fetching recaptcha site key:' , error ) ) ;
38
49
handleReCaptchaVerify ( ) ;
39
- } , [ ] ) ;
50
+ } , [ handleReCaptchaVerify ] ) ;
40
51
41
52
// Helper function to jump to the first form error.
42
53
function focusOnFirstError ( ) {
@@ -83,6 +94,7 @@ function FoiaRequestForm({
83
94
84
95
function onFormSubmit ( { formData : data } ) {
85
96
// Now you can use the recaptcha token for your form submission
97
+ handleReCaptchaVerify ( ) ;
86
98
data . expedited_processing . captcha = token ;
87
99
88
100
// Merge the sections into a single payload
@@ -126,26 +138,13 @@ function FoiaRequestForm({
126
138
} ;
127
139
128
140
// Map these to react-jsonschema-form Ids
129
- const steps = ( ( requestForm && requestForm . sections ) || [ ] ) . map ( ( section ) => `root_${ section . id } ` ) ;
141
+ const steps = ( requestForm . sections || [ ] ) . map ( ( section ) => `root_${ section . id } ` ) ;
130
142
131
- // eslint-disable-next-line no-nested-ternary
132
- const errors = ( submissionResult !== undefined ? ( ( submissionResult . errors instanceof Map )
143
+ const errors = ( submissionResult . errors instanceof Map )
133
144
? submissionResult . errors . toJS ( )
134
- : submissionResult . errors ) : null ) ;
135
-
145
+ : submissionResult . errors ;
136
146
const formContext = { steps, errors } ;
137
-
138
- let jsonSchema ;
139
- let uiSchema ;
140
- if ( requestForm ) {
141
- ( { jsonSchema, uiSchema } = requestForm ) ;
142
- } else {
143
- return (
144
- < Form
145
- validator = { validator }
146
- />
147
- ) ;
148
- }
147
+ const { jsonSchema, uiSchema } = requestForm ;
149
148
150
149
const templates = {
151
150
FieldTemplate : CustomFieldTemplate ,
@@ -155,14 +154,14 @@ function FoiaRequestForm({
155
154
return (
156
155
< Form
157
156
className = "foia-request-form sidebar_content-inner"
158
- disabled = { ( upload !== undefined ? upload . get ( 'inProgress' ) : false ) }
157
+ disabled = { upload . get ( 'inProgress' ) }
159
158
templates = { templates }
160
159
formContext = { formContext }
161
- formData = { ( formData !== undefined ? formData . toJS ( ) : [ ] ) }
160
+ formData = { formData . toJS ( ) }
162
161
onChange = { onChange }
163
162
onSubmit = { onFormSubmit }
164
- schema = { ( jsonSchema !== undefined ? jsonSchema : [ ] ) }
165
- uiSchema = { ( uiSchema !== undefined ? uiSchema : [ ] ) }
163
+ schema = { jsonSchema }
164
+ uiSchema = { uiSchema }
166
165
widgets = { widgets }
167
166
customValidate = { validate }
168
167
validator = { validator }
@@ -184,20 +183,28 @@ function FoiaRequestForm({
184
183
</ p >
185
184
</ div >
186
185
{ /* eslint-disable-next-line no-nested-ternary */ }
187
- { ( ( upload !== undefined ) && upload . get ( 'inProgress' ) )
186
+ { upload . get ( 'inProgress' )
188
187
? (
189
188
< UploadProgress
190
189
progressTotal = { upload . get ( 'progressTotal' ) }
191
190
progressLoaded = { upload . get ( 'progressLoaded' ) }
192
191
/>
193
- ) : (
194
- < div style = { { marginTop : '2em' } } >
195
- < button className = "usa-button usa-button-big usa-button-primary-alt" type = "submit" >
196
- Submit request
197
- </ button >
198
- </ div >
199
- ) }
200
- { ( submissionResult !== undefined && submissionResult . errorMessage )
192
+ )
193
+ : settingsdata && settingsdata . RECAPTCHA_SITE_KEY
194
+ ? (
195
+ < GoogleReCaptchaProvider reCaptchaKey = { settingsdata . RECAPTCHA_SITE_KEY } >
196
+ < GoogleReCaptcha
197
+ className = "google-recaptcha-custom-class"
198
+ onVerify = { setTokenFunc }
199
+ refreshReCaptcha = { refreshReCaptcha }
200
+ />
201
+ < div style = { { marginTop : '2em' } } />
202
+ < button className = "usa-button usa-button-big usa-button-primary-alt" type = "submit" >
203
+ Submit request
204
+ </ button >
205
+ </ GoogleReCaptchaProvider >
206
+ ) : ( < p > Invalid key</ p > ) }
207
+ { submissionResult . errorMessage
201
208
&& (
202
209
< p >
203
210
< span className = "usa-input-error-message" role = "alert" >
@@ -211,80 +218,15 @@ function FoiaRequestForm({
211
218
}
212
219
213
220
FoiaRequestForm . propTypes = {
214
- formData : PropTypes . object ,
215
- upload : PropTypes . instanceOf ( Map ) ,
221
+ formData : PropTypes . object . isRequired ,
222
+ upload : PropTypes . instanceOf ( Map ) . isRequired ,
216
223
onSubmit : PropTypes . func ,
217
- requestForm : PropTypes . object ,
218
- submissionResult : PropTypes . instanceOf ( SubmissionResult ) ,
219
- setRefreshReCaptcha : PropTypes . func ,
220
- refreshReCaptcha : PropTypes . bool ,
221
- token : PropTypes . string ,
222
- setTokenFunc : PropTypes . func ,
224
+ requestForm : PropTypes . object . isRequired ,
225
+ submissionResult : PropTypes . instanceOf ( SubmissionResult ) . isRequired ,
223
226
} ;
224
227
225
228
FoiaRequestForm . defaultProps = {
226
229
onSubmit : ( ) => { } ,
227
230
} ;
228
231
229
- function SubmitRequestPage ( {
230
- formData, upload, onSubmit, requestForm, submissionResult,
231
- } ) {
232
- const [ settingsdata , setSettingsdata ] = useState ( null ) ;
233
- const [ refreshReCaptcha , setRefreshReCaptcha ] = useState ( false ) ;
234
- const [ token , setToken ] = useState ( '' ) ;
235
-
236
- const setTokenFunc = ( getToken ) => {
237
- setToken ( getToken ) ;
238
- } ;
239
-
240
- useEffect ( ( ) => {
241
- fetch ( '/files/settings.json' )
242
- . then ( ( response ) => response . json ( ) )
243
- . then ( ( result ) => {
244
- console . log ( 'result = ' , result ) ;
245
- setSettingsdata ( result ) ;
246
- } )
247
- . catch ( ( error ) => {
248
- settingsdata . RECAPTCHA_SITE_KEY = 'DEFAULT' ;
249
- console . error ( 'Error fetching recaptcha site key:' , error ) ;
250
- } ) ;
251
- } , [ ] ) ;
252
-
253
- return (
254
- settingsdata && settingsdata . RECAPTCHA_SITE_KEY
255
- ? (
256
- // eslint-disable-next-line react/jsx-wrap-multilines
257
- < GoogleReCaptchaProvider reCaptchaKey = { settingsdata . RECAPTCHA_SITE_KEY } >
258
- < GoogleReCaptcha
259
- className = "google-recaptcha-custom-class"
260
- onVerify = { setTokenFunc }
261
- refreshReCaptcha = { refreshReCaptcha }
262
- scriptProps = { { async : true } }
263
- />
264
- < FoiaRequestForm
265
- setRefreshReCaptcha = { setRefreshReCaptcha }
266
- refreshReCaptcha = { refreshReCaptcha }
267
- token = { token }
268
- setTokenFunc = { setTokenFunc }
269
- formData = { formData }
270
- upload = { upload }
271
- onSubmit = { onSubmit }
272
- requestForm = { requestForm }
273
- submissionResult = { submissionResult }
274
- />
275
- </ GoogleReCaptchaProvider > )
276
- : (
277
- < p > Invalid key</ p >
278
- )
279
- ) ;
280
- }
281
-
282
- SubmitRequestPage . propTypes = {
283
- formData : PropTypes . object ,
284
- upload : PropTypes . instanceOf ( Map ) ,
285
- onSubmit : PropTypes . func ,
286
- requestForm : PropTypes . object ,
287
- submissionResult : PropTypes . instanceOf ( SubmissionResult ) ,
288
- } ;
289
-
290
- export default SubmitRequestPage ;
232
+ export default FoiaRequestForm ;
0 commit comments