1
- import React , { useState , useEffect , useCallback } from 'react' ;
1
+ import React , { useRef , useState , useEffect } from 'react' ;
2
2
import PropTypes from 'prop-types' ;
3
3
import Form from '@rjsf/core' ;
4
4
import validator from '@rjsf/validator-ajv8' ;
5
5
import { Map } from 'immutable' ;
6
6
import CustomFieldTemplate from 'components/request_custom_field_template' ;
7
7
import USWDSRadioWidget from 'components/uswds_radio_widget' ;
8
8
import USWDSCheckboxWidget from 'components/uswds_checkbox_widget' ;
9
- import { GoogleReCaptchaProvider , GoogleReCaptcha , useGoogleReCaptcha } from 'react-google-recaptcha-v3' ; // Import v3 hook
9
+ import ReCAPTCHA from 'react-google-recaptcha' ;
10
10
import { requestActions } from '../actions' ;
11
11
import { SubmissionResult } from '../models' ;
12
12
import CustomObjectFieldTemplate from './object_field_template' ;
@@ -20,34 +20,16 @@ import dispatcher from '../util/dispatcher';
20
20
function FoiaRequestForm ( {
21
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 ) ;
26
- const { executeRecaptcha } = useGoogleReCaptcha ( ) ;
23
+ const recaptchaRef = useRef ( ) ;
27
24
28
- const setTokenFunc = ( getToken ) => {
29
- setToken ( getToken ) ;
30
- } ;
31
-
32
- const handleReCaptchaVerify = useCallback ( async ( ) => {
33
- if ( ! executeRecaptcha ) {
34
- console . log ( 'Execute recaptcha not yet available' ) ;
35
- return ;
36
- }
37
-
38
- const newtoken = await executeRecaptcha ( 'submit' ) ;
39
- setTokenFunc ( newtoken ) ;
40
- console . log ( 'newtoken:' ) ;
41
- console . log ( newtoken ) ;
42
- } , [ executeRecaptcha ] ) ;
25
+ const [ settingsdata , setData ] = useState ( null ) ;
43
26
44
27
useEffect ( ( ) => {
45
28
fetch ( '/files/settings.json' )
46
29
. then ( ( response ) => response . json ( ) )
47
- . then ( ( result ) => setSettingsdata ( result ) )
30
+ . then ( ( result ) => setData ( result ) )
48
31
. catch ( ( error ) => console . error ( 'Error fetching recaptcha site key:' , error ) ) ;
49
- handleReCaptchaVerify ( ) ;
50
- } , [ handleReCaptchaVerify ] ) ;
32
+ } , [ ] ) ;
51
33
52
34
// Helper function to jump to the first form error.
53
35
function focusOnFirstError ( ) {
@@ -75,7 +57,6 @@ function FoiaRequestForm({
75
57
76
58
// Listen for jsonSchema validation errors and jump to them.
77
59
function onError ( ) {
78
- setRefreshReCaptcha ( ! refreshReCaptcha ) ;
79
60
focusOnFirstError ( ) ;
80
61
}
81
62
@@ -93,23 +74,20 @@ function FoiaRequestForm({
93
74
}
94
75
95
76
function onFormSubmit ( { formData : data } ) {
96
- // Now you can use the recaptcha token for your form submission
97
- handleReCaptchaVerify ( ) ;
98
- data . expedited_processing . captcha = token ;
77
+ const recaptchaValue = recaptchaRef . current . getValue ( ) ;
78
+ // Now you can use the recaptchaValue for your form submission
79
+
80
+ // TODO - probably not needed -- remove ?
81
+ // The captcha field is added to the Expedited Processing section.
82
+ data . expedited_processing . captcha = recaptchaValue ;
99
83
100
84
// Merge the sections into a single payload
101
85
const payload = rf . mergeSectionFormData ( data ) ;
102
86
// Transform file fields to attachments
103
87
findFileFields ( requestForm )
104
88
. filter ( ( fileFieldName ) => fileFieldName in payload )
105
89
. forEach ( ( fileFieldName ) => {
106
- payload [ fileFieldName ] = dataUrlToAttachment ( payload [ fileFieldName ] )
107
- . catch ( ( error ) => {
108
- console . error ( 'Error:' , error ) ;
109
- } ) ;
110
- } )
111
- . catch ( ( error ) => {
112
- console . error ( 'Error2:' , error ) ;
90
+ payload [ fileFieldName ] = dataUrlToAttachment ( payload [ fileFieldName ] ) ;
113
91
} ) ;
114
92
// Submit the request
115
93
return requestActions
@@ -182,28 +160,25 @@ function FoiaRequestForm({
182
160
using the contact information provided to you on this site.
183
161
</ p >
184
162
</ div >
185
- { /* eslint-disable-next-line no-nested-ternary */ }
186
163
{ upload . get ( 'inProgress' )
187
164
? (
188
165
< UploadProgress
189
166
progressTotal = { upload . get ( 'progressTotal' ) }
190
167
progressLoaded = { upload . get ( 'progressLoaded' ) }
191
168
/>
192
169
)
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 > ) }
170
+ : (
171
+ < div style = { { marginTop : '2em' } } >
172
+ { settingsdata && settingsdata . RECAPTCHA_SITE_KEY
173
+ ? < ReCAPTCHA ref = { recaptchaRef } sitekey = { settingsdata . RECAPTCHA_SITE_KEY } /> : < p > Inavlid Site Key</ p > }
174
+ < button
175
+ className = "usa-button usa-button-big usa-button-primary-alt"
176
+ type = "submit"
177
+ >
178
+ Submit request
179
+ </ button >
180
+ </ div >
181
+ ) }
207
182
{ submissionResult . errorMessage
208
183
&& (
209
184
< p >
0 commit comments