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

Get latest cart when triggering checkout events #10254

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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