Skip to content

Commit 8dbb415

Browse files
authored
Merge pull request #2040 from rgristroph-usdoj/WS-50-add-recaptcha
WS-50: Add recaptcha to FOIA request form
2 parents b834505 + edcc518 commit 8dbb415

File tree

4 files changed

+148
-7
lines changed

4 files changed

+148
-7
lines changed

js/actions/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ export const requestActions = {
243243
submissionResult.errorMessage = 'Sorry, there was a problem with the information you provided, please check the form and correct any errors.';
244244
}
245245

246+
if (error.response && error.response.status === 401) {
247+
submissionResult.errorMessage = 'Sorry, there was a problem with captcha confirming you are human, please check the captcha and re-submit the form.';
248+
}
249+
246250
return Promise.resolve(submissionResult);
247251
})
248252
.then(requestActions.completeSubmitRequestForm);

js/components/foia_request_form.jsx

+30-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import React from 'react';
1+
import React, { useRef, useState, useEffect } from 'react';
22
import PropTypes from 'prop-types';
33
import Form from '@rjsf/core';
44
import validator from '@rjsf/validator-ajv8';
55
import { Map } from 'immutable';
66
import CustomFieldTemplate from 'components/request_custom_field_template';
77
import USWDSRadioWidget from 'components/uswds_radio_widget';
88
import USWDSCheckboxWidget from 'components/uswds_checkbox_widget';
9+
import ReCAPTCHA from 'react-google-recaptcha';
910
import { requestActions } from '../actions';
1011
import { SubmissionResult } from '../models';
1112
import CustomObjectFieldTemplate from './object_field_template';
@@ -19,6 +20,17 @@ import dispatcher from '../util/dispatcher';
1920
function FoiaRequestForm({
2021
formData, upload, onSubmit, requestForm, submissionResult,
2122
}) {
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+
2234
// Helper function to jump to the first form error.
2335
function focusOnFirstError() {
2436
const fieldErrors = document.getElementsByClassName('usa-input-error');
@@ -62,6 +74,13 @@ function FoiaRequestForm({
6274
}
6375

6476
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+
6584
// Merge the sections into a single payload
6685
const payload = rf.mergeSectionFormData(data);
6786
// Transform file fields to attachments
@@ -149,12 +168,16 @@ function FoiaRequestForm({
149168
/>
150169
)
151170
: (
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>
158181
)}
159182
{submissionResult.errorMessage
160183
&& (

package-lock.json

+112
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"prop-types": "15.x",
5353
"react": "16.x",
5454
"react-dom": "16.x",
55+
"react-google-recaptcha": "^3.1.0",
5556
"react-modal": "3.x",
5657
"react-router-dom": "5.x",
5758
"react-url-query": "1.x",
@@ -69,6 +70,7 @@
6970
"@babel/preset-env": "^7.25.4",
7071
"@babel/preset-react": "^7.24.7",
7172
"@cucumber/cucumber": "^9.1.0",
73+
"@types/react-google-recaptcha": "^2.1.9",
7274
"babel-loader": "9.x",
7375
"btoa": "^1.2.1",
7476
"buffer": "^6.0.3",

0 commit comments

Comments
 (0)