From d0bf58069f42ea9d0d74d981d73846dd9ba1f907 Mon Sep 17 00:00:00 2001 From: Rua Haszard Date: Fri, 6 Mar 2020 14:04:31 +1300 Subject: [PATCH] use quantity from store, instead of passing in to hook + + fix latent bug in useStoreCartItem - the cartItem value is now object: - was previously single-item array - (note no client code is using this at present) --- assets/js/base/hooks/use-store-cart-item.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/assets/js/base/hooks/use-store-cart-item.js b/assets/js/base/hooks/use-store-cart-item.js index 8f01e4d03d0..05c3166f890 100644 --- a/assets/js/base/hooks/use-store-cart-item.js +++ b/assets/js/base/hooks/use-store-cart-item.js @@ -20,16 +20,15 @@ import { useStoreCart } from './use-store-cart'; * @see https://github.com/woocommerce/woocommerce-gutenberg-products-block/tree/master/src/RestApi/StoreApi * * @param {string} cartItemKey Key for a cart item. - * @param {number} initialQuantity Quantity of the cart item. * @return {StoreCartItem} An object exposing data and actions relating to cart items. */ -export const useStoreCartItem = ( cartItemKey, initialQuantity ) => { +export const useStoreCartItem = ( cartItemKey ) => { const { cartItems, cartIsLoading } = useStoreCart(); - const cartItem = cartItems.filter( ( item ) => item.key === cartItemKey ); + const cartItem = cartItems.find( ( item ) => item.key === cartItemKey ); // Store quantity in hook state. This is used to keep the UI // updated while server request is updated. - const [ quantity, changeQuantity ] = useState( initialQuantity ); + const [ quantity, changeQuantity ] = useState( cartItem.quantity ); const [ debouncedQuantity ] = useDebounce( quantity, 400 ); const results = useSelect( @@ -57,7 +56,7 @@ export const useStoreCartItem = ( cartItemKey, initialQuantity ) => { // Observe debounced quantity value, fire action to update server when it changes. useEffect( () => { - if ( initialQuantity === debouncedQuantity ) return; + if ( cartItem.quantity === debouncedQuantity ) return; asyncChangeQuantity( debouncedQuantity ); }, [ debouncedQuantity ] );