2019-12-03 14:12:46 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-03-06 11:43:40 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2020-03-12 09:24:50 +00:00
|
|
|
import {
|
|
|
|
withStoreCartApiHydration,
|
2021-04-22 11:37:27 +00:00
|
|
|
withRestApiHydration,
|
2020-03-12 09:24:50 +00:00
|
|
|
} from '@woocommerce/block-hocs';
|
2021-04-08 12:31:12 +00:00
|
|
|
import { useStoreCart } from '@woocommerce/base-context/hooks';
|
2020-03-23 11:22:00 +00:00
|
|
|
import {
|
|
|
|
StoreNoticesProvider,
|
|
|
|
ValidationContextProvider,
|
|
|
|
} from '@woocommerce/base-context';
|
2020-03-06 11:43:40 +00:00
|
|
|
import BlockErrorBoundary from '@woocommerce/base-components/block-error-boundary';
|
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 14:12:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import Block from './block.js';
|
2020-03-05 13:06:47 +00:00
|
|
|
import blockAttributes from './attributes';
|
2020-03-26 13:31:09 +00:00
|
|
|
import EmptyCart from './empty-cart/index.js';
|
2019-12-03 14:12:46 +00:00
|
|
|
|
2020-04-03 13:22:56 +00:00
|
|
|
const reloadPage = () => void window.location.reload( true );
|
|
|
|
|
2021-06-29 14:04:24 +00:00
|
|
|
const errorBoundaryProps = {
|
|
|
|
header: __( 'Something went wrong…', 'woo-gutenberg-products-block' ),
|
|
|
|
text: __(
|
|
|
|
'The checkout has encountered an unexpected error. If the error persists, please get in touch with us for help.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
showErrorMessage: CURRENT_USER_IS_ADMIN,
|
|
|
|
button: (
|
|
|
|
<button className="wc-block-button" onClick={ reloadPage }>
|
|
|
|
{ __( 'Reload the page', 'woo-gutenberg-products-block' ) }
|
|
|
|
</button>
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
2020-03-05 19:54:05 +00:00
|
|
|
/**
|
2020-03-06 11:43:40 +00:00
|
|
|
* Wrapper component for the checkout block.
|
2020-03-05 19:54:05 +00:00
|
|
|
*
|
2020-03-06 11:43:40 +00:00
|
|
|
* @param {Object} props Props for the block.
|
2020-03-05 19:54:05 +00:00
|
|
|
*/
|
2020-03-06 11:43:40 +00:00
|
|
|
const CheckoutFrontend = ( props ) => {
|
2020-04-07 15:41:22 +00:00
|
|
|
const { cartItems, cartIsLoading } = useStoreCart();
|
2020-03-10 15:49:26 +00:00
|
|
|
|
2020-03-06 11:43:40 +00:00
|
|
|
return (
|
2020-03-19 15:50:36 +00:00
|
|
|
<>
|
|
|
|
{ ! cartIsLoading && cartItems.length === 0 ? (
|
|
|
|
<EmptyCart />
|
|
|
|
) : (
|
2021-06-29 14:04:24 +00:00
|
|
|
<BlockErrorBoundary { ...errorBoundaryProps }>
|
2020-03-19 15:50:36 +00:00
|
|
|
<StoreNoticesProvider context="wc/checkout">
|
2020-03-23 11:22:00 +00:00
|
|
|
<ValidationContextProvider>
|
2020-04-07 15:41:22 +00:00
|
|
|
<Block { ...props } />
|
2020-03-23 11:22:00 +00:00
|
|
|
</ValidationContextProvider>
|
2020-03-19 15:50:36 +00:00
|
|
|
</StoreNoticesProvider>
|
|
|
|
</BlockErrorBoundary>
|
2020-03-06 11:43:40 +00:00
|
|
|
) }
|
2020-03-19 15:50:36 +00:00
|
|
|
</>
|
2020-03-06 11:43:40 +00:00
|
|
|
);
|
2020-03-05 19:54:05 +00:00
|
|
|
};
|
|
|
|
|
2020-03-04 15:13:38 +00:00
|
|
|
const getProps = ( el ) => {
|
2019-12-03 14:12:46 +00:00
|
|
|
return {
|
2020-06-05 12:18:16 +00:00
|
|
|
attributes: getValidBlockAttributes( blockAttributes, el.dataset ),
|
2019-12-03 14:12:46 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-03-06 11:43:40 +00:00
|
|
|
const getErrorBoundaryProps = () => {
|
2021-06-29 14:04:24 +00:00
|
|
|
return errorBoundaryProps;
|
2020-03-06 11:43:40 +00:00
|
|
|
};
|
|
|
|
|
2020-05-27 10:11:30 +00:00
|
|
|
renderFrontend( {
|
|
|
|
selector: '.wp-block-woocommerce-checkout',
|
|
|
|
Block: withStoreCartApiHydration(
|
|
|
|
withRestApiHydration( CheckoutFrontend )
|
|
|
|
),
|
2020-03-06 11:43:40 +00:00
|
|
|
getProps,
|
2020-05-27 10:11:30 +00:00
|
|
|
getErrorBoundaryProps,
|
|
|
|
} );
|