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';
|
2021-10-15 09:48:57 +00:00
|
|
|
import type { TemplateArray } from '@wordpress/blocks';
|
2022-08-12 14:23:08 +00:00
|
|
|
import { CartCheckoutFeedbackPrompt } from '@woocommerce/editor-components/feedback-prompt';
|
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,
|
|
|
|
} = 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>
|
2022-08-12 14:23:08 +00:00
|
|
|
<CartCheckoutFeedbackPrompt />
|
2021-07-22 11:03:00 +00:00
|
|
|
</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
|
|
|
|
previewData={ { previewCart, previewSavedPaymentMethods } }
|
|
|
|
>
|
2021-09-16 12:16:21 +00:00
|
|
|
<CheckoutProvider>
|
2021-11-09 13:18:55 +00:00
|
|
|
<SidebarLayout
|
|
|
|
className={ classnames( 'wc-block-checkout', {
|
|
|
|
'has-dark-controls': attributes.hasDarkControls,
|
|
|
|
} ) }
|
|
|
|
>
|
|
|
|
<CheckoutBlockControlsContext.Provider
|
2022-12-15 11:13:28 +00:00
|
|
|
value={ { addressFieldControls } }
|
2021-07-22 11:03:00 +00:00
|
|
|
>
|
2021-11-09 13:18:55 +00:00
|
|
|
<CheckoutBlockContext.Provider
|
2021-07-22 11:03:00 +00:00
|
|
|
value={ {
|
2021-11-09 13:18:55 +00:00
|
|
|
showCompanyField,
|
|
|
|
requireCompanyField,
|
|
|
|
showApartmentField,
|
|
|
|
showPhoneField,
|
|
|
|
requirePhoneField,
|
|
|
|
showOrderNotes,
|
|
|
|
showPolicyLinks,
|
|
|
|
showReturnToCart,
|
|
|
|
cartPageId,
|
|
|
|
showRateAfterTaxName,
|
2021-07-22 11:03:00 +00:00
|
|
|
} }
|
|
|
|
>
|
2021-11-09 13:18:55 +00:00
|
|
|
<InnerBlocks
|
|
|
|
allowedBlocks={ ALLOWED_BLOCKS }
|
|
|
|
template={ defaultTemplate }
|
|
|
|
templateLock="insert"
|
|
|
|
/>
|
|
|
|
</CheckoutBlockContext.Provider>
|
|
|
|
</CheckoutBlockControlsContext.Provider>
|
|
|
|
</SidebarLayout>
|
2021-09-16 12:16:21 +00:00
|
|
|
</CheckoutProvider>
|
|
|
|
</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>
|
|
|
|
);
|
|
|
|
};
|