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

Commit

Permalink
Get latest cart when triggering events (#10254)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejolley authored Jul 19, 2023
1 parent aa34f43 commit 9e46daf
Showing 1 changed file with 3 additions and 16 deletions.
19 changes: 3 additions & 16 deletions assets/js/base/context/hooks/use-store-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
* External dependencies
*/
import { doAction } from '@wordpress/hooks';
import { useCallback, useRef, useEffect } from '@wordpress/element';

/**
* Internal dependencies
*/
import { useStoreCart } from './cart/use-store-cart';
import { select } from '@wordpress/data';
import { useCallback } from '@wordpress/element';

type StoreEvent = (
eventName: string,
Expand All @@ -21,15 +17,6 @@ export const useStoreEvents = (): {
dispatchStoreEvent: StoreEvent;
dispatchCheckoutEvent: StoreEvent;
} => {
const storeCart = useStoreCart();
const currentStoreCart = useRef( storeCart );

// Track the latest version of the cart so we can use the current value in our callback function below without triggering
// other useEffect hooks using dispatchCheckoutEvent as a dependency.
useEffect( () => {
currentStoreCart.current = storeCart;
}, [ storeCart ] );

const dispatchStoreEvent = useCallback( ( eventName, eventParams = {} ) => {
try {
doAction(
Expand All @@ -50,7 +37,7 @@ export const useStoreEvents = (): {
`experimental__woocommerce_blocks-checkout-${ eventName }`,
{
...eventParams,
storeCart: currentStoreCart.current,
storeCart: select( 'wc/store/cart' ).getCartData(),
}
);
} catch ( e ) {
Expand Down

0 comments on commit 9e46daf

Please sign in to comment.