Skip to content

Commit c1bf298

Browse files
committed
fix(cart): sync active cart cache key
1 parent 20b3751 commit c1bf298

1 file changed

Lines changed: 53 additions & 1 deletion

File tree

libs/storefront-data/src/cart/hooks.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
useQueryClient,
55
useSuspenseQuery,
66
} from "@tanstack/react-query"
7-
import { useCallback } from "react"
7+
import { useCallback, useEffect } from "react"
88
import { createCacheConfig, type CacheConfig } from "../shared/cache-config"
99
import type { QueryNamespace } from "../shared/query-keys"
1010
import type { RegionInfo } from "../shared/region"
@@ -391,6 +391,7 @@ export function createCartHooks<
391391
}
392392

393393
function useCart(input: CartInputBase): UseCartResult<TCart> {
394+
const queryClient = useQueryClient()
394395
const region = resolveRegion ? resolveRegion() : null
395396
const resolvedInput = applyRegion(input, region ?? undefined)
396397
const cartId = resolveCartId(resolvedInput.cartId)
@@ -414,6 +415,31 @@ export function createCartHooks<
414415
const cart = data ?? null
415416
const itemCount = getItemCount(cart)
416417

418+
useEffect(() => {
419+
if (!cart?.id) {
420+
return
421+
}
422+
const nextRegionId = cart.region_id ?? resolvedInput.region_id ?? null
423+
const nextKey = resolvedQueryKeys.active({
424+
cartId: cart.id,
425+
regionId: nextRegionId,
426+
})
427+
queryClient.setQueryData(nextKey, cart)
428+
if (cartId !== cart.id) {
429+
const previousKey = resolvedQueryKeys.active({
430+
cartId,
431+
regionId: resolvedInput.region_id ?? null,
432+
})
433+
queryClient.removeQueries({ queryKey: previousKey, exact: true })
434+
}
435+
}, [
436+
cart,
437+
cartId,
438+
queryClient,
439+
resolvedInput.region_id,
440+
resolvedQueryKeys,
441+
])
442+
417443
return {
418444
cart,
419445
isLoading,
@@ -428,6 +454,7 @@ export function createCartHooks<
428454
}
429455

430456
function useSuspenseCart(input: CartInputBase): UseSuspenseCartResult<TCart> {
457+
const queryClient = useQueryClient()
431458
const region = resolveRegionSuspense
432459
? resolveRegionSuspense()
433460
: resolveRegion?.()
@@ -451,6 +478,31 @@ export function createCartHooks<
451478
const cart = data ?? null
452479
const itemCount = getItemCount(cart)
453480

481+
useEffect(() => {
482+
if (!cart?.id) {
483+
return
484+
}
485+
const nextRegionId = cart.region_id ?? resolvedInput.region_id ?? null
486+
const nextKey = resolvedQueryKeys.active({
487+
cartId: cart.id,
488+
regionId: nextRegionId,
489+
})
490+
queryClient.setQueryData(nextKey, cart)
491+
if (cartId !== cart.id) {
492+
const previousKey = resolvedQueryKeys.active({
493+
cartId,
494+
regionId: resolvedInput.region_id ?? null,
495+
})
496+
queryClient.removeQueries({ queryKey: previousKey, exact: true })
497+
}
498+
}, [
499+
cart,
500+
cartId,
501+
queryClient,
502+
resolvedInput.region_id,
503+
resolvedQueryKeys,
504+
])
505+
454506
return {
455507
cart,
456508
isFetching,

0 commit comments

Comments
 (0)