Skip to content

Commit 1968a45

Browse files
committed
Refactor captcha widget.
1 parent fe21f63 commit 1968a45

File tree

2 files changed

+11
-70
lines changed

2 files changed

+11
-70
lines changed

frontend/packages/volto-form-block/src/components/ViewSchemaForm.jsx

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,7 @@ const messages = defineMessages({
5656
},
5757
});
5858

59-
const FormBlockView = ({
60-
data,
61-
id,
62-
properties,
63-
metadata,
64-
path,
65-
moment: momentlib,
66-
}) => {
59+
const FormBlockView = ({ data, id, path, moment: momentlib }) => {
6760
const dispatch = useDispatch();
6861
const intl = useIntl();
6962
const location = useLocation();
@@ -106,19 +99,14 @@ const FormBlockView = ({
10699

107100
const onCancel = () => {};
108101

109-
const onSubmit = (formData) => {
102+
const onSubmit = async (formData) => {
110103
let submitData = { ...formData };
111104
let captcha = {
112105
provider: data.captcha,
113-
token: submitData.captchaToken,
114106
};
115-
if (data.captcha === 'honeypot') {
116-
captcha.value = submitData['captchaWidget']?.value ?? '';
117-
delete submitData.captchaWidget;
118-
}
119107

120108
if (data.captcha === 'recaptcha') {
121-
captcha.token = submitData.captchaWidget;
109+
captcha.token = await submitData.captchaWidget();
122110
delete submitData.captchaWidget;
123111
}
124112

@@ -213,8 +201,6 @@ const FormBlockView = ({
213201
);
214202
}
215203

216-
const errEvent = new Event('formBlockSubmitError');
217-
document.dispatchEvent(errEvent);
218204
setSubmitPressed(false);
219205
});
220206
};

frontend/packages/volto-form-block/src/components/Widgets/GoogleReCaptchaWidget.jsx

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,22 @@
1-
import React, { useCallback, useEffect, useRef } from 'react';
1+
import React, { useCallback, useEffect } from 'react';
22
import { injectLazyLibs } from '@plone/volto/helpers/Loadable/Loadable';
33
import { useIntl } from 'react-intl';
44

55
const ReCaptchaComponent = (props) => {
66
const { GoogleReCaptcha: recaptchalib, id, onChange } = props;
7-
const { useGoogleReCaptcha } = recaptchalib || {};
7+
const { useGoogleReCaptcha } = recaptchalib;
8+
const { executeRecaptcha } = useGoogleReCaptcha();
89

9-
const executeRecaptchaRef = useRef(null);
10-
const onChangeRef = useRef(onChange);
11-
const idRef = useRef(id);
12-
// Keep track if the initial verification has been attempted/done
13-
const initialVerificationDoneRef = useRef(false);
14-
15-
const { executeRecaptcha } = useGoogleReCaptcha ? useGoogleReCaptcha() : {};
16-
17-
// Memoize the verification function
1810
const handleReCaptchaVerify = useCallback(async () => {
19-
const currentExecuteRecaptcha = executeRecaptchaRef.current;
20-
if (!currentExecuteRecaptcha) {
21-
console.warn(
22-
'Attempted to verify reCAPTCHA, but executeRecaptcha is not available.',
23-
);
11+
if (!executeRecaptcha) {
2412
return;
2513
}
26-
try {
27-
const token = await currentExecuteRecaptcha();
28-
if (token && onChangeRef.current && idRef.current) {
29-
onChangeRef.current(idRef.current, token);
30-
} else if (!token) {
31-
console.warn('executeRecaptcha ran but returned no token.');
32-
}
33-
} catch (error) {
34-
console.error('Error executing reCAPTCHA:', error);
35-
}
36-
}, []);
37-
38-
// Update refs for props
39-
useEffect(() => {
40-
onChangeRef.current = onChange;
41-
idRef.current = id;
42-
}, [onChange, id]);
43-
44-
useEffect(() => {
45-
executeRecaptchaRef.current = executeRecaptcha;
46-
47-
if (executeRecaptcha && !initialVerificationDoneRef.current) {
48-
initialVerificationDoneRef.current = true;
49-
handleReCaptchaVerify();
50-
}
51-
}, [executeRecaptcha, handleReCaptchaVerify]);
14+
return await executeRecaptcha();
15+
}, [executeRecaptcha]);
5216

5317
useEffect(() => {
54-
const handleError = (event) => {
55-
console.log(
56-
'formBlockSubmitError event received, triggering reCAPTCHA verification.',
57-
);
58-
handleReCaptchaVerify();
59-
};
60-
61-
document.addEventListener('formBlockSubmitError', handleError);
62-
return () => {
63-
document.removeEventListener('formBlockSubmitError', handleError);
64-
};
18+
handleReCaptchaVerify();
19+
onChange(id, handleReCaptchaVerify);
6520
}, [handleReCaptchaVerify]);
6621

6722
return null;

0 commit comments

Comments
 (0)