woocommerce/plugins/woocommerce-blocks/assets/js/base/hooks/use-store-cart.js

70 lines
1.8 KiB
JavaScript

/** @typedef { import('@woocommerce/type-defs/hooks').StoreCart } StoreCart */
/**
* External dependencies
*/
import { CART_STORE_KEY as storeKey } from '@woocommerce/block-data';
import { useSelect } from '@wordpress/data';
/**
* @constant
* @type {StoreCart} Object containing cart data.
*/
const defaultCartData = {
cartCoupons: [],
cartItems: [],
cartItemsCount: 0,
cartItemsWeight: 0,
cartNeedsShipping: true,
cartTotals: {},
cartIsLoading: true,
cartErrors: [],
};
/**
* This is a custom hook that is wired up to the `wc/store/cart` data
* store.
*
* @param {Object} options An object declaring the various
* collection arguments.
* @param {boolean} options.shouldSelect If false, the previous results will be
* returned and internal selects will not
* fire.
*
* @return {StoreCart} Object containing cart data.
*/
export const useStoreCart = ( options = { shouldSelect: true } ) => {
const { shouldSelect } = options;
const results = useSelect(
( select ) => {
if ( ! shouldSelect ) {
return null;
}
const store = select( storeKey );
const cartData = store.getCartData();
const cartErrors = store.getCartErrors();
const cartTotals = store.getCartTotals();
const cartIsLoading = ! store.hasFinishedResolution(
'getCartData'
);
return {
cartCoupons: cartData.coupons,
cartItems: cartData.items,
cartItemsCount: cartData.itemsCount,
cartItemsWeight: cartData.itemsWeight,
cartNeedsShipping: cartData.needsShipping,
cartTotals,
cartIsLoading,
cartErrors,
};
},
[ shouldSelect ]
);
if ( results === null ) {
return defaultCartData;
}
return results;
};