2021-10-25 14:46:34 +00:00
|
|
|
/* tslint:disable */
|
2019-12-03 13:57:56 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-08-13 13:24:51 +00:00
|
|
|
import classnames from 'classnames';
|
2019-12-16 14:59:16 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2020-03-17 10:30:52 +00:00
|
|
|
import {
|
2021-10-25 14:46:34 +00:00
|
|
|
useBlockProps,
|
|
|
|
InnerBlocks,
|
2022-08-12 14:23:08 +00:00
|
|
|
InspectorControls,
|
2021-10-25 14:46:34 +00:00
|
|
|
} from '@wordpress/block-editor';
|
2020-03-06 11:43:40 +00:00
|
|
|
import BlockErrorBoundary from '@woocommerce/base-components/block-error-boundary';
|
2022-08-12 14:23:08 +00:00
|
|
|
import { EditorProvider, CartProvider } from '@woocommerce/base-context';
|
2020-08-13 13:24:51 +00:00
|
|
|
import { previewCart } from '@woocommerce/resource-previews';
|
2023-01-06 14:53:53 +00:00
|
|
|
import { SlotFillProvider } from '@woocommerce/blocks-checkout';
|
2020-04-08 15:03:39 +00:00
|
|
|
|
2019-12-05 21:08:48 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2022-04-01 13:45:18 +00:00
|
|
|
import './inner-blocks';
|
2020-03-17 10:30:52 +00:00
|
|
|
import './editor.scss';
|
2021-10-25 14:46:34 +00:00
|
|
|
import {
|
|
|
|
addClassToBody,
|
|
|
|
useBlockPropsWithLocking,
|
2022-08-12 14:23:08 +00:00
|
|
|
BlockSettings,
|
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';
|
2022-12-22 14:35:24 +00:00
|
|
|
import '../cart-checkout-shared/view-switcher';
|
2021-10-25 14:46:34 +00:00
|
|
|
import { CartBlockContext } from './context';
|
|
|
|
|
|
|
|
// This is adds a class to body to signal if the selected block is locked
|
|
|
|
addClassToBody();
|
|
|
|
|
|
|
|
// Array of allowed block names.
|
|
|
|
const ALLOWED_BLOCKS = [
|
|
|
|
'woocommerce/filled-cart-block',
|
|
|
|
'woocommerce/empty-cart-block',
|
|
|
|
];
|
2019-12-03 13:57:56 +00:00
|
|
|
|
2023-03-30 14:15:00 +00:00
|
|
|
export const Edit = ( { className, attributes, setAttributes } ) => {
|
|
|
|
const { hasDarkControls, currentView, isPreview = false } = attributes;
|
2021-10-25 14:46:34 +00:00
|
|
|
const defaultTemplate = [
|
|
|
|
[ 'woocommerce/filled-cart-block', {}, [] ],
|
|
|
|
[ 'woocommerce/empty-cart-block', {}, [] ],
|
|
|
|
];
|
|
|
|
const blockProps = useBlockPropsWithLocking( {
|
|
|
|
className: classnames( className, 'wp-block-woocommerce-cart', {
|
2023-03-30 14:15:00 +00:00
|
|
|
'is-editor-preview': isPreview,
|
2021-10-25 14:46:34 +00:00
|
|
|
} ),
|
|
|
|
} );
|
2022-08-12 14:23:08 +00:00
|
|
|
|
2019-12-05 21:08:48 +00:00
|
|
|
return (
|
2021-10-25 14:46:34 +00:00
|
|
|
<div { ...blockProps }>
|
2022-08-12 14:23:08 +00:00
|
|
|
<InspectorControls>
|
|
|
|
<BlockSettings
|
|
|
|
attributes={ attributes }
|
|
|
|
setAttributes={ setAttributes }
|
|
|
|
/>
|
|
|
|
</InspectorControls>
|
2021-10-25 14:46:34 +00:00
|
|
|
<BlockErrorBoundary
|
|
|
|
header={ __(
|
|
|
|
'Cart Block Error',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
text={ __(
|
|
|
|
'There was an error whilst rendering the cart block. If this problem continues, try re-creating the block.',
|
|
|
|
'woo-gutenberg-products-block'
|
2020-01-16 14:50:48 +00:00
|
|
|
) }
|
2021-10-25 14:46:34 +00:00
|
|
|
showErrorMessage={ true }
|
|
|
|
errorMessagePrefix={ __(
|
|
|
|
'Error message:',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
<EditorProvider
|
|
|
|
previewData={ { previewCart } }
|
2022-12-22 14:35:24 +00:00
|
|
|
currentView={ currentView }
|
2023-03-30 14:15:00 +00:00
|
|
|
isPreview={ isPreview }
|
2021-10-25 14:46:34 +00:00
|
|
|
>
|
|
|
|
<CartBlockContext.Provider
|
|
|
|
value={ {
|
|
|
|
hasDarkControls,
|
|
|
|
} }
|
|
|
|
>
|
2023-01-06 14:53:53 +00:00
|
|
|
<SlotFillProvider>
|
|
|
|
<CartProvider>
|
|
|
|
<InnerBlocks
|
|
|
|
allowedBlocks={ ALLOWED_BLOCKS }
|
|
|
|
template={ defaultTemplate }
|
2023-03-30 14:15:00 +00:00
|
|
|
templateLock="insert"
|
2023-01-06 14:53:53 +00:00
|
|
|
/>
|
|
|
|
</CartProvider>
|
|
|
|
</SlotFillProvider>
|
2021-10-25 14:46:34 +00:00
|
|
|
</CartBlockContext.Provider>
|
|
|
|
</EditorProvider>
|
|
|
|
</BlockErrorBoundary>
|
2019-12-10 15:41:57 +00:00
|
|
|
</div>
|
2019-12-05 21:08:48 +00:00
|
|
|
);
|
2019-12-03 13:57:56 +00:00
|
|
|
};
|
|
|
|
|
2021-10-25 14:46:34 +00:00
|
|
|
export const Save = () => {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
{ ...useBlockProps.save( {
|
|
|
|
className: 'is-loading',
|
|
|
|
} ) }
|
|
|
|
>
|
|
|
|
<InnerBlocks.Content />
|
|
|
|
</div>
|
|
|
|
);
|
2019-12-10 15:41:57 +00:00
|
|
|
};
|