2021-08-25 15:42:55 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-09-07 08:27:16 +00:00
|
|
|
import classNames from 'classnames';
|
2021-09-02 09:44:25 +00:00
|
|
|
import { __, _n, sprintf } from '@wordpress/i18n';
|
2021-09-07 08:27:16 +00:00
|
|
|
import { useState, useEffect } from '@wordpress/element';
|
|
|
|
import { dispatch } from '@wordpress/data';
|
|
|
|
import {
|
|
|
|
translateJQueryEventToNative,
|
|
|
|
renderFrontend,
|
|
|
|
} from '@woocommerce/base-utils';
|
2021-08-25 15:42:55 +00:00
|
|
|
import { useStoreCart } from '@woocommerce/base-context/hooks';
|
2021-09-02 09:44:25 +00:00
|
|
|
import Drawer from '@woocommerce/base-components/drawer';
|
2021-09-07 08:27:16 +00:00
|
|
|
import { CART_STORE_KEY as storeKey } from '@woocommerce/block-data';
|
2021-08-25 15:42:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import CartLineItemsTable from '../cart/full-cart/cart-line-items-table';
|
2021-09-07 08:27:16 +00:00
|
|
|
import withMiniCartConditionalHydration from './with-mini-cart-conditional-hydration';
|
2021-09-02 09:44:25 +00:00
|
|
|
import './style.scss';
|
2021-08-25 15:42:55 +00:00
|
|
|
|
2021-09-07 08:27:16 +00:00
|
|
|
interface MiniCartBlockProps {
|
2021-09-02 09:44:25 +00:00
|
|
|
isPlaceholderOpen?: boolean;
|
|
|
|
}
|
2021-08-25 15:42:55 +00:00
|
|
|
|
2021-09-07 08:27:16 +00:00
|
|
|
const MiniCartBlock = ( {
|
|
|
|
isPlaceholderOpen = false,
|
|
|
|
}: MiniCartBlockProps ): JSX.Element => {
|
2021-09-02 09:44:25 +00:00
|
|
|
const { cartItems, cartItemsCount, cartIsLoading } = useStoreCart();
|
|
|
|
const [ isOpen, setIsOpen ] = useState< boolean >( isPlaceholderOpen );
|
|
|
|
// We already rendered the HTML drawer placeholder, so we want to skip the
|
|
|
|
// slide in animation.
|
|
|
|
const [ skipSlideIn, setSkipSlideIn ] = useState< boolean >(
|
|
|
|
isPlaceholderOpen
|
|
|
|
);
|
|
|
|
|
2021-09-07 08:27:16 +00:00
|
|
|
useEffect( () => {
|
|
|
|
const openMiniCartAndRefreshData = () => {
|
|
|
|
dispatch( storeKey ).invalidateResolutionForStore();
|
|
|
|
setSkipSlideIn( false );
|
|
|
|
setIsOpen( true );
|
|
|
|
};
|
|
|
|
|
|
|
|
// Make it so we can read jQuery events triggered by WC Core elements.
|
|
|
|
const removeJQueryAddedToCartEvent = translateJQueryEventToNative(
|
|
|
|
'added_to_cart',
|
|
|
|
'wc-blocks_added_to_cart'
|
|
|
|
);
|
|
|
|
|
|
|
|
document.body.addEventListener(
|
|
|
|
'wc-blocks_added_to_cart',
|
|
|
|
openMiniCartAndRefreshData
|
|
|
|
);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
removeJQueryAddedToCartEvent();
|
|
|
|
|
|
|
|
document.body.removeEventListener(
|
|
|
|
'wc-blocks_added_to_cart',
|
|
|
|
openMiniCartAndRefreshData
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}, [] );
|
|
|
|
|
2021-09-02 09:44:25 +00:00
|
|
|
const contents =
|
2021-09-07 08:27:16 +00:00
|
|
|
! cartIsLoading && cartItems.length === 0 ? (
|
2021-09-02 09:44:25 +00:00
|
|
|
<>{ __( 'Cart is empty', 'woo-gutenberg-products-block' ) }</>
|
|
|
|
) : (
|
2021-09-07 08:27:16 +00:00
|
|
|
<CartLineItemsTable
|
|
|
|
lineItems={ cartItems }
|
|
|
|
isLoading={ cartIsLoading }
|
|
|
|
/>
|
2021-09-02 09:44:25 +00:00
|
|
|
);
|
2021-08-25 15:42:55 +00:00
|
|
|
|
|
|
|
return (
|
2021-09-02 09:44:25 +00:00
|
|
|
<>
|
|
|
|
<button
|
|
|
|
className="wc-block-mini-cart__button"
|
|
|
|
onClick={ () => {
|
|
|
|
if ( ! isOpen ) {
|
|
|
|
setIsOpen( true );
|
|
|
|
setSkipSlideIn( false );
|
|
|
|
}
|
|
|
|
} }
|
|
|
|
>
|
|
|
|
{ sprintf(
|
|
|
|
/* translators: %d is the count of items in the cart. */
|
|
|
|
_n(
|
|
|
|
'%d item',
|
|
|
|
'%d items',
|
|
|
|
cartItemsCount,
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
cartItemsCount
|
|
|
|
) }
|
|
|
|
</button>
|
|
|
|
<Drawer
|
2021-09-07 08:27:16 +00:00
|
|
|
className={ classNames(
|
|
|
|
'wc-block-mini-cart__drawer',
|
|
|
|
'is-mobile',
|
|
|
|
{
|
|
|
|
'is-loading': cartIsLoading,
|
|
|
|
}
|
|
|
|
) }
|
2021-09-02 09:44:25 +00:00
|
|
|
title={ sprintf(
|
|
|
|
/* translators: %d is the count of items in the cart. */
|
|
|
|
_n(
|
|
|
|
'Your cart (%d item)',
|
|
|
|
'Your cart (%d items)',
|
|
|
|
cartItemsCount,
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
cartItemsCount
|
|
|
|
) }
|
|
|
|
isOpen={ isOpen }
|
|
|
|
onClose={ () => {
|
|
|
|
setIsOpen( false );
|
|
|
|
} }
|
|
|
|
slideIn={ ! skipSlideIn }
|
|
|
|
>
|
|
|
|
{ contents }
|
|
|
|
</Drawer>
|
|
|
|
</>
|
2021-08-25 15:42:55 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2021-09-02 09:44:25 +00:00
|
|
|
const renderMiniCartFrontend = () => {
|
|
|
|
// Check if button is focused. In that case, we want to refocus it after we
|
|
|
|
// replace it with the React equivalent.
|
|
|
|
let focusedMiniCartBlock: HTMLElement | null = null;
|
|
|
|
/* eslint-disable @wordpress/no-global-active-element */
|
|
|
|
if (
|
|
|
|
document.activeElement &&
|
|
|
|
document.activeElement.classList.contains(
|
|
|
|
'wc-block-mini-cart__button'
|
|
|
|
) &&
|
|
|
|
document.activeElement.parentNode instanceof HTMLElement
|
|
|
|
) {
|
|
|
|
focusedMiniCartBlock = document.activeElement.parentNode;
|
|
|
|
}
|
|
|
|
/* eslint-enable @wordpress/no-global-active-element */
|
|
|
|
|
|
|
|
renderFrontend( {
|
|
|
|
selector: '.wc-block-mini-cart',
|
2021-09-07 08:27:16 +00:00
|
|
|
Block: withMiniCartConditionalHydration( MiniCartBlock ),
|
2021-09-02 09:44:25 +00:00
|
|
|
getProps: ( el: HTMLElement ) => ( {
|
2021-09-07 08:27:16 +00:00
|
|
|
isDataOutdated: el.dataset.isDataOutdated,
|
2021-09-02 09:44:25 +00:00
|
|
|
isPlaceholderOpen: el.dataset.isPlaceholderOpen,
|
|
|
|
} ),
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Refocus previously focused button if drawer is not open.
|
|
|
|
if (
|
|
|
|
focusedMiniCartBlock instanceof HTMLElement &&
|
|
|
|
! focusedMiniCartBlock.dataset.isPlaceholderOpen
|
|
|
|
) {
|
|
|
|
const innerButton = focusedMiniCartBlock.querySelector(
|
|
|
|
'.wc-block-mini-cart__button'
|
|
|
|
);
|
|
|
|
if ( innerButton instanceof HTMLElement ) {
|
|
|
|
innerButton.focus();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
renderMiniCartFrontend();
|