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,
|
|
|
|
BlockControls,
|
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';
|
2022-02-01 16:54:38 +00:00
|
|
|
import { filledCart, removeCart } from '@woocommerce/icons';
|
|
|
|
import { Icon } from '@wordpress/icons';
|
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,
|
|
|
|
useViewSwitcher,
|
|
|
|
useBlockPropsWithLocking,
|
|
|
|
useForcedLayout,
|
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';
|
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
|
|
|
|
2021-12-14 00:46:10 +00:00
|
|
|
const views = [
|
|
|
|
{
|
|
|
|
view: 'woocommerce/filled-cart-block',
|
|
|
|
label: __( 'Filled Cart', 'woo-gutenberg-products-block' ),
|
2022-02-01 16:54:38 +00:00
|
|
|
icon: <Icon icon={ filledCart } />,
|
2021-12-14 00:46:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
view: 'woocommerce/empty-cart-block',
|
|
|
|
label: __( 'Empty Cart', 'woo-gutenberg-products-block' ),
|
2022-02-01 16:54:38 +00:00
|
|
|
icon: <Icon icon={ removeCart } />,
|
2021-12-14 00:46:10 +00:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2021-10-25 14:46:34 +00:00
|
|
|
export const Edit = ( { className, attributes, setAttributes, clientId } ) => {
|
|
|
|
const { hasDarkControls } = attributes;
|
|
|
|
const { currentView, component: ViewSwitcherComponent } = useViewSwitcher(
|
|
|
|
clientId,
|
2021-12-14 00:46:10 +00:00
|
|
|
views
|
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', {
|
|
|
|
'is-editor-preview': attributes.isPreview,
|
|
|
|
} ),
|
|
|
|
} );
|
|
|
|
useForcedLayout( {
|
|
|
|
clientId,
|
|
|
|
registeredBlocks: ALLOWED_BLOCKS,
|
|
|
|
defaultTemplate,
|
|
|
|
} );
|
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
|
|
|
|
currentView={ currentView }
|
|
|
|
previewData={ { previewCart } }
|
|
|
|
>
|
2022-09-16 06:02:46 +00:00
|
|
|
<BlockControls>{ ViewSwitcherComponent }</BlockControls>
|
2021-10-25 14:46:34 +00:00
|
|
|
<CartBlockContext.Provider
|
|
|
|
value={ {
|
|
|
|
hasDarkControls,
|
|
|
|
} }
|
|
|
|
>
|
|
|
|
<CartProvider>
|
|
|
|
<InnerBlocks
|
|
|
|
allowedBlocks={ ALLOWED_BLOCKS }
|
|
|
|
template={ defaultTemplate }
|
|
|
|
templateLock={ false }
|
|
|
|
/>
|
|
|
|
</CartProvider>
|
|
|
|
</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
|
|
|
};
|