2020-02-28 02:05:10 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2020-03-09 02:09:47 +00:00
|
|
|
import { useSelect, useDispatch } from '@wordpress/data';
|
2021-03-10 15:03:26 +00:00
|
|
|
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-04-07 11:03:22 +00:00
|
|
|
import { useCheckoutContext } from '@woocommerce/base-context';
|
2020-04-24 13:41:32 +00:00
|
|
|
import { triggerFragmentRefresh } from '@woocommerce/base-utils';
|
2021-03-10 15:03:26 +00:00
|
|
|
import type { CartItem, StoreCartItemQuantity } from '@woocommerce/types';
|
2021-03-16 11:40:22 +00:00
|
|
|
|
2020-03-10 15:49:26 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { useStoreCart } from './use-store-cart';
|
2021-01-19 15:55:44 +00:00
|
|
|
import { usePrevious } from '../use-previous';
|
2021-03-16 11:40:22 +00:00
|
|
|
|
2020-02-28 02:05:10 +00:00
|
|
|
/**
|
2021-03-16 11:40:22 +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
|
|
|
*
|
2020-09-26 17:28:16 +00:00
|
|
|
* @see https://github.com/woocommerce/woocommerce-gutenberg-products-block/tree/trunk/src/RestApi/StoreApi
|
2020-02-28 02:05:10 +00:00
|
|
|
*
|
2021-03-16 11:40:22 +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
|
|
|
*/
|
2021-03-10 15:03:26 +00:00
|
|
|
export const useStoreCartItemQuantity = (
|
|
|
|
cartItem: CartItem
|
|
|
|
): StoreCartItemQuantity => {
|
2020-03-17 15:34:33 +00:00
|
|
|
const { key: cartItemKey = '', quantity: cartItemQuantity = 1 } = cartItem;
|
2020-03-10 15:49:26 +00:00
|
|
|
const { cartErrors } = useStoreCart();
|
2020-04-07 11:03:22 +00:00
|
|
|
const { dispatchActions } = useCheckoutContext();
|
|
|
|
|
2021-03-16 11:40:22 +00:00
|
|
|
// Store quantity in hook state. This is used to keep the UI updated while server request is updated.
|
|
|
|
const [ quantity, setQuantity ] = useState< number >( cartItemQuantity );
|
2021-03-10 15:03:26 +00:00
|
|
|
const [ debouncedQuantity ] = useDebounce< number >( quantity, 400 );
|
2020-03-10 16:50:18 +00:00
|
|
|
const previousDebouncedQuantity = usePrevious( debouncedQuantity );
|
2020-03-17 15:34:33 +00:00
|
|
|
const { removeItemFromCart, changeCartItemQuantity } = useDispatch(
|
|
|
|
storeKey
|
|
|
|
);
|
|
|
|
|
2021-03-16 11:40:22 +00:00
|
|
|
// Track when things are already pending updates.
|
|
|
|
const isPending = useSelect(
|
2020-03-09 02:09:47 +00:00
|
|
|
( select ) => {
|
2020-03-17 15:34:33 +00:00
|
|
|
if ( ! cartItemKey ) {
|
2021-03-16 11:40:22 +00:00
|
|
|
return {
|
|
|
|
quantity: false,
|
|
|
|
delete: false,
|
|
|
|
};
|
2020-03-17 15:34:33 +00:00
|
|
|
}
|
2020-02-28 02:05:10 +00:00
|
|
|
const store = select( storeKey );
|
2021-03-16 11:40:22 +00:00
|
|
|
return {
|
|
|
|
quantity: store.isItemPendingQuantity( cartItemKey ),
|
|
|
|
delete: store.isItemPendingDelete( cartItemKey ),
|
|
|
|
};
|
2020-02-28 02:05:10 +00:00
|
|
|
},
|
2020-03-17 15:34:33 +00:00
|
|
|
[ cartItemKey ]
|
2020-02-28 02:05:10 +00:00
|
|
|
);
|
2021-03-16 11:40:22 +00:00
|
|
|
const previousIsPending = usePrevious( isPending );
|
2020-04-24 13:41:32 +00:00
|
|
|
|
2021-03-10 15:03:26 +00:00
|
|
|
const removeItem = () => {
|
2020-04-24 13:41:32 +00:00
|
|
|
return cartItemKey
|
|
|
|
? removeItemFromCart( cartItemKey ).then( () => {
|
|
|
|
triggerFragmentRefresh();
|
2021-03-16 11:40:22 +00:00
|
|
|
return true;
|
2020-04-24 13:41:32 +00:00
|
|
|
} )
|
2021-03-16 11:40:22 +00:00
|
|
|
: Promise.resolve( false );
|
2021-03-10 15:03:26 +00:00
|
|
|
};
|
2020-03-09 02:09:47 +00:00
|
|
|
|
2020-03-17 15:34:33 +00:00
|
|
|
// Observe debounced quantity value, fire action to update server on change.
|
2020-03-09 02:09:47 +00:00
|
|
|
useEffect( () => {
|
2021-03-16 12:23:33 +00:00
|
|
|
if (
|
|
|
|
cartItemKey &&
|
|
|
|
Number.isFinite( previousDebouncedQuantity ) &&
|
|
|
|
previousDebouncedQuantity !== debouncedQuantity
|
|
|
|
) {
|
2020-04-24 13:41:32 +00:00
|
|
|
changeCartItemQuantity( cartItemKey, debouncedQuantity ).then(
|
|
|
|
triggerFragmentRefresh
|
|
|
|
);
|
2020-03-10 16:50:18 +00:00
|
|
|
}
|
2020-10-27 14:37:18 +00:00
|
|
|
}, [
|
|
|
|
cartItemKey,
|
|
|
|
changeCartItemQuantity,
|
|
|
|
debouncedQuantity,
|
|
|
|
previousDebouncedQuantity,
|
|
|
|
] );
|
2020-04-24 13:41:32 +00:00
|
|
|
|
2020-04-07 11:03:22 +00:00
|
|
|
useEffect( () => {
|
2021-03-16 11:40:22 +00:00
|
|
|
if ( typeof previousIsPending === 'undefined' ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( previousIsPending.quantity !== isPending.quantity ) {
|
|
|
|
if ( isPending.quantity ) {
|
2020-10-27 14:37:18 +00:00
|
|
|
dispatchActions.incrementCalculating();
|
|
|
|
} else {
|
|
|
|
dispatchActions.decrementCalculating();
|
|
|
|
}
|
2020-04-07 11:03:22 +00:00
|
|
|
}
|
2021-03-16 11:40:22 +00:00
|
|
|
if ( previousIsPending.delete !== isPending.delete ) {
|
|
|
|
if ( isPending.delete ) {
|
2020-10-27 14:37:18 +00:00
|
|
|
dispatchActions.incrementCalculating();
|
|
|
|
} else {
|
|
|
|
dispatchActions.decrementCalculating();
|
|
|
|
}
|
2020-04-07 11:03:22 +00:00
|
|
|
}
|
|
|
|
return () => {
|
2021-03-16 11:40:22 +00:00
|
|
|
if ( isPending.quantity ) {
|
|
|
|
dispatchActions.decrementCalculating();
|
|
|
|
}
|
|
|
|
if ( isPending.delete ) {
|
2020-04-07 11:03:22 +00:00
|
|
|
dispatchActions.decrementCalculating();
|
|
|
|
}
|
|
|
|
};
|
2021-03-16 11:40:22 +00:00
|
|
|
}, [ dispatchActions, isPending, previousIsPending ] );
|
2020-02-28 02:05:10 +00:00
|
|
|
|
|
|
|
return {
|
2021-03-16 11:40:22 +00:00
|
|
|
isPendingDelete: isPending.delete,
|
2020-03-09 02:09:47 +00:00
|
|
|
quantity,
|
2021-03-16 11:40:22 +00:00
|
|
|
setItemQuantity: setQuantity,
|
2020-03-09 02:09:47 +00:00
|
|
|
removeItem,
|
2020-03-10 15:49:26 +00:00
|
|
|
cartItemQuantityErrors: cartErrors,
|
2020-02-28 02:05:10 +00:00
|
|
|
};
|
|
|
|
};
|