2019-12-03 13:57:56 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-03-04 12:40:03 +00:00
|
|
|
import {
|
|
|
|
withRestApiHydration,
|
|
|
|
withStoreCartApiHydration,
|
|
|
|
} from '@woocommerce/block-hocs';
|
2020-03-06 11:43:40 +00:00
|
|
|
import { __ } from '@wordpress/i18n';
|
2020-02-18 23:06:37 +00:00
|
|
|
import { useStoreCart } from '@woocommerce/base-hooks';
|
|
|
|
import { RawHTML } from '@wordpress/element';
|
2020-02-25 11:36:53 +00:00
|
|
|
import LoadingMask from '@woocommerce/base-components/loading-mask';
|
2020-03-10 13:39:21 +00:00
|
|
|
import { StoreNoticesProvider } from '@woocommerce/base-context';
|
2020-03-06 11:43:40 +00:00
|
|
|
import { CURRENT_USER_IS_ADMIN } from '@woocommerce/block-settings';
|
|
|
|
import { __experimentalCreateInterpolateElement } from 'wordpress-element';
|
2020-03-03 10:26:02 +00:00
|
|
|
|
2019-12-03 13:57:56 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-12-10 15:41:57 +00:00
|
|
|
import FullCart from './full-cart';
|
2019-12-03 13:57:56 +00:00
|
|
|
import renderFrontend from '../../../utils/render-frontend.js';
|
|
|
|
|
2020-02-18 23:06:37 +00:00
|
|
|
/**
|
|
|
|
* Wrapper component to supply API data and show empty cart view as needed.
|
|
|
|
*/
|
2020-02-19 16:33:10 +00:00
|
|
|
const CartFrontend = ( {
|
|
|
|
emptyCart,
|
|
|
|
isShippingCalculatorEnabled,
|
|
|
|
isShippingCostHidden,
|
|
|
|
} ) => {
|
2020-02-25 11:36:53 +00:00
|
|
|
const {
|
|
|
|
cartItems,
|
|
|
|
cartTotals,
|
|
|
|
cartIsLoading,
|
|
|
|
cartCoupons,
|
2020-03-05 19:54:05 +00:00
|
|
|
shippingRates,
|
2020-02-25 11:36:53 +00:00
|
|
|
} = useStoreCart();
|
2020-02-18 23:06:37 +00:00
|
|
|
|
2020-02-25 11:36:53 +00:00
|
|
|
return (
|
2020-03-03 10:26:02 +00:00
|
|
|
<StoreNoticesProvider context="wc/cart">
|
2020-02-26 15:49:07 +00:00
|
|
|
{ ! cartIsLoading && ! cartItems.length ? (
|
2020-02-25 11:36:53 +00:00
|
|
|
<RawHTML>{ emptyCart }</RawHTML>
|
|
|
|
) : (
|
|
|
|
<LoadingMask showSpinner={ true } isLoading={ cartIsLoading }>
|
|
|
|
<FullCart
|
|
|
|
cartItems={ cartItems }
|
|
|
|
cartTotals={ cartTotals }
|
|
|
|
cartCoupons={ cartCoupons }
|
|
|
|
isShippingCalculatorEnabled={
|
|
|
|
isShippingCalculatorEnabled
|
|
|
|
}
|
|
|
|
isShippingCostHidden={ isShippingCostHidden }
|
2020-02-26 15:49:07 +00:00
|
|
|
isLoading={ cartIsLoading }
|
2020-03-05 19:54:05 +00:00
|
|
|
shippingRates={ shippingRates }
|
2020-02-25 11:36:53 +00:00
|
|
|
/>
|
|
|
|
</LoadingMask>
|
|
|
|
) }
|
2020-03-03 10:26:02 +00:00
|
|
|
</StoreNoticesProvider>
|
2020-02-18 23:06:37 +00:00
|
|
|
);
|
|
|
|
};
|
2019-12-10 15:41:57 +00:00
|
|
|
|
2020-02-18 23:06:37 +00:00
|
|
|
const getProps = ( el ) => ( {
|
|
|
|
emptyCart: el.innerHTML,
|
2020-02-19 16:33:10 +00:00
|
|
|
isShippingCalculatorEnabled:
|
2020-03-10 09:09:34 +00:00
|
|
|
el.dataset.isShippingCalculatorEnabled === 'true',
|
|
|
|
isShippingCostHidden: el.dataset.isShippingCostHidden === 'true',
|
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' ),
|
|
|
|
text: __experimentalCreateInterpolateElement(
|
|
|
|
__(
|
|
|
|
'The cart has encountered an unexpected error. <a>Try reloading the page</a>. If the error persists, please get in touch with us so we can assist.',
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
{
|
|
|
|
a: (
|
|
|
|
// eslint-disable-next-line jsx-a11y/anchor-has-content, jsx-a11y/anchor-is-valid
|
|
|
|
<a href="javascript:window.location.reload(true)" />
|
|
|
|
),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
showErrorMessage: CURRENT_USER_IS_ADMIN,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-02-18 23:06:37 +00:00
|
|
|
renderFrontend(
|
|
|
|
'.wp-block-woocommerce-cart',
|
2020-03-04 12:40:03 +00:00
|
|
|
withStoreCartApiHydration( withRestApiHydration( CartFrontend ) ),
|
2020-03-06 11:43:40 +00:00
|
|
|
getProps,
|
|
|
|
getErrorBoundaryProps
|
2020-02-18 23:06:37 +00:00
|
|
|
);
|