2021-07-22 11:03:00 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import {
|
|
|
|
InnerBlocks,
|
|
|
|
useBlockProps,
|
|
|
|
InspectorControls,
|
|
|
|
} from '@wordpress/block-editor';
|
|
|
|
import { SidebarLayout } from '@woocommerce/base-components/sidebar-layout';
|
2022-08-12 14:23:08 +00:00
|
|
|
import { CheckoutProvider, EditorProvider } from '@woocommerce/base-context';
|
2021-07-22 11:03:00 +00:00
|
|
|
import {
|
|
|
|
previewCart,
|
|
|
|
previewSavedPaymentMethods,
|
|
|
|
} from '@woocommerce/resource-previews';
|
|
|
|
import {
|
|
|
|
PanelBody,
|
|
|
|
ToggleControl,
|
2021-11-09 16:47:46 +00:00
|
|
|
CheckboxControl,
|
2021-07-22 11:03:00 +00:00
|
|
|
} from '@wordpress/components';
|
2023-01-06 14:53:53 +00:00
|
|
|
import { SlotFillProvider } from '@woocommerce/blocks-checkout';
|
2021-10-15 09:48:57 +00:00
|
|
|
import type { TemplateArray } from '@wordpress/blocks';
|
2021-07-22 11:03:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2022-04-01 13:45:18 +00:00
|
|
|
import './inner-blocks';
|
2021-07-22 11:03:00 +00:00
|
|
|
import './styles/editor.scss';
|
2022-04-05 10:14:32 +00:00
|
|
|
import {
|
|
|
|
addClassToBody,
|
2022-08-12 14:23:08 +00:00
|
|
|
BlockSettings,
|
2022-04-05 10:14:32 +00:00
|
|
|
useBlockPropsWithLocking,
|
2022-04-07 13:47:58 +00:00
|
|
|
} from '../cart-checkout-shared';
|
2022-08-16 10:51:26 +00:00
|
|
|
import '../cart-checkout-shared/sidebar-notices';
|
2021-07-22 11:03:00 +00:00
|
|
|
import { CheckoutBlockContext, CheckoutBlockControlsContext } from './context';
|
|
|
|
import type { Attributes } from './types';
|
|
|
|
|
|
|
|
// This is adds a class to body to signal if the selected block is locked
|
|
|
|
addClassToBody();
|
|
|
|
|
|
|
|
// Array of allowed block names.
|
|
|
|
const ALLOWED_BLOCKS: string[] = [
|
|
|
|
'woocommerce/checkout-fields-block',
|
|
|
|
'woocommerce/checkout-totals-block',
|
|
|
|
];
|
|
|
|
|
|
|
|
export const Edit = ( {
|
|
|
|
attributes,
|
|
|
|
setAttributes,
|
|
|
|
}: {
|
|
|
|
attributes: Attributes;
|
|
|
|
setAttributes: ( attributes: Record< string, unknown > ) => undefined;
|
|
|
|
} ): JSX.Element => {
|
|
|
|
const {
|
|
|
|
showCompanyField,
|
|
|
|
requireCompanyField,
|
|
|
|
showApartmentField,
|
|
|
|
showPhoneField,
|
|
|
|
requirePhoneField,
|
|
|
|
showOrderNotes,
|
|
|
|
showPolicyLinks,
|
|
|
|
showReturnToCart,
|
|
|
|
showRateAfterTaxName,
|
|
|
|
cartPageId,
|
2023-03-30 14:15:00 +00:00
|
|
|
isPreview = false,
|
2021-07-22 11:03:00 +00:00
|
|
|
} = attributes;
|
|
|
|
|
2021-10-15 09:48:57 +00:00
|
|
|
const defaultTemplate = [
|
|
|
|
[ 'woocommerce/checkout-fields-block', {}, [] ],
|
|
|
|
[ 'woocommerce/checkout-totals-block', {}, [] ],
|
|
|
|
] as TemplateArray;
|
2021-07-22 11:03:00 +00:00
|
|
|
|
|
|
|
const toggleAttribute = ( key: keyof Attributes ): void => {
|
|
|
|
const newAttributes = {} as Partial< Attributes >;
|
|
|
|
newAttributes[ key ] = ! ( attributes[ key ] as boolean );
|
|
|
|
setAttributes( newAttributes );
|
|
|
|
};
|
|
|
|
|
|
|
|
const addressFieldControls = (): JSX.Element => (
|
|
|
|
<InspectorControls>
|
|
|
|
<PanelBody
|
|
|
|
title={ __( 'Address Fields', 'woo-gutenberg-products-block' ) }
|
|
|
|
>
|
|
|
|
<p className="wc-block-checkout__controls-text">
|
|
|
|
{ __(
|
|
|
|
'Show or hide fields in the checkout address forms.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
</p>
|
|
|
|
<ToggleControl
|
|
|
|
label={ __( 'Company', 'woo-gutenberg-products-block' ) }
|
|
|
|
checked={ showCompanyField }
|
|
|
|
onChange={ () => toggleAttribute( 'showCompanyField' ) }
|
|
|
|
/>
|
|
|
|
{ showCompanyField && (
|
|
|
|
<CheckboxControl
|
|
|
|
label={ __(
|
|
|
|
'Require company name?',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
checked={ requireCompanyField }
|
|
|
|
onChange={ () =>
|
|
|
|
toggleAttribute( 'requireCompanyField' )
|
|
|
|
}
|
|
|
|
className="components-base-control--nested"
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
<ToggleControl
|
|
|
|
label={ __(
|
|
|
|
'Apartment, suite, etc.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
checked={ showApartmentField }
|
|
|
|
onChange={ () => toggleAttribute( 'showApartmentField' ) }
|
|
|
|
/>
|
|
|
|
<ToggleControl
|
|
|
|
label={ __( 'Phone', 'woo-gutenberg-products-block' ) }
|
|
|
|
checked={ showPhoneField }
|
|
|
|
onChange={ () => toggleAttribute( 'showPhoneField' ) }
|
|
|
|
/>
|
|
|
|
{ showPhoneField && (
|
|
|
|
<CheckboxControl
|
|
|
|
label={ __(
|
|
|
|
'Require phone number?',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
checked={ requirePhoneField }
|
|
|
|
onChange={ () =>
|
|
|
|
toggleAttribute( 'requirePhoneField' )
|
|
|
|
}
|
|
|
|
className="components-base-control--nested"
|
|
|
|
/>
|
|
|
|
) }
|
|
|
|
</PanelBody>
|
|
|
|
</InspectorControls>
|
|
|
|
);
|
2021-09-24 13:44:05 +00:00
|
|
|
const blockProps = useBlockPropsWithLocking();
|
2021-07-22 11:03:00 +00:00
|
|
|
return (
|
2021-09-24 13:44:05 +00:00
|
|
|
<div { ...blockProps }>
|
2022-08-12 14:23:08 +00:00
|
|
|
<InspectorControls>
|
2021-09-16 12:16:21 +00:00
|
|
|
<BlockSettings
|
|
|
|
attributes={ attributes }
|
|
|
|
setAttributes={ setAttributes }
|
|
|
|
/>
|
2022-08-12 14:23:08 +00:00
|
|
|
</InspectorControls>
|
|
|
|
<EditorProvider
|
2023-03-30 14:15:00 +00:00
|
|
|
isPreview={ isPreview }
|
2022-08-12 14:23:08 +00:00
|
|
|
previewData={ { previewCart, previewSavedPaymentMethods } }
|
|
|
|
>
|
2023-01-06 14:53:53 +00:00
|
|
|
<SlotFillProvider>
|
|
|
|
<CheckoutProvider>
|
|
|
|
<SidebarLayout
|
|
|
|
className={ classnames( 'wc-block-checkout', {
|
|
|
|
'has-dark-controls': attributes.hasDarkControls,
|
|
|
|
} ) }
|
2021-07-22 11:03:00 +00:00
|
|
|
>
|
2023-01-06 14:53:53 +00:00
|
|
|
<CheckoutBlockControlsContext.Provider
|
|
|
|
value={ { addressFieldControls } }
|
2021-07-22 11:03:00 +00:00
|
|
|
>
|
2023-01-06 14:53:53 +00:00
|
|
|
<CheckoutBlockContext.Provider
|
|
|
|
value={ {
|
|
|
|
showCompanyField,
|
|
|
|
requireCompanyField,
|
|
|
|
showApartmentField,
|
|
|
|
showPhoneField,
|
|
|
|
requirePhoneField,
|
|
|
|
showOrderNotes,
|
|
|
|
showPolicyLinks,
|
|
|
|
showReturnToCart,
|
|
|
|
cartPageId,
|
|
|
|
showRateAfterTaxName,
|
|
|
|
} }
|
|
|
|
>
|
|
|
|
<InnerBlocks
|
|
|
|
allowedBlocks={ ALLOWED_BLOCKS }
|
|
|
|
template={ defaultTemplate }
|
|
|
|
templateLock="insert"
|
|
|
|
/>
|
|
|
|
</CheckoutBlockContext.Provider>
|
|
|
|
</CheckoutBlockControlsContext.Provider>
|
|
|
|
</SidebarLayout>
|
|
|
|
</CheckoutProvider>
|
|
|
|
</SlotFillProvider>
|
2021-09-16 12:16:21 +00:00
|
|
|
</EditorProvider>
|
2021-09-24 13:44:05 +00:00
|
|
|
</div>
|
2021-07-22 11:03:00 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const Save = (): JSX.Element => {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
{ ...useBlockProps.save( {
|
|
|
|
className: 'wc-block-checkout is-loading',
|
|
|
|
} ) }
|
|
|
|
>
|
|
|
|
<InnerBlocks.Content />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|