2021-09-23 15:38:30 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useStoreCart } from '@woocommerce/base-context/hooks';
|
|
|
|
import { useEffect } from '@wordpress/element';
|
|
|
|
import { dispatchEvent } from '@woocommerce/base-utils';
|
|
|
|
|
2021-10-25 14:46:34 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import './style.scss';
|
|
|
|
|
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
|
|
|
}: {
|
|
|
|
children: JSX.Element;
|
2021-10-25 15:31:22 +00:00
|
|
|
className: string;
|
2021-09-23 15:38:30 +00:00
|
|
|
} ): JSX.Element | null => {
|
|
|
|
const { cartItems, cartIsLoading } = useStoreCart();
|
|
|
|
useEffect( () => {
|
2023-08-24 08:37:14 +00:00
|
|
|
if ( cartItems.length !== 0 || cartIsLoading ) {
|
|
|
|
return;
|
|
|
|
}
|
2021-09-23 15:38:30 +00:00
|
|
|
dispatchEvent( 'wc-blocks_render_blocks_frontend', {
|
|
|
|
element: document.body.querySelector(
|
|
|
|
'.wp-block-woocommerce-cart'
|
|
|
|
),
|
|
|
|
} );
|
2023-08-24 08:37:14 +00:00
|
|
|
}, [ cartIsLoading, cartItems ] );
|
2021-09-23 15:38:30 +00:00
|
|
|
if ( ! cartIsLoading && cartItems.length === 0 ) {
|
2021-10-25 15:31:22 +00:00
|
|
|
return <div className={ className }>{ children }</div>;
|
2021-09-23 15:38:30 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default FrontendBlock;
|