| 1 |
/* eslint-disable camelcase, react-hooks/rules-of-hooks */ |
| 2 |
|
| 3 |
( function ( wp, wc ) { |
| 4 |
'use strict'; |
| 5 |
|
| 6 |
if ( ! wp || ! wc ) { |
| 7 |
return; |
| 8 |
} |
| 9 |
|
| 10 |
const el = wp.element.createElement; |
| 11 |
const { withInstanceId } = wp.compose; |
| 12 |
const { useDispatch, useSelect } = wp.data; |
| 13 |
const { useEffect, useState } = wp.element; |
| 14 |
const { getSetting } = wc.wcSettings; |
| 15 |
const { VALIDATION_STORE_KEY } = wc.wcBlocksData; |
| 16 |
const { CheckboxControl, registerCheckoutBlock } = wc.blocksCheckout; |
| 17 |
|
| 18 |
registerCheckoutBlock( { |
| 19 |
metadata: { |
| 20 |
name: 'mypreview/woo-additional-terms', |
| 21 |
parent: [ 'woocommerce/checkout-fields-block' ], |
| 22 |
}, |
| 23 |
component: withInstanceId( ( { instanceId, checkoutExtensionData } ) => { |
| 24 |
const data = getSetting( '_woo_additional_terms_data', '' ); |
| 25 |
|
| 26 |
// Bail early if the checkbox label is empty. |
| 27 |
if ( ! data?.checkbox_label ) { |
| 28 |
return null; |
| 29 |
} |
| 30 |
|
| 31 |
const { checkbox_label, is_required, display_action, page_content, error_message } = data; |
| 32 |
const validationErrorId = `_woo_additional_terms_data_${ instanceId }`; |
| 33 |
const { setExtensionData } = checkoutExtensionData; |
| 34 |
const [ checked, setChecked ] = useState( false ); |
| 35 |
const { setValidationErrors, clearValidationError } = useDispatch( VALIDATION_STORE_KEY ); |
| 36 |
const error = useSelect( ( select ) => |
| 37 |
select( VALIDATION_STORE_KEY ).getValidationError( validationErrorId ) |
| 38 |
); |
| 39 |
const hasError = !! ( error?.message && ! error?.hidden ); |
| 40 |
|
| 41 |
useEffect( () => { |
| 42 |
setExtensionData( '_woo_additional_terms', 'wat_checkbox', checked ); |
| 43 |
|
| 44 |
if ( ! is_required ) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
if ( checked ) { |
| 49 |
clearValidationError( validationErrorId ); |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
setValidationErrors( { |
| 54 |
[ validationErrorId ]: { |
| 55 |
message: error_message, |
| 56 |
hidden: true, |
| 57 |
}, |
| 58 |
} ); |
| 59 |
|
| 60 |
return () => { |
| 61 |
clearValidationError( validationErrorId ); |
| 62 |
}; |
| 63 |
}, [ |
| 64 |
setExtensionData, |
| 65 |
checked, |
| 66 |
validationErrorId, |
| 67 |
clearValidationError, |
| 68 |
setValidationErrors, |
| 69 |
is_required, |
| 70 |
error_message, |
| 71 |
] ); |
| 72 |
|
| 73 |
return el( |
| 74 |
'div', |
| 75 |
{ |
| 76 |
className: 'woocommerce-terms-and-conditions-wrapper woo-additional-terms', |
| 77 |
}, |
| 78 |
el( 'div', { |
| 79 |
id: 'woo-additional-terms-content', |
| 80 |
className: `woo-additional-terms__content woo-additional-terms__content--${ display_action }`, |
| 81 |
dangerouslySetInnerHTML: { |
| 82 |
__html: page_content, |
| 83 |
}, |
| 84 |
} ), |
| 85 |
el( |
| 86 |
CheckboxControl, |
| 87 |
{ |
| 88 |
checked, |
| 89 |
hasError, |
| 90 |
id: '_woo_additional_terms', |
| 91 |
name: '_woo_additional_terms', |
| 92 |
onChange: () => setChecked( ( value ) => ! value ), |
| 93 |
}, |
| 94 |
el( 'span', { |
| 95 |
dangerouslySetInnerHTML: { |
| 96 |
__html: checkbox_label, |
| 97 |
}, |
| 98 |
} ) |
| 99 |
) |
| 100 |
); |
| 101 |
} ), |
| 102 |
} ); |
| 103 |
} )( window.wp, window.wc ); |
| 104 |
|