File size: 960 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import config from '@automattic/calypso-config';
import { useDispatch } from '@wordpress/data';
import PropTypes from 'prop-types';
import { memo, useEffect } from 'react';
import { initGoogleRecaptcha } from 'calypso/lib/analytics/recaptcha';
import './style.scss';
import { CHECKOUT_STORE } from 'calypso/my-sites/checkout/src/lib/wpcom-store';

function Recaptcha( { badgePosition } ) {
	const { setRecaptchaClientId } = useDispatch( CHECKOUT_STORE ) ?? {};
	useEffect( () => {
		initGoogleRecaptcha( 'g-recaptcha', config( 'google_recaptcha_site_key' ) ).then(
			( clientId ) => {
				if ( clientId === null ) {
					return;
				}

				setRecaptchaClientId?.( parseInt( clientId ) );
			}
		);
	}, [ setRecaptchaClientId ] );

	return <div id="g-recaptcha" data-badge={ badgePosition }></div>;
}

Recaptcha.propTypes = {
	badgePosition: PropTypes.string,
};

Recaptcha.defaultProps = {
	badgePosition: 'bottomright',
};

export default memo( Recaptcha );