1
- import React 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 ReCAPTCHA from 'react-google-recaptcha' ;
9
10
import { requestActions } from '../actions' ;
10
11
import { SubmissionResult } from '../models' ;
11
12
import CustomObjectFieldTemplate from './object_field_template' ;
@@ -19,6 +20,17 @@ import dispatcher from '../util/dispatcher';
19
20
function FoiaRequestForm ( {
20
21
formData, upload, onSubmit, requestForm, submissionResult,
21
22
} ) {
23
+ const recaptchaRef = useRef ( ) ;
24
+
25
+ const [ settingsdata , setData ] = useState ( null ) ;
26
+
27
+ useEffect ( ( ) => {
28
+ fetch ( '/files/settings.json' )
29
+ . then ( ( response ) => response . json ( ) )
30
+ . then ( ( result ) => setData ( result ) )
31
+ . catch ( ( error ) => console . error ( 'Error fetching recaptcha site key:' , error ) ) ;
32
+ } , [ ] ) ;
33
+
22
34
// Helper function to jump to the first form error.
23
35
function focusOnFirstError ( ) {
24
36
const fieldErrors = document . getElementsByClassName ( 'usa-input-error' ) ;
@@ -62,6 +74,13 @@ function FoiaRequestForm({
62
74
}
63
75
64
76
function onFormSubmit ( { formData : data } ) {
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 ;
83
+
65
84
// Merge the sections into a single payload
66
85
const payload = rf . mergeSectionFormData ( data ) ;
67
86
// Transform file fields to attachments
@@ -149,12 +168,16 @@ function FoiaRequestForm({
149
168
/>
150
169
)
151
170
: (
152
- < button
153
- className = "usa-button usa-button-big usa-button-primary-alt"
154
- type = "submit"
155
- >
156
- Submit request
157
- </ button >
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 >
158
181
) }
159
182
{ submissionResult . errorMessage
160
183
&& (
0 commit comments