2021-09-15 11:40:36 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-11-17 15:39:07 +00:00
|
|
|
import classnames from 'classnames';
|
2021-09-15 11:40:36 +00:00
|
|
|
import { __, _n, sprintf } from '@wordpress/i18n';
|
2021-10-28 15:48:39 +00:00
|
|
|
import {
|
2021-11-19 11:47:48 +00:00
|
|
|
RawHTML,
|
|
|
|
useState,
|
|
|
|
useEffect,
|
|
|
|
useRef,
|
|
|
|
unmountComponentAtNode,
|
|
|
|
} from '@wordpress/element';
|
|
|
|
import {
|
|
|
|
renderBlock,
|
2021-10-28 15:48:39 +00:00
|
|
|
translateJQueryEventToNative,
|
|
|
|
} from '@woocommerce/base-utils';
|
2021-11-19 11:47:48 +00:00
|
|
|
import { useStoreCart } from '@woocommerce/base-context/hooks';
|
2021-09-15 11:40:36 +00:00
|
|
|
import Drawer from '@woocommerce/base-components/drawer';
|
2021-10-04 03:54:28 +00:00
|
|
|
import {
|
|
|
|
formatPrice,
|
|
|
|
getCurrencyFromPriceResponse,
|
|
|
|
} from '@woocommerce/price-format';
|
|
|
|
import { getSetting } from '@woocommerce/settings';
|
2021-09-15 11:40:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-10-29 02:35:17 +00:00
|
|
|
import QuantityBadge from './quantity-badge';
|
2021-11-19 11:47:48 +00:00
|
|
|
import MiniCartContentsBlock from '../mini-cart-contents/block';
|
2021-09-15 11:40:36 +00:00
|
|
|
import './style.scss';
|
|
|
|
|
2021-11-17 15:39:07 +00:00
|
|
|
interface Props {
|
|
|
|
isInitiallyOpen?: boolean;
|
|
|
|
transparentButton: boolean;
|
|
|
|
colorClassNames?: string;
|
|
|
|
style?: Record< string, Record< string, string > >;
|
2021-11-19 11:47:48 +00:00
|
|
|
contents: string;
|
2021-11-17 15:39:07 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 11:40:36 +00:00
|
|
|
const MiniCartBlock = ( {
|
2021-10-25 16:05:01 +00:00
|
|
|
isInitiallyOpen = false,
|
2021-11-17 15:39:07 +00:00
|
|
|
colorClassNames,
|
|
|
|
style,
|
2021-11-19 11:47:48 +00:00
|
|
|
contents = '',
|
2021-11-17 15:39:07 +00:00
|
|
|
}: Props ): JSX.Element => {
|
2021-11-19 11:47:48 +00:00
|
|
|
const { cartItemsCount, cartIsLoading, cartTotals } = useStoreCart();
|
2021-10-25 16:05:01 +00:00
|
|
|
const [ isOpen, setIsOpen ] = useState< boolean >( isInitiallyOpen );
|
2021-09-15 11:40:36 +00:00
|
|
|
// We already rendered the HTML drawer placeholder, so we want to skip the
|
|
|
|
// slide in animation.
|
|
|
|
const [ skipSlideIn, setSkipSlideIn ] = useState< boolean >(
|
2021-10-25 16:05:01 +00:00
|
|
|
isInitiallyOpen
|
2021-09-15 11:40:36 +00:00
|
|
|
);
|
|
|
|
|
2021-11-19 11:47:48 +00:00
|
|
|
const contentsRef = useRef() as React.MutableRefObject< HTMLDivElement >;
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
if ( contentsRef.current instanceof Element ) {
|
|
|
|
const container = contentsRef.current.querySelector(
|
|
|
|
'.wc-block-mini-cart-contents'
|
|
|
|
);
|
|
|
|
if ( ! container ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( isOpen ) {
|
|
|
|
renderBlock( {
|
|
|
|
Block: MiniCartContentsBlock,
|
|
|
|
container,
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
unmountComponentAtNode( container );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [ isOpen ] );
|
|
|
|
|
|
|
|
useEffect( () => {
|
|
|
|
return () => {
|
|
|
|
const contentsNode = contentsRef.current as unknown;
|
|
|
|
if ( contentsNode instanceof Element ) {
|
|
|
|
const container = contentsNode.querySelector(
|
|
|
|
'.wc-block-mini-cart-contents'
|
|
|
|
);
|
|
|
|
if ( container ) {
|
|
|
|
unmountComponentAtNode( container );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, [] );
|
|
|
|
|
2021-09-15 11:40:36 +00:00
|
|
|
useEffect( () => {
|
2021-10-25 16:05:01 +00:00
|
|
|
const openMiniCart = () => {
|
2021-09-15 11:40:36 +00:00
|
|
|
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',
|
2021-10-25 16:05:01 +00:00
|
|
|
openMiniCart
|
2021-09-15 11:40:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
removeJQueryAddedToCartEvent();
|
|
|
|
|
|
|
|
document.body.removeEventListener(
|
|
|
|
'wc-blocks_added_to_cart',
|
2021-10-25 16:05:01 +00:00
|
|
|
openMiniCart
|
2021-09-15 11:40:36 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
}, [] );
|
|
|
|
|
2021-10-04 03:54:28 +00:00
|
|
|
const subTotal = getSetting( 'displayCartPricesIncludingTax', false )
|
|
|
|
? parseInt( cartTotals.total_items, 10 ) +
|
|
|
|
parseInt( cartTotals.total_items_tax, 10 )
|
2021-10-28 15:48:39 +00:00
|
|
|
: parseInt( cartTotals.total_items, 10 );
|
2021-10-04 03:54:28 +00:00
|
|
|
|
|
|
|
const ariaLabel = sprintf(
|
|
|
|
/* translators: %1$d is the number of products in the cart. %2$s is the cart total */
|
|
|
|
_n(
|
|
|
|
'%1$d item in cart, total price of %2$s',
|
|
|
|
'%1$d items in cart, total price of %2$s',
|
|
|
|
cartItemsCount,
|
|
|
|
'woo-gutenberg-products-block'
|
|
|
|
),
|
|
|
|
cartItemsCount,
|
|
|
|
formatPrice( subTotal, getCurrencyFromPriceResponse( cartTotals ) )
|
|
|
|
);
|
|
|
|
|
2021-11-17 15:39:07 +00:00
|
|
|
const colorStyle = {
|
|
|
|
backgroundColor: style?.color?.background,
|
|
|
|
color: style?.color?.text,
|
|
|
|
};
|
|
|
|
|
2021-09-15 11:40:36 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<button
|
2021-11-17 15:39:07 +00:00
|
|
|
className={ `wc-block-mini-cart__button ${ colorClassNames }` }
|
|
|
|
style={ colorStyle }
|
2021-09-15 11:40:36 +00:00
|
|
|
onClick={ () => {
|
|
|
|
if ( ! isOpen ) {
|
|
|
|
setIsOpen( true );
|
|
|
|
setSkipSlideIn( false );
|
|
|
|
}
|
|
|
|
} }
|
2021-10-04 03:54:28 +00:00
|
|
|
aria-label={ ariaLabel }
|
2021-09-15 11:40:36 +00:00
|
|
|
>
|
2021-10-29 02:35:17 +00:00
|
|
|
<span className="wc-block-mini-cart__amount">
|
|
|
|
{ formatPrice(
|
|
|
|
subTotal,
|
|
|
|
getCurrencyFromPriceResponse( cartTotals )
|
|
|
|
) }
|
|
|
|
</span>
|
2021-11-17 15:39:07 +00:00
|
|
|
<QuantityBadge
|
|
|
|
count={ cartItemsCount }
|
|
|
|
colorClassNames={ colorClassNames }
|
|
|
|
style={ colorStyle }
|
|
|
|
/>
|
2021-09-15 11:40:36 +00:00
|
|
|
</button>
|
|
|
|
<Drawer
|
2021-11-17 15:39:07 +00:00
|
|
|
className={ classnames(
|
2021-09-15 11:40:36 +00:00
|
|
|
'wc-block-mini-cart__drawer',
|
|
|
|
'is-mobile',
|
|
|
|
{
|
|
|
|
'is-loading': cartIsLoading,
|
|
|
|
}
|
|
|
|
) }
|
|
|
|
title={
|
|
|
|
cartIsLoading
|
|
|
|
? __( 'Your cart', 'woo-gutenberg-products-block' )
|
|
|
|
: 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 }
|
|
|
|
>
|
2021-11-19 11:47:48 +00:00
|
|
|
<div ref={ contentsRef }>
|
|
|
|
<RawHTML>{ contents }</RawHTML>
|
|
|
|
</div>
|
2021-09-15 11:40:36 +00:00
|
|
|
</Drawer>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default MiniCartBlock;
|