2021-09-23 15:38:30 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import { SidebarLayout } from '@woocommerce/base-components/sidebar-layout';
|
2022-04-08 12:11:50 +00:00
|
|
|
import { useStoreCart } from '@woocommerce/base-context/hooks';
|
2021-09-24 14:23:10 +00:00
|
|
|
import { useEffect } from '@wordpress/element';
|
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2022-04-08 12:11:50 +00:00
|
|
|
import { useDispatch } from '@wordpress/data';
|
2021-09-23 15:38:30 +00:00
|
|
|
|
2021-10-18 16:12:36 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { useCartBlockContext } from '../../context';
|
|
|
|
|
2021-09-23 15:38:30 +00:00
|
|
|
const FrontendBlock = ( {
|
|
|
|
children,
|
2021-10-25 15:31:22 +00:00
|
|
|
className,
|
2021-09-23 15:38:30 +00:00
|
|
|
}: {
|
2021-10-25 14:46:34 +00:00
|
|
|
children: JSX.Element | JSX.Element[];
|
2021-10-25 15:31:22 +00:00
|
|
|
className: string;
|
2021-09-23 15:38:30 +00:00
|
|
|
} ): JSX.Element | null => {
|
2021-09-24 14:23:10 +00:00
|
|
|
const { cartItems, cartIsLoading, cartItemErrors } = useStoreCart();
|
2021-10-18 16:12:36 +00:00
|
|
|
const { hasDarkControls } = useCartBlockContext();
|
2022-04-08 12:11:50 +00:00
|
|
|
const { createErrorNotice } = useDispatch( 'core/notices' );
|
2021-09-24 14:23:10 +00:00
|
|
|
|
|
|
|
// Ensures any cart errors listed in the API response get shown.
|
|
|
|
useEffect( () => {
|
|
|
|
cartItemErrors.forEach( ( error ) => {
|
2022-04-08 12:11:50 +00:00
|
|
|
createErrorNotice( decodeEntities( error.message ), {
|
2021-09-24 14:23:10 +00:00
|
|
|
isDismissible: true,
|
|
|
|
id: error.code,
|
2022-04-15 12:17:09 +00:00
|
|
|
context: 'wc/cart',
|
2021-09-24 14:23:10 +00:00
|
|
|
} );
|
|
|
|
} );
|
2022-04-08 12:11:50 +00:00
|
|
|
}, [ createErrorNotice, cartItemErrors ] );
|
2021-10-18 16:12:36 +00:00
|
|
|
|
2021-09-23 15:38:30 +00:00
|
|
|
if ( cartIsLoading || cartItems.length >= 1 ) {
|
|
|
|
return (
|
|
|
|
<SidebarLayout
|
2021-10-25 15:31:22 +00:00
|
|
|
className={ classnames( 'wc-block-cart', className, {
|
2021-09-23 15:38:30 +00:00
|
|
|
'has-dark-controls': hasDarkControls,
|
|
|
|
} ) }
|
|
|
|
>
|
|
|
|
{ children }
|
|
|
|
</SidebarLayout>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default FrontendBlock;
|