2021-07-22 11:03:00 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import {
|
|
|
|
useBlockProps,
|
|
|
|
RichText,
|
|
|
|
InspectorControls,
|
|
|
|
} from '@wordpress/block-editor';
|
2021-11-09 16:47:46 +00:00
|
|
|
import { CheckboxControl } from '@woocommerce/blocks-checkout';
|
2021-07-22 11:03:00 +00:00
|
|
|
import { PanelBody, ToggleControl, Notice } from '@wordpress/components';
|
2021-09-28 15:01:48 +00:00
|
|
|
import { PRIVACY_URL, TERMS_URL } from '@woocommerce/block-settings';
|
|
|
|
import { ADMIN_URL } from '@woocommerce/settings';
|
|
|
|
import { Icon, external } from '@wordpress/icons';
|
2021-07-22 11:03:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './editor.scss';
|
|
|
|
import { termsConsentDefaultText, termsCheckboxDefaultText } from './constants';
|
|
|
|
|
|
|
|
export const Edit = ( {
|
|
|
|
attributes: { checkbox, text },
|
|
|
|
setAttributes,
|
|
|
|
}: {
|
|
|
|
attributes: { text: string; checkbox: boolean };
|
|
|
|
setAttributes: ( attributes: Record< string, unknown > ) => void;
|
|
|
|
} ): JSX.Element => {
|
2021-09-24 14:59:41 +00:00
|
|
|
const blockProps = useBlockProps();
|
2021-07-22 11:03:00 +00:00
|
|
|
|
2021-09-28 15:01:48 +00:00
|
|
|
const defaultText = checkbox
|
|
|
|
? termsCheckboxDefaultText
|
|
|
|
: termsConsentDefaultText;
|
|
|
|
|
|
|
|
const currentText = text || defaultText;
|
2021-07-22 11:03:00 +00:00
|
|
|
return (
|
2021-09-24 14:59:41 +00:00
|
|
|
<div { ...blockProps }>
|
2021-07-22 11:03:00 +00:00
|
|
|
<InspectorControls>
|
|
|
|
<PanelBody
|
|
|
|
title={ __(
|
|
|
|
'Display options',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
<ToggleControl
|
|
|
|
label={ __(
|
|
|
|
'Require checkbox',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
checked={ checkbox }
|
|
|
|
onChange={ () =>
|
|
|
|
setAttributes( {
|
|
|
|
checkbox: ! checkbox,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</PanelBody>
|
|
|
|
</InspectorControls>
|
|
|
|
<div className="wc-block-checkout__terms">
|
|
|
|
{ checkbox ? (
|
|
|
|
<>
|
|
|
|
<CheckboxControl
|
|
|
|
id="terms-condition"
|
|
|
|
checked={ false }
|
|
|
|
/>
|
|
|
|
<RichText
|
|
|
|
value={ currentText }
|
|
|
|
onChange={ ( value ) =>
|
|
|
|
setAttributes( { text: value } )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<RichText
|
|
|
|
tagName="span"
|
2021-09-28 15:01:48 +00:00
|
|
|
value={ currentText }
|
2021-07-22 11:03:00 +00:00
|
|
|
onChange={ ( value ) =>
|
|
|
|
setAttributes( { text: value } )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</div>
|
2021-09-28 15:01:48 +00:00
|
|
|
{ /* Show this notice if a terms page or a privacy page is not setup. */ }
|
|
|
|
{ ( ! TERMS_URL || ! PRIVACY_URL ) && (
|
2021-07-22 11:03:00 +00:00
|
|
|
<Notice
|
|
|
|
className="wc-block-checkout__terms_notice"
|
|
|
|
status="warning"
|
|
|
|
isDismissible={ false }
|
2021-09-28 15:01:48 +00:00
|
|
|
actions={ [
|
|
|
|
! TERMS_URL && {
|
|
|
|
className: 'wc-block-checkout__terms_notice-button',
|
|
|
|
label: (
|
|
|
|
<>
|
|
|
|
{ __(
|
|
|
|
'Setup a Terms and Conditions page',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
<Icon
|
|
|
|
icon={ external }
|
|
|
|
size={ 16 }
|
|
|
|
className="wc-block-checkout__terms_notice-button__icon"
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
),
|
|
|
|
|
|
|
|
onClick: () =>
|
|
|
|
window.open(
|
|
|
|
`${ ADMIN_URL }admin.php?page=wc-settings&tab=advanced`,
|
|
|
|
'_blank'
|
|
|
|
),
|
|
|
|
},
|
|
|
|
! PRIVACY_URL && {
|
|
|
|
className: 'wc-block-checkout__terms_notice-button',
|
|
|
|
label: (
|
|
|
|
<>
|
|
|
|
{ __(
|
|
|
|
'Setup a Privacy Policy page',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
<Icon
|
|
|
|
size={ 16 }
|
|
|
|
icon={ external }
|
|
|
|
className="wc-block-checkout__terms_notice-button__icon"
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
),
|
|
|
|
onClick: () =>
|
|
|
|
window.open(
|
|
|
|
`${ ADMIN_URL }options-privacy.php`,
|
|
|
|
'_blank'
|
|
|
|
),
|
|
|
|
},
|
|
|
|
].filter( Boolean ) }
|
2021-07-22 11:03:00 +00:00
|
|
|
>
|
|
|
|
<p>
|
|
|
|
{ __(
|
2021-09-28 15:01:48 +00:00
|
|
|
"You don't seem to have a Terms and Conditions and/or a Privacy Policy pages setup.",
|
2021-07-22 11:03:00 +00:00
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</p>
|
|
|
|
</Notice>
|
|
|
|
) }
|
2021-09-28 15:01:48 +00:00
|
|
|
{ /* Show this notice if we have both a terms and privacy pages, but they're not present in the text. */ }
|
|
|
|
{ TERMS_URL &&
|
|
|
|
PRIVACY_URL &&
|
|
|
|
! (
|
|
|
|
currentText.includes( TERMS_URL ) &&
|
|
|
|
currentText.includes( PRIVACY_URL )
|
|
|
|
) && (
|
|
|
|
<Notice
|
|
|
|
className="wc-block-checkout__terms_notice"
|
|
|
|
status="warning"
|
|
|
|
isDismissible={ false }
|
|
|
|
actions={
|
|
|
|
termsConsentDefaultText !== text
|
|
|
|
? [
|
|
|
|
{
|
|
|
|
label: __(
|
|
|
|
'Restore default text',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
onClick: () =>
|
|
|
|
setAttributes( { text: '' } ),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
: []
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<p>
|
|
|
|
{ __(
|
|
|
|
'Ensure you add links to your policy pages in this section.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</p>
|
|
|
|
</Notice>
|
|
|
|
) }
|
2021-09-24 14:59:41 +00:00
|
|
|
</div>
|
2021-07-22 11:03:00 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const Save = (): JSX.Element => {
|
|
|
|
return <div { ...useBlockProps.save() } />;
|
|
|
|
};
|