2021-10-25 14:46:34 +00:00
|
|
|
/* tslint:disable */
|
2019-12-03 13:57:56 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2024-05-31 03:49:36 +00:00
|
|
|
import clsx from 'clsx';
|
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';
|
2023-10-19 15:43:43 +00:00
|
|
|
import { useEffect, useRef } from '@wordpress/element';
|
|
|
|
import { getQueryArg } from '@wordpress/url';
|
|
|
|
import { dispatch, select } from '@wordpress/data';
|
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-10-19 15:43:43 +00:00
|
|
|
export const Edit = ( { clientId, className, attributes, setAttributes } ) => {
|
2023-03-30 14:15:00 +00:00
|
|
|
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( {
|
2024-05-31 03:49:36 +00:00
|
|
|
className: clsx( 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
|
|
|
|
2023-10-19 15:43:43 +00:00
|
|
|
// This focuses on the block when a certain query param is found. This is used on the link from the task list.
|
|
|
|
const focus = useRef( getQueryArg( window.location.href, 'focus' ) );
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
if (
|
|
|
|
focus.current === 'cart' &&
|
|
|
|
! select( 'core/block-editor' ).hasSelectedBlock()
|
|
|
|
) {
|
|
|
|
dispatch( 'core/block-editor' ).selectBlock( clientId );
|
|
|
|
dispatch( 'core/interface' ).enableComplementaryArea(
|
|
|
|
'core/edit-site',
|
|
|
|
'edit-site/block-inspector'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}, [ clientId ] );
|
|
|
|
|
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
|
2023-12-12 23:05:20 +00:00
|
|
|
header={ __( 'Cart Block Error', 'woocommerce' ) }
|
2021-10-25 14:46:34 +00:00
|
|
|
text={ __(
|
|
|
|
'There was an error whilst rendering the cart block. If this problem continues, try re-creating the block.',
|
2023-12-09 13:44:54 +00:00
|
|
|
'woocommerce'
|
2020-01-16 14:50:48 +00:00
|
|
|
) }
|
2021-10-25 14:46:34 +00:00
|
|
|
showErrorMessage={ true }
|
2023-12-12 23:05:20 +00:00
|
|
|
errorMessagePrefix={ __( 'Error message:', 'woocommerce' ) }
|
2021-10-25 14:46:34 +00:00
|
|
|
>
|
|
|
|
<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
|
|
|
};
|