PluginProbe
Additional Terms Lite for WooCommerce / 1.7.3
Additional Terms Lite for WooCommerce v1.7.3
1.7.3 1.7.2.1 trunk 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.6.0 1.6.1 1.6.2 1.6.3 All 35 releases
woo-additional-terms / assets / js / checkout.js

checkout.js in Additional Terms Lite for WooCommerce 1.7.3, at assets/js/checkout.js

107 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 const { setExtensionData } = checkoutExtensionData;
26
27 // Bail early if the checkbox label is empty.
28 if ( ! data?.checkbox_label ) {
29 useEffect( () => {
30 setExtensionData( '_woo_additional_terms', 'data', '' );
31 }, [] );
32 return null;
33 }
34
35 const { checkbox_label, is_required, display_action, page_content, error_message } = data;
36 const validationErrorId = `_woo_additional_terms_data_${ instanceId }`;
37 const [ checked, setChecked ] = useState( false );
38 const { setValidationErrors, clearValidationError } = useDispatch( VALIDATION_STORE_KEY );
39 const error = useSelect( ( select ) =>
40 select( VALIDATION_STORE_KEY ).getValidationError( validationErrorId )
41 );
42 const hasError = !! ( error?.message && ! error?.hidden );
43
44 useEffect( () => {
45 setExtensionData( '_woo_additional_terms', 'data', checked ? 'yes' : 'no' );
46
47 if ( ! is_required ) {
48 return;
49 }
50
51 if ( checked ) {
52 clearValidationError( validationErrorId );
53 return;
54 }
55
56 setValidationErrors( {
57 [ validationErrorId ]: {
58 message: error_message,
59 hidden: true,
60 },
61 } );
62
63 return () => {
64 clearValidationError( validationErrorId );
65 };
66 }, [
67 setExtensionData,
68 checked,
69 validationErrorId,
70 clearValidationError,
71 setValidationErrors,
72 is_required,
73 error_message,
74 ] );
75
76 return el(
77 'div',
78 {
79 className: 'woocommerce-terms-and-conditions-wrapper woo-additional-terms',
80 },
81 el( 'div', {
82 id: 'woo-additional-terms-content',
83 className: `woo-additional-terms__content woo-additional-terms__content--${ display_action }`,
84 dangerouslySetInnerHTML: {
85 __html: page_content,
86 },
87 } ),
88 el(
89 CheckboxControl,
90 {
91 checked,
92 hasError,
93 id: '_woo_additional_terms',
94 name: '_woo_additional_terms',
95 onChange: () => setChecked( ( value ) => ! value ),
96 },
97 el( 'span', {
98 dangerouslySetInnerHTML: {
99 __html: checkbox_label,
100 },
101 } )
102 )
103 );
104 } ),
105 } );
106 } )( window.wp, window.wc );
107