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-09-02 08:21:46 +00:00
|
|
|
import { CartCheckoutFeedbackPrompt } from '@woocommerce/editor-components/feedback-prompt';
|
2020-03-17 10:30:52 +00:00
|
|
|
import {
|
2021-10-25 14:46:34 +00:00
|
|
|
useBlockProps,
|
|
|
|
InnerBlocks,
|
|
|
|
InspectorControls,
|
|
|
|
BlockControls,
|
|
|
|
} from '@wordpress/block-editor';
|
|
|
|
import { PanelBody, ToggleControl, Notice } from '@wordpress/components';
|
2021-03-19 10:05:42 +00:00
|
|
|
import { CartCheckoutCompatibilityNotice } from '@woocommerce/editor-components/compatibility-notices';
|
2021-04-22 11:37:27 +00:00
|
|
|
import { CART_PAGE_ID } from '@woocommerce/block-settings';
|
2020-03-06 11:43:40 +00:00
|
|
|
import BlockErrorBoundary from '@woocommerce/base-components/block-error-boundary';
|
2020-04-01 09:27:53 +00:00
|
|
|
import {
|
|
|
|
EditorProvider,
|
|
|
|
useEditorContext,
|
|
|
|
CartProvider,
|
|
|
|
} from '@woocommerce/base-context';
|
2021-10-25 14:46:34 +00:00
|
|
|
import { createInterpolateElement } from '@wordpress/element';
|
|
|
|
import { getAdminLink } from '@woocommerce/settings';
|
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-04-07 13:47:58 +00:00
|
|
|
} from '../cart-checkout-shared';
|
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
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2020-03-16 16:38:24 +00:00
|
|
|
const BlockSettings = ( { attributes, setAttributes } ) => {
|
2021-10-25 14:46:34 +00:00
|
|
|
const { hasDarkControls } = attributes;
|
2020-03-17 10:30:52 +00:00
|
|
|
const { currentPostId } = useEditorContext();
|
2020-03-16 16:38:24 +00:00
|
|
|
return (
|
2020-02-19 16:33:10 +00:00
|
|
|
<InspectorControls>
|
2020-03-17 10:30:52 +00:00
|
|
|
{ currentPostId !== CART_PAGE_ID && (
|
|
|
|
<Notice
|
|
|
|
className="wc-block-cart__page-notice"
|
|
|
|
isDismissible={ false }
|
|
|
|
status="warning"
|
|
|
|
>
|
2020-09-26 13:51:21 +00:00
|
|
|
{ createInterpolateElement(
|
2020-03-17 10:30:52 +00:00
|
|
|
__(
|
|
|
|
'If you would like to use this block as your default cart you must update your <a>page settings in WooCommerce</a>.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
{
|
|
|
|
a: (
|
|
|
|
// eslint-disable-next-line jsx-a11y/anchor-has-content
|
|
|
|
<a
|
|
|
|
href={ getAdminLink(
|
|
|
|
'admin.php?page=wc-settings&tab=advanced'
|
|
|
|
) }
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
/>
|
|
|
|
),
|
|
|
|
}
|
|
|
|
) }
|
|
|
|
</Notice>
|
|
|
|
) }
|
2020-08-14 11:08:16 +00:00
|
|
|
<PanelBody title={ __( 'Style', 'woo-gutenberg-products-block' ) }>
|
|
|
|
<ToggleControl
|
|
|
|
label={ __(
|
|
|
|
'Dark mode inputs',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
help={ __(
|
|
|
|
'Inputs styled specifically for use on dark background colors.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
checked={ hasDarkControls }
|
|
|
|
onChange={ () =>
|
|
|
|
setAttributes( {
|
|
|
|
hasDarkControls: ! hasDarkControls,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</PanelBody>
|
2020-08-09 20:18:36 +00:00
|
|
|
<CartCheckoutFeedbackPrompt />
|
2020-02-19 16:33:10 +00:00
|
|
|
</InspectorControls>
|
|
|
|
);
|
2020-03-16 16:38:24 +00:00
|
|
|
};
|
2020-02-19 16:33: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,
|
|
|
|
} );
|
2019-12-05 21:08:48 +00:00
|
|
|
return (
|
2021-10-25 14:46:34 +00:00
|
|
|
<div { ...blockProps }>
|
|
|
|
<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 } }
|
|
|
|
>
|
|
|
|
<BlockSettings
|
|
|
|
attributes={ attributes }
|
|
|
|
setAttributes={ setAttributes }
|
|
|
|
/>
|
|
|
|
<BlockControls __experimentalShareWithChildBlocks>
|
2021-10-27 15:41:57 +00:00
|
|
|
{ ViewSwitcherComponent }
|
2021-10-25 14:46:34 +00:00
|
|
|
</BlockControls>
|
|
|
|
<CartBlockContext.Provider
|
|
|
|
value={ {
|
|
|
|
hasDarkControls,
|
|
|
|
} }
|
|
|
|
>
|
|
|
|
<CartProvider>
|
|
|
|
<InnerBlocks
|
|
|
|
allowedBlocks={ ALLOWED_BLOCKS }
|
|
|
|
template={ defaultTemplate }
|
|
|
|
templateLock={ false }
|
|
|
|
/>
|
|
|
|
</CartProvider>
|
|
|
|
</CartBlockContext.Provider>
|
|
|
|
</EditorProvider>
|
|
|
|
</BlockErrorBoundary>
|
2021-03-19 10:05:42 +00:00
|
|
|
<CartCheckoutCompatibilityNotice blockName="cart" />
|
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
|
|
|
};
|