|
1 | | -import React, { useCallback, useEffect, useRef } from 'react'; |
| 1 | +import React, { useCallback, useEffect } from 'react'; |
2 | 2 | import { injectLazyLibs } from '@plone/volto/helpers/Loadable/Loadable'; |
3 | 3 | import { useIntl } from 'react-intl'; |
4 | 4 |
|
5 | 5 | const ReCaptchaComponent = (props) => { |
6 | 6 | const { GoogleReCaptcha: recaptchalib, id, onChange } = props; |
7 | | - const { useGoogleReCaptcha } = recaptchalib || {}; |
| 7 | + const { useGoogleReCaptcha } = recaptchalib; |
| 8 | + const { executeRecaptcha } = useGoogleReCaptcha(); |
8 | 9 |
|
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 |
18 | 10 | 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) { |
24 | 12 | return; |
25 | 13 | } |
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]); |
52 | 16 |
|
53 | 17 | 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); |
65 | 20 | }, [handleReCaptchaVerify]); |
66 | 21 |
|
67 | 22 | return null; |
|
0 commit comments