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';
|
2021-03-10 15:03:26 +00:00
|
|
|
import type { CartItem } from '@woocommerce/types';
|
2021-04-08 12:31:12 +00:00
|
|
|
|
2020-04-27 16:24:54 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2021-04-08 12:31:12 +00:00
|
|
|
import { useStoreCart } from './cart/use-store-cart';
|
2020-04-06 10:36:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {import('@woocommerce/type-defs/hooks').StoreCartItemAddToCart} StoreCartItemAddToCart
|
|
|
|
*/
|
|
|
|
|
2021-03-10 15:03:26 +00:00
|
|
|
interface StoreAddToCart {
|
|
|
|
cartQuantity: number;
|
|
|
|
addingToCart: boolean;
|
|
|
|
cartIsLoading: boolean;
|
2021-03-16 11:40:22 +00:00
|
|
|
addToCart: ( quantity?: number ) => Promise< boolean >;
|
2021-03-10 15:03:26 +00:00
|
|
|
}
|
2020-04-06 10:36:28 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-03-10 15:03:26 +00:00
|
|
|
const getQuantityFromCartItems = (
|
|
|
|
cartItems: Array< CartItem >,
|
|
|
|
productId: number
|
|
|
|
): number => {
|
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.
|
|
|
|
*/
|
2021-03-10 15:03:26 +00:00
|
|
|
export const useStoreAddToCart = ( productId: number ): StoreAddToCart => {
|
2020-04-17 09:24:44 +00:00
|
|
|
const { addItemToCart } = useDispatch( storeKey );
|
2020-04-06 10:36:28 +00:00
|
|
|
const { cartItems, cartIsLoading } = useStoreCart();
|
2022-04-08 12:11:50 +00:00
|
|
|
const { createErrorNotice, removeNotice } = useDispatch( 'core/notices' );
|
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 );
|
2021-03-16 11:40:22 +00:00
|
|
|
return addItemToCart( productId, quantity )
|
|
|
|
.then( () => {
|
|
|
|
removeNotice( 'add-to-cart' );
|
2020-04-27 16:24:54 +00:00
|
|
|
} )
|
|
|
|
.catch( ( error ) => {
|
2022-04-08 12:11:50 +00:00
|
|
|
createErrorNotice( decodeEntities( error.message ), {
|
2020-04-27 16:24:54 +00:00
|
|
|
id: 'add-to-cart',
|
2022-04-08 12:11:50 +00:00
|
|
|
context: 'wc/all-products',
|
2020-04-27 16:24:54 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
};
|