|
1 | | -import React, { useState } 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 | | -const GoogleReCaptchaWidget = (props) => { |
6 | | - const { captcha_props, GoogleReCaptcha: recaptchalib, id, onChange } = props; |
| 5 | +const ReCaptchaComponent = (props) => { |
| 6 | + const { GoogleReCaptcha: recaptchalib, id, onChange } = props; |
| 7 | + |
| 8 | + const { useGoogleReCaptcha } = recaptchalib; |
| 9 | + |
| 10 | + const { executeRecaptcha } = useGoogleReCaptcha(); |
| 11 | + |
| 12 | + const handleReCaptchaVerify = useCallback(async () => { |
| 13 | + if (!executeRecaptcha) { |
| 14 | + return; |
| 15 | + } |
| 16 | + |
| 17 | + const token = await executeRecaptcha(); |
| 18 | + onChange(id, token); |
| 19 | + }, [executeRecaptcha]); |
7 | 20 |
|
8 | | - const { GoogleReCaptchaProvider, GoogleReCaptcha } = recaptchalib; |
| 21 | + useEffect(() => { |
| 22 | + handleReCaptchaVerify(); |
| 23 | + }, [handleReCaptchaVerify]); |
| 24 | + |
| 25 | + return null; |
| 26 | +}; |
| 27 | + |
| 28 | +const GoogleReCaptchaWidget = (props) => { |
9 | 29 | const intl = useIntl(); |
10 | | - const [submitted, setSubmitted] = useState(false); |
| 30 | + |
| 31 | + const { captcha_props, GoogleReCaptcha: recaptchalib } = props; |
| 32 | + const { GoogleReCaptchaProvider } = recaptchalib; |
11 | 33 |
|
12 | 34 | return ( |
13 | | - !submitted && ( |
14 | | - <GoogleReCaptchaProvider |
15 | | - reCaptchaKey={captcha_props.public_key} |
16 | | - language={intl.locale ?? 'en'} |
17 | | - > |
18 | | - <GoogleReCaptcha |
19 | | - onVerify={(token) => { |
20 | | - setSubmitted(true); |
21 | | - onChange(id, token); |
22 | | - }} |
23 | | - /> |
24 | | - </GoogleReCaptchaProvider> |
25 | | - ) |
| 35 | + <GoogleReCaptchaProvider |
| 36 | + reCaptchaKey={captcha_props.public_key} |
| 37 | + language={intl.locale ?? 'en'} |
| 38 | + > |
| 39 | + <ReCaptchaComponent {...props} /> |
| 40 | + </GoogleReCaptchaProvider> |
26 | 41 | ); |
27 | 42 | }; |
28 | 43 |
|
|
0 commit comments