2020-04-06 10:36:28 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { useState, useEffect, useRef } from '@wordpress/element';
|
|
|
|
import { useDispatch } from '@wordpress/data';
|
|
|
|
import { CART_STORE_KEY as storeKey } from '@woocommerce/block-data';
|
2020-04-27 16:24:54 +00:00
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { useStoreNotices } from './use-store-notices';
|
2021-01-19 15:55:44 +00:00
|
|
|
import { useStoreCart } from './cart';
|
2020-04-06 10:36:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {import('@woocommerce/type-defs/hooks').StoreCartItemAddToCart} StoreCartItemAddToCart
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the quantity of a product in the cart.
|
|
|
|
*
|
|
|
|
* @param {Object} cartItems Array of items.
|
|
|
|
* @param {number} productId The product id to look for.
|
|
|
|
* @return {number} Quantity in the cart.
|
|
|
|
*/
|
|
|
|
const getQuantityFromCartItems = ( cartItems, productId ) => {
|
2020-04-24 12:23:25 +00:00
|
|
|
const productItem = cartItems.find( ( { id } ) => id === productId );
|
2020-04-06 10:36:28 +00:00
|
|
|
|
|
|
|
return productItem ? productItem.quantity : 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A custom hook for exposing cart related data for a given product id and an
|
|
|
|
* action for adding a single quantity of the product _to_ the cart.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param {number} productId The product id to be added to the cart.
|
|
|
|
*
|
|
|
|
* @return {StoreCartItemAddToCart} An object exposing data and actions relating
|
|
|
|
* to add to cart functionality.
|
|
|
|
*/
|
|
|
|
export const useStoreAddToCart = ( productId ) => {
|
2020-04-17 09:24:44 +00:00
|
|
|
const { addItemToCart } = useDispatch( storeKey );
|
2020-04-06 10:36:28 +00:00
|
|
|
const { cartItems, cartIsLoading } = useStoreCart();
|
2020-04-27 16:24:54 +00:00
|
|
|
const { addErrorNotice, removeNotice } = useStoreNotices();
|
2020-04-17 09:24:44 +00:00
|
|
|
|
|
|
|
const [ addingToCart, setAddingToCart ] = useState( false );
|
2020-04-06 10:36:28 +00:00
|
|
|
const currentCartItemQuantity = useRef(
|
|
|
|
getQuantityFromCartItems( cartItems, productId )
|
|
|
|
);
|
|
|
|
|
2020-06-17 09:52:03 +00:00
|
|
|
const addToCart = ( quantity = 1 ) => {
|
2020-04-06 10:36:28 +00:00
|
|
|
setAddingToCart( true );
|
2020-06-17 09:52:03 +00:00
|
|
|
addItemToCart( productId, quantity )
|
2020-04-27 16:24:54 +00:00
|
|
|
.then( ( result ) => {
|
|
|
|
if ( result === true ) {
|
|
|
|
removeNotice( 'add-to-cart' );
|
|
|
|
}
|
|
|
|
} )
|
|
|
|
.catch( ( error ) => {
|
|
|
|
addErrorNotice( decodeEntities( error.message ), {
|
|
|
|
context: 'wc/all-products',
|
|
|
|
id: 'add-to-cart',
|
|
|
|
isDismissible: true,
|
|
|
|
} );
|
|
|
|
} )
|
|
|
|
.finally( () => {
|
|
|
|
setAddingToCart( false );
|
|
|
|
} );
|
2020-04-06 10:36:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
useEffect( () => {
|
2020-04-17 09:24:44 +00:00
|
|
|
const quantity = getQuantityFromCartItems( cartItems, productId );
|
2020-04-06 10:36:28 +00:00
|
|
|
|
2020-04-17 09:24:44 +00:00
|
|
|
if ( quantity !== currentCartItemQuantity.current ) {
|
|
|
|
currentCartItemQuantity.current = quantity;
|
2020-04-06 10:36:28 +00:00
|
|
|
}
|
|
|
|
}, [ cartItems, productId ] );
|
|
|
|
|
|
|
|
return {
|
2020-06-17 09:52:03 +00:00
|
|
|
cartQuantity: Number.isFinite( currentCartItemQuantity.current )
|
|
|
|
? currentCartItemQuantity.current
|
|
|
|
: 0,
|
2020-04-06 10:36:28 +00:00
|
|
|
addingToCart,
|
|
|
|
cartIsLoading,
|
|
|
|
addToCart,
|
|
|
|
};
|
|
|
|
};
|