2021-09-15 11:40:36 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-12-10 11:11:59 +00:00
|
|
|
import { renderParentBlock } from '@woocommerce/atomic-utils';
|
2021-09-15 11:40:36 +00:00
|
|
|
import Drawer from '@woocommerce/base-components/drawer';
|
2021-12-10 11:11:59 +00:00
|
|
|
import { useStoreCart } from '@woocommerce/base-context/hooks';
|
|
|
|
import { translateJQueryEventToNative } from '@woocommerce/base-utils';
|
|
|
|
import { getRegisteredBlockComponents } from '@woocommerce/blocks-registry';
|
2021-10-04 03:54:28 +00:00
|
|
|
import {
|
|
|
|
formatPrice,
|
|
|
|
getCurrencyFromPriceResponse,
|
|
|
|
} from '@woocommerce/price-format';
|
2021-12-03 09:45:06 +00:00
|
|
|
import { getSettingWithCoercion } from '@woocommerce/settings';
|
2021-12-10 11:11:59 +00:00
|
|
|
import { isBoolean, isString } from '@woocommerce/types';
|
|
|
|
import {
|
|
|
|
unmountComponentAtNode,
|
|
|
|
useCallback,
|
|
|
|
useEffect,
|
|
|
|
useState,
|
|
|
|
} from '@wordpress/element';
|
2021-12-20 07:57:55 +00:00
|
|
|
import { sprintf, _n } from '@wordpress/i18n';
|
2021-12-10 11:11:59 +00:00
|
|
|
import classnames from 'classnames';
|
2021-09-15 11:40:36 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-10-29 02:35:17 +00:00
|
|
|
import QuantityBadge from './quantity-badge';
|
2021-12-10 11:11:59 +00:00
|
|
|
import { MiniCartContentsBlock } from '../mini-cart-contents/block';
|
2021-09-15 11:40:36 +00:00
|
|
|
import './style.scss';
|
2021-12-10 11:11:59 +00:00
|
|
|
import { blockName } from '../mini-cart-contents/attributes';
|
2021-09-15 11:40:36 +00:00
|
|
|
|
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-12-03 10:37:15 +00:00
|
|
|
const [ contentsNode, setContentsNode ] = useState< HTMLDivElement | null >(
|
|
|
|
null
|
|
|
|
);
|
2021-09-15 11:40:36 +00:00
|
|
|
|
2021-12-03 10:37:15 +00:00
|
|
|
const contentsRef = useCallback( ( node ) => {
|
|
|
|
setContentsNode( node );
|
|
|
|
}, [] );
|
2021-11-19 11:47:48 +00:00
|
|
|
|
|
|
|
useEffect( () => {
|
2021-12-03 10:37:15 +00:00
|
|
|
if ( contentsNode instanceof Element ) {
|
|
|
|
const container = contentsNode.querySelector(
|
2021-12-10 11:11:59 +00:00
|
|
|
'.wp-block-woocommerce-mini-cart-contents'
|
|
|
|
);
|
2021-11-19 11:47:48 +00:00
|
|
|
if ( ! container ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( isOpen ) {
|
2021-12-10 11:11:59 +00:00
|
|
|
renderParentBlock( {
|
2021-11-19 11:47:48 +00:00
|
|
|
Block: MiniCartContentsBlock,
|
2021-12-10 11:11:59 +00:00
|
|
|
blockName,
|
|
|
|
selector: '.wp-block-woocommerce-mini-cart-contents',
|
|
|
|
blockMap: getRegisteredBlockComponents( blockName ),
|
2021-11-19 11:47:48 +00:00
|
|
|
} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
2021-12-03 10:37:15 +00:00
|
|
|
if ( contentsNode instanceof Element && isOpen ) {
|
2021-11-19 11:47:48 +00:00
|
|
|
const container = contentsNode.querySelector(
|
2021-12-10 11:11:59 +00:00
|
|
|
'.wp-block-woocommerce-mini-cart-contents'
|
2021-11-19 11:47:48 +00:00
|
|
|
);
|
|
|
|
if ( container ) {
|
|
|
|
unmountComponentAtNode( container );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2021-12-03 10:37:15 +00:00
|
|
|
}, [ isOpen, contentsNode ] );
|
2021-11-19 11:47:48 +00:00
|
|
|
|
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-12-03 09:45:06 +00:00
|
|
|
const showIncludingTax = getSettingWithCoercion(
|
|
|
|
'displayCartPricesIncludingTax',
|
|
|
|
false,
|
|
|
|
isBoolean
|
|
|
|
);
|
|
|
|
|
|
|
|
const taxLabel = getSettingWithCoercion( 'taxLabel', '', isString );
|
|
|
|
|
|
|
|
const subTotal = showIncludingTax
|
2021-10-04 03:54:28 +00:00
|
|
|
? 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-12-03 09:45:06 +00:00
|
|
|
{ taxLabel !== '' && subTotal !== 0 && (
|
|
|
|
<small className="wc-block-mini-cart__tax-label">
|
|
|
|
{ taxLabel }
|
|
|
|
</small>
|
|
|
|
) }
|
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,
|
|
|
|
}
|
|
|
|
) }
|
2021-12-20 07:57:55 +00:00
|
|
|
title=""
|
2021-09-15 11:40:36 +00:00
|
|
|
isOpen={ isOpen }
|
|
|
|
onClose={ () => {
|
|
|
|
setIsOpen( false );
|
|
|
|
} }
|
|
|
|
slideIn={ ! skipSlideIn }
|
|
|
|
>
|
2021-12-20 07:57:55 +00:00
|
|
|
<div
|
|
|
|
className="wc-block-mini-cart__template-part"
|
|
|
|
ref={ contentsRef }
|
2022-02-14 16:39:04 +00:00
|
|
|
dangerouslySetInnerHTML={ { __html: contents } }
|
|
|
|
></div>
|
2021-09-15 11:40:36 +00:00
|
|
|
</Drawer>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default MiniCartBlock;
|