2021-07-22 11:03:00 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-07-23 13:21:59 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import { useState, useEffect } from '@wordpress/element';
|
2021-11-09 16:47:46 +00:00
|
|
|
import { CheckboxControl } from '@woocommerce/blocks-checkout';
|
2021-07-23 13:21:59 +00:00
|
|
|
import { useCheckoutSubmit } from '@woocommerce/base-context/hooks';
|
2021-08-05 09:26:00 +00:00
|
|
|
import { withInstanceId } from '@wordpress/compose';
|
2021-09-09 15:35:53 +00:00
|
|
|
import type { ValidationData } from '@woocommerce/type-defs/contexts';
|
2021-07-22 11:03:00 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { termsConsentDefaultText, termsCheckboxDefaultText } from './constants';
|
|
|
|
import './style.scss';
|
|
|
|
|
|
|
|
const FrontendBlock = ( {
|
|
|
|
text,
|
|
|
|
checkbox,
|
2021-07-23 13:21:59 +00:00
|
|
|
instanceId,
|
2021-09-09 15:35:53 +00:00
|
|
|
validation,
|
2021-10-20 16:18:13 +00:00
|
|
|
className,
|
2021-07-22 11:03:00 +00:00
|
|
|
}: {
|
|
|
|
text: string;
|
|
|
|
checkbox: boolean;
|
2021-07-23 13:21:59 +00:00
|
|
|
instanceId: string;
|
2021-09-09 15:35:53 +00:00
|
|
|
validation: ValidationData;
|
2021-10-20 16:18:13 +00:00
|
|
|
className?: string;
|
2021-07-22 11:03:00 +00:00
|
|
|
} ): JSX.Element => {
|
|
|
|
const [ checked, setChecked ] = useState( false );
|
2021-07-23 13:21:59 +00:00
|
|
|
|
|
|
|
const { isDisabled } = useCheckoutSubmit();
|
2021-09-09 15:35:53 +00:00
|
|
|
|
|
|
|
const validationErrorId = 'terms-and-conditions-' + instanceId;
|
2022-06-15 09:56:52 +00:00
|
|
|
const { getValidationError, setValidationErrors, clearValidationError } =
|
|
|
|
validation;
|
2021-07-23 13:21:59 +00:00
|
|
|
|
|
|
|
const error = getValidationError( validationErrorId ) || {};
|
|
|
|
const hasError = error.message && ! error.hidden;
|
|
|
|
|
|
|
|
// Track validation errors for this input.
|
|
|
|
useEffect( () => {
|
|
|
|
if ( ! checkbox ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( checked ) {
|
|
|
|
clearValidationError( validationErrorId );
|
|
|
|
} else {
|
|
|
|
setValidationErrors( {
|
|
|
|
[ validationErrorId ]: {
|
|
|
|
message: __(
|
|
|
|
'Please read and accept the terms and conditions.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
hidden: true,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
return () => {
|
|
|
|
clearValidationError( validationErrorId );
|
|
|
|
};
|
|
|
|
}, [
|
|
|
|
checkbox,
|
|
|
|
checked,
|
|
|
|
validationErrorId,
|
|
|
|
clearValidationError,
|
|
|
|
setValidationErrors,
|
|
|
|
] );
|
|
|
|
|
2021-07-22 11:03:00 +00:00
|
|
|
return (
|
2021-07-23 13:21:59 +00:00
|
|
|
<div
|
2021-10-20 16:18:13 +00:00
|
|
|
className={ classnames(
|
|
|
|
'wc-block-checkout__terms',
|
|
|
|
{
|
|
|
|
'wc-block-checkout__terms--disabled': isDisabled,
|
|
|
|
},
|
|
|
|
className
|
|
|
|
) }
|
2021-07-23 13:21:59 +00:00
|
|
|
>
|
2021-07-22 11:03:00 +00:00
|
|
|
{ checkbox ? (
|
|
|
|
<>
|
|
|
|
<CheckboxControl
|
2021-07-23 13:21:59 +00:00
|
|
|
id="terms-and-conditions"
|
2021-07-22 11:03:00 +00:00
|
|
|
checked={ checked }
|
|
|
|
onChange={ () => setChecked( ( value ) => ! value ) }
|
2021-09-14 11:26:41 +00:00
|
|
|
hasError={ hasError }
|
2021-07-23 13:21:59 +00:00
|
|
|
disabled={ isDisabled }
|
2021-07-22 11:03:00 +00:00
|
|
|
>
|
|
|
|
<span
|
|
|
|
dangerouslySetInnerHTML={ {
|
|
|
|
__html: text || termsCheckboxDefaultText,
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
</CheckboxControl>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<span
|
|
|
|
dangerouslySetInnerHTML={ {
|
|
|
|
__html: text || termsConsentDefaultText,
|
|
|
|
} }
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-07-23 13:21:59 +00:00
|
|
|
export default withInstanceId( FrontendBlock );
|