2020-02-28 02:05:10 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-03-09 02:09:47 +00:00
|
|
|
import { useSelect, useDispatch } from '@wordpress/data';
|
|
|
|
import { useState, useEffect } from '@wordpress/element';
|
2020-02-28 02:05:10 +00:00
|
|
|
import { CART_STORE_KEY as storeKey } from '@woocommerce/block-data';
|
2020-03-09 02:09:47 +00:00
|
|
|
import { useDebounce } from 'use-debounce';
|
2020-02-28 02:05:10 +00:00
|
|
|
|
2020-03-10 15:49:26 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { useStoreCart } from './use-store-cart';
|
|
|
|
|
2020-03-09 12:24:56 +00:00
|
|
|
/**
|
|
|
|
* @typedef {import('@woocommerce/type-defs/hooks').StoreCartItemQuantity} StoreCartItemQuantity
|
|
|
|
* @typedef {import('@woocommerce/type-defs/cart').CartItem} CartItem
|
|
|
|
*/
|
|
|
|
|
2020-02-28 02:05:10 +00:00
|
|
|
/**
|
|
|
|
* This is a custom hook for loading the Store API /cart/ endpoint and
|
|
|
|
* actions for removing or changing item quantity.
|
2020-02-28 11:51:30 +00:00
|
|
|
*
|
|
|
|
* @see https://github.com/woocommerce/woocommerce-gutenberg-products-block/tree/master/src/RestApi/StoreApi
|
2020-02-28 02:05:10 +00:00
|
|
|
*
|
2020-03-10 11:43:57 +00:00
|
|
|
* @param {CartItem} cartItem The cartItem to get quantity info from and
|
|
|
|
* will have quantity updated on.
|
|
|
|
* @return {StoreCartItemQuantity} An object exposing data and actions relating
|
|
|
|
* to cart items.
|
2020-02-28 02:05:10 +00:00
|
|
|
*/
|
2020-03-10 11:43:57 +00:00
|
|
|
export const useStoreCartItemQuantity = ( cartItem ) => {
|
2020-03-10 15:49:26 +00:00
|
|
|
const { cartErrors } = useStoreCart();
|
2020-03-09 02:09:47 +00:00
|
|
|
// Store quantity in hook state. This is used to keep the UI
|
|
|
|
// updated while server request is updated.
|
|
|
|
const [ quantity, changeQuantity ] = useState( cartItem.quantity );
|
|
|
|
const [ debouncedQuantity ] = useDebounce( quantity, 400 );
|
|
|
|
const isPending = useSelect(
|
|
|
|
( select ) => {
|
2020-02-28 02:05:10 +00:00
|
|
|
const store = select( storeKey );
|
2020-03-10 11:43:57 +00:00
|
|
|
return store.isItemQuantityPending( cartItem.key );
|
2020-02-28 02:05:10 +00:00
|
|
|
},
|
2020-03-10 11:43:57 +00:00
|
|
|
[ cartItem.key ]
|
2020-02-28 02:05:10 +00:00
|
|
|
);
|
2020-03-09 02:09:47 +00:00
|
|
|
|
|
|
|
const { removeItemFromCart, changeCartItemQuantity } = useDispatch(
|
|
|
|
storeKey
|
|
|
|
);
|
|
|
|
const removeItem = () => {
|
2020-03-10 11:43:57 +00:00
|
|
|
removeItemFromCart( cartItem.key );
|
2020-03-09 02:09:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Observe debounced quantity value, fire action to update server when it
|
|
|
|
// changes.
|
|
|
|
useEffect( () => {
|
2020-03-10 11:43:57 +00:00
|
|
|
changeCartItemQuantity( cartItem.key, debouncedQuantity );
|
|
|
|
}, [ debouncedQuantity, cartItem.key ] );
|
2020-02-28 02:05:10 +00:00
|
|
|
|
|
|
|
return {
|
2020-03-09 02:09:47 +00:00
|
|
|
isPending,
|
|
|
|
quantity,
|
|
|
|
changeQuantity,
|
|
|
|
removeItem,
|
2020-03-10 15:49:26 +00:00
|
|
|
cartItemQuantityErrors: cartErrors,
|
2020-02-28 02:05:10 +00:00
|
|
|
};
|
|
|
|
};
|