Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
use quantity from store, instead of passing in to hook +
Browse files Browse the repository at this point in the history
+ 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)
  • Loading branch information
haszari committed Mar 6, 2020
1 parent 72cb01c commit d0bf580
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions assets/js/base/hooks/use-store-cart-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 ] );

Expand Down

0 comments on commit d0bf580

Please sign in to comment.