2019-12-03 13:57:56 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { withRestApiHydration } from '@woocommerce/block-hocs';
|
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';
|
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,
|
|
|
|
cartErrors,
|
|
|
|
cartCoupons,
|
|
|
|
} = useStoreCart();
|
2020-02-18 23:06:37 +00:00
|
|
|
|
2020-02-25 11:36:53 +00:00
|
|
|
// Blank state on first load.
|
|
|
|
if ( cartIsLoading && ! cartItems.length ) {
|
2020-02-18 23:06:37 +00:00
|
|
|
return null;
|
|
|
|
}
|
2019-12-10 15:41:57 +00:00
|
|
|
|
2020-02-25 11:36:53 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className="errors">
|
|
|
|
{ // @todo This is a placeholder for error messages - this needs refactoring.
|
|
|
|
cartErrors &&
|
|
|
|
cartErrors.map( ( error = {}, i ) => (
|
|
|
|
<div className="woocommerce-info" key={ 'notice-' + i }>
|
|
|
|
{ error.message }
|
|
|
|
</div>
|
|
|
|
) ) }
|
|
|
|
</div>
|
|
|
|
{ ! cartItems.length ? (
|
|
|
|
<RawHTML>{ emptyCart }</RawHTML>
|
|
|
|
) : (
|
|
|
|
<LoadingMask showSpinner={ true } isLoading={ cartIsLoading }>
|
|
|
|
<FullCart
|
|
|
|
cartItems={ cartItems }
|
|
|
|
cartTotals={ cartTotals }
|
|
|
|
cartCoupons={ cartCoupons }
|
|
|
|
isShippingCalculatorEnabled={
|
|
|
|
isShippingCalculatorEnabled
|
|
|
|
}
|
|
|
|
isShippingCostHidden={ isShippingCostHidden }
|
|
|
|
/>
|
|
|
|
</LoadingMask>
|
|
|
|
) }
|
|
|
|
</>
|
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:
|
|
|
|
el.dataset.isshippingcalculatorenabled === 'true',
|
|
|
|
isShippingCostHidden: el.dataset.isshippingcosthidden === 'true',
|
2020-02-18 23:06:37 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
renderFrontend(
|
|
|
|
'.wp-block-woocommerce-cart',
|
|
|
|
withRestApiHydration( CartFrontend ),
|
|
|
|
getProps
|
|
|
|
);
|