2021-09-21 10:18:27 +00:00
|
|
|
/* tslint:disable */
|
2021-09-17 15:29:58 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { CartCheckoutFeedbackPrompt } from '@woocommerce/editor-components/feedback-prompt';
|
2021-09-21 15:41:09 +00:00
|
|
|
import {
|
|
|
|
InnerBlocks,
|
|
|
|
useBlockProps,
|
|
|
|
InspectorControls,
|
|
|
|
} from '@wordpress/block-editor';
|
2021-09-21 10:18:27 +00:00
|
|
|
import { PanelBody, ToggleControl, Notice } from '@wordpress/components';
|
2021-09-17 15:29:58 +00:00
|
|
|
import { CartCheckoutCompatibilityNotice } from '@woocommerce/editor-components/compatibility-notices';
|
|
|
|
import { CART_PAGE_ID } from '@woocommerce/block-settings';
|
|
|
|
import BlockErrorBoundary from '@woocommerce/base-components/block-error-boundary';
|
|
|
|
import {
|
|
|
|
EditorProvider,
|
|
|
|
useEditorContext,
|
|
|
|
CartProvider,
|
|
|
|
} from '@woocommerce/base-context';
|
2021-09-21 12:20:42 +00:00
|
|
|
import { createInterpolateElement } from '@wordpress/element';
|
|
|
|
import { getAdminLink, getSetting } from '@woocommerce/settings';
|
2021-09-17 15:29:58 +00:00
|
|
|
import { previewCart } from '@woocommerce/resource-previews';
|
2021-09-22 15:00:19 +00:00
|
|
|
import { Icon, filledCart, removeCart } from '@woocommerce/icons';
|
2021-09-17 15:29:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './editor.scss';
|
2021-09-21 15:41:09 +00:00
|
|
|
import { addClassToBody } from './hacks';
|
2021-09-22 15:00:19 +00:00
|
|
|
import { useViewSwitcher } from './use-view-switcher';
|
2021-09-21 15:41:09 +00:00
|
|
|
import type { Attributes } from './types';
|
2021-09-22 15:00:19 +00:00
|
|
|
import { CartBlockControlsContext } from './context';
|
2021-09-21 15:41:09 +00:00
|
|
|
|
|
|
|
// 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/filled-cart-block',
|
|
|
|
'woocommerce/empty-cart-block',
|
|
|
|
];
|
2021-09-17 15:29:58 +00:00
|
|
|
|
2021-09-21 15:41:09 +00:00
|
|
|
const BlockSettings = ( {
|
|
|
|
attributes,
|
|
|
|
setAttributes,
|
|
|
|
}: {
|
|
|
|
attributes: Attributes;
|
|
|
|
setAttributes: ( attributes: Record< string, unknown > ) => undefined;
|
|
|
|
} ): JSX.Element => {
|
2021-09-21 12:20:42 +00:00
|
|
|
const { isShippingCalculatorEnabled, showRateAfterTaxName } = attributes;
|
2021-09-17 15:29:58 +00:00
|
|
|
const { currentPostId } = useEditorContext();
|
|
|
|
return (
|
|
|
|
<InspectorControls>
|
|
|
|
{ currentPostId !== CART_PAGE_ID && (
|
|
|
|
<Notice
|
|
|
|
className="wc-block-cart__page-notice"
|
|
|
|
isDismissible={ false }
|
|
|
|
status="warning"
|
|
|
|
>
|
|
|
|
{ createInterpolateElement(
|
|
|
|
__(
|
|
|
|
'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>
|
|
|
|
) }
|
2021-09-21 12:20:42 +00:00
|
|
|
{ getSetting( 'shippingEnabled', true ) && (
|
|
|
|
<PanelBody
|
|
|
|
title={ __(
|
|
|
|
'Shipping rates',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
<ToggleControl
|
|
|
|
label={ __(
|
|
|
|
'Shipping calculator',
|
2021-09-17 15:29:58 +00:00
|
|
|
'woo-gutenberg-products-block'
|
2021-09-21 12:20:42 +00:00
|
|
|
) }
|
|
|
|
help={ __(
|
|
|
|
'Allow customers to estimate shipping by entering their address.',
|
2021-09-17 15:29:58 +00:00
|
|
|
'woo-gutenberg-products-block'
|
2021-09-21 12:20:42 +00:00
|
|
|
) }
|
|
|
|
checked={ isShippingCalculatorEnabled }
|
|
|
|
onChange={ () =>
|
|
|
|
setAttributes( {
|
|
|
|
isShippingCalculatorEnabled: ! isShippingCalculatorEnabled,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</PanelBody>
|
2021-09-17 15:29:58 +00:00
|
|
|
) }
|
2021-09-21 12:20:42 +00:00
|
|
|
{ getSetting( 'taxesEnabled' ) &&
|
|
|
|
getSetting( 'displayItemizedTaxes', false ) &&
|
|
|
|
! getSetting( 'displayCartPricesIncludingTax', false ) && (
|
|
|
|
<PanelBody
|
|
|
|
title={ __( 'Taxes', 'woo-gutenberg-products-block' ) }
|
|
|
|
>
|
|
|
|
<ToggleControl
|
|
|
|
label={ __(
|
|
|
|
'Show rate after tax name',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
help={ __(
|
|
|
|
'Show the percentage rate alongside each tax line in the summary.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
checked={ showRateAfterTaxName }
|
|
|
|
onChange={ () =>
|
|
|
|
setAttributes( {
|
|
|
|
showRateAfterTaxName: ! showRateAfterTaxName,
|
|
|
|
} )
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</PanelBody>
|
|
|
|
) }
|
2021-09-21 13:42:12 +00:00
|
|
|
<CartCheckoutFeedbackPrompt />
|
2021-09-17 15:29:58 +00:00
|
|
|
</InspectorControls>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Component to handle edit mode of "Cart Block".
|
|
|
|
*/
|
2021-09-21 15:41:09 +00:00
|
|
|
export const Edit = ( {
|
|
|
|
className,
|
|
|
|
attributes,
|
|
|
|
setAttributes,
|
|
|
|
}: {
|
|
|
|
className: string;
|
|
|
|
attributes: Attributes;
|
|
|
|
setAttributes: ( attributes: Record< string, unknown > ) => undefined;
|
|
|
|
} ): JSX.Element => {
|
2021-09-22 15:00:19 +00:00
|
|
|
const { currentView, component: ViewSwitcherComponent } = useViewSwitcher( [
|
|
|
|
{
|
|
|
|
view: 'emptyCart',
|
|
|
|
label: __( 'Empty Cart', 'woo-gutenberg-products-block' ),
|
|
|
|
icon: <Icon srcElement={ removeCart } />,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
view: 'filledCart',
|
|
|
|
label: __( 'Filled Cart', 'woo-gutenberg-products-block' ),
|
|
|
|
icon: <Icon srcElement={ filledCart } />,
|
|
|
|
default: true,
|
|
|
|
},
|
|
|
|
] );
|
2021-09-21 15:41:09 +00:00
|
|
|
const cartClassName = classnames( {
|
2021-09-21 10:18:27 +00:00
|
|
|
'has-dark-controls': attributes.hasDarkControls,
|
|
|
|
} );
|
|
|
|
const defaultInnerBlocksTemplate = [
|
2021-09-21 10:47:20 +00:00
|
|
|
[
|
2021-09-21 15:41:09 +00:00
|
|
|
'woocommerce/filled-cart-block',
|
2021-09-21 11:58:36 +00:00
|
|
|
{},
|
2021-09-21 12:20:42 +00:00
|
|
|
[
|
2021-09-21 15:41:09 +00:00
|
|
|
[
|
|
|
|
'woocommerce/cart-items-block',
|
|
|
|
{},
|
|
|
|
[ [ 'woocommerce/cart-line-items-block', {}, [] ] ],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'woocommerce/cart-totals-block',
|
|
|
|
{},
|
|
|
|
[
|
|
|
|
[ 'woocommerce/cart-order-summary-block', {}, [] ],
|
|
|
|
[ 'woocommerce/cart-express-payment-block', {}, [] ],
|
|
|
|
[ 'woocommerce/proceed-to-checkout-block', {}, [] ],
|
|
|
|
],
|
|
|
|
],
|
2021-09-21 12:20:42 +00:00
|
|
|
],
|
2021-09-21 11:58:36 +00:00
|
|
|
],
|
2021-09-21 15:41:09 +00:00
|
|
|
[ 'woocommerce/empty-cart-block', {}, [] ],
|
2021-09-21 10:18:27 +00:00
|
|
|
];
|
2021-09-17 15:29:58 +00:00
|
|
|
return (
|
|
|
|
<div
|
|
|
|
className={ classnames( className, 'wp-block-woocommerce-cart', {
|
|
|
|
'is-editor-preview': attributes.isPreview,
|
|
|
|
} ) }
|
|
|
|
>
|
2021-09-21 15:41:09 +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'
|
2021-09-17 15:29:58 +00:00
|
|
|
) }
|
2021-09-21 15:41:09 +00:00
|
|
|
showErrorMessage={ true }
|
|
|
|
errorMessagePrefix={ __(
|
|
|
|
'Error message:',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
) }
|
|
|
|
>
|
|
|
|
<EditorProvider previewData={ { previewCart } }>
|
|
|
|
<BlockSettings
|
|
|
|
attributes={ attributes }
|
|
|
|
setAttributes={ setAttributes }
|
|
|
|
/>
|
2021-09-22 15:00:19 +00:00
|
|
|
<ViewSwitcherComponent />
|
|
|
|
<CartBlockControlsContext.Provider
|
|
|
|
value={ {
|
|
|
|
viewSwitcher: {
|
|
|
|
component: ViewSwitcherComponent,
|
|
|
|
currentView,
|
|
|
|
},
|
|
|
|
} }
|
|
|
|
>
|
|
|
|
<CartProvider>
|
|
|
|
<div className={ cartClassName }>
|
|
|
|
<InnerBlocks
|
|
|
|
allowedBlocks={ ALLOWED_BLOCKS }
|
|
|
|
template={ defaultInnerBlocksTemplate }
|
|
|
|
templateLock="insert"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</CartProvider>
|
|
|
|
</CartBlockControlsContext.Provider>
|
2021-09-21 15:41:09 +00:00
|
|
|
</EditorProvider>
|
|
|
|
</BlockErrorBoundary>
|
2021-09-17 15:29:58 +00:00
|
|
|
<CartCheckoutCompatibilityNotice blockName="cart" />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-09-21 15:41:09 +00:00
|
|
|
export const Save = (): JSX.Element => {
|
|
|
|
return (
|
|
|
|
<div
|
|
|
|
{ ...useBlockProps.save( {
|
|
|
|
className: 'wc-block-cart is-loading',
|
|
|
|
} ) }
|
|
|
|
>
|
|
|
|
<InnerBlocks.Content />
|
|
|
|
</div>
|
|
|
|
);
|
2021-09-17 15:29:58 +00:00
|
|
|
};
|