2019-12-03 13:57:56 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-03-04 12:40:03 +00:00
|
|
|
import {
|
|
|
|
withStoreCartApiHydration,
|
2021-04-22 11:37:27 +00:00
|
|
|
withRestApiHydration,
|
2020-03-04 12:40:03 +00:00
|
|
|
} from '@woocommerce/block-hocs';
|
2020-03-06 11:43:40 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2021-07-05 08:38:08 +00:00
|
|
|
import {
|
|
|
|
StoreNoticesProvider,
|
|
|
|
StoreSnackbarNoticesProvider,
|
2021-07-08 15:04:13 +00:00
|
|
|
CartProvider,
|
2021-07-05 08:38:08 +00:00
|
|
|
} from '@woocommerce/base-context/providers';
|
2021-07-08 15:04:13 +00:00
|
|
|
import { SlotFillProvider } from '@woocommerce/blocks-checkout';
|
2021-04-22 11:37:27 +00:00
|
|
|
import { CURRENT_USER_IS_ADMIN } from '@woocommerce/settings';
|
2020-05-27 10:11:30 +00:00
|
|
|
import {
|
|
|
|
renderFrontend,
|
2020-06-05 12:18:16 +00:00
|
|
|
getValidBlockAttributes,
|
2020-05-27 10:11:30 +00:00
|
|
|
} from '@woocommerce/base-utils';
|
2019-12-03 13:57:56 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2020-04-08 15:03:39 +00:00
|
|
|
import Block from './block.js';
|
2020-03-13 13:41:59 +00:00
|
|
|
import blockAttributes from './attributes';
|
2019-12-03 13:57:56 +00:00
|
|
|
|
2020-04-03 13:22:56 +00:00
|
|
|
const reloadPage = () => void window.location.reload( true );
|
2020-03-13 13:41:59 +00:00
|
|
|
/**
|
|
|
|
* Wrapper component to supply API data and show empty cart view as needed.
|
|
|
|
*
|
|
|
|
* @param {*} props
|
|
|
|
*/
|
|
|
|
const CartFrontend = ( props ) => {
|
|
|
|
return (
|
2021-07-05 08:38:08 +00:00
|
|
|
<StoreSnackbarNoticesProvider context="wc/cart">
|
|
|
|
<StoreNoticesProvider context="wc/cart">
|
2021-07-08 15:04:13 +00:00
|
|
|
<SlotFillProvider>
|
|
|
|
<CartProvider>
|
|
|
|
<Block { ...props } />
|
|
|
|
</CartProvider>
|
|
|
|
</SlotFillProvider>
|
2021-07-05 08:38:08 +00:00
|
|
|
</StoreNoticesProvider>
|
|
|
|
</StoreSnackbarNoticesProvider>
|
2020-02-18 23:06:37 +00:00
|
|
|
);
|
|
|
|
};
|
2019-12-10 15:41:57 +00:00
|
|
|
|
2020-03-13 13:41:59 +00:00
|
|
|
const getProps = ( el ) => {
|
|
|
|
return {
|
|
|
|
emptyCart: el.innerHTML,
|
2020-06-05 12:18:16 +00:00
|
|
|
attributes: getValidBlockAttributes( blockAttributes, el.dataset ),
|
2020-03-13 13:41:59 +00:00
|
|
|
};
|
|
|
|
};
|
2020-02-18 23:06:37 +00:00
|
|
|
|
2020-03-06 11:43:40 +00:00
|
|
|
const getErrorBoundaryProps = () => {
|
|
|
|
return {
|
|
|
|
header: __( 'Something went wrong…', 'woo-gutenberg-products-block' ),
|
2021-06-29 14:04:24 +00:00
|
|
|
text: __(
|
|
|
|
'The cart has encountered an unexpected error. If the error persists, please get in touch with us for help.',
|
|
|
|
'woo-gutenberg-products-block'
|
2020-03-06 11:43:40 +00:00
|
|
|
),
|
|
|
|
showErrorMessage: CURRENT_USER_IS_ADMIN,
|
2021-06-29 14:04:24 +00:00
|
|
|
button: (
|
|
|
|
<button className="wc-block-button" onClick={ reloadPage }>
|
|
|
|
{ __( 'Reload the page', 'woo-gutenberg-products-block' ) }
|
|
|
|
</button>
|
|
|
|
),
|
2020-03-06 11:43:40 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-05-27 10:11:30 +00:00
|
|
|
renderFrontend( {
|
|
|
|
selector: '.wp-block-woocommerce-cart',
|
|
|
|
Block: withStoreCartApiHydration( withRestApiHydration( CartFrontend ) ),
|
2020-03-06 11:43:40 +00:00
|
|
|
getProps,
|
2020-05-27 10:11:30 +00:00
|
|
|
getErrorBoundaryProps,
|
|
|
|
} );
|