Skip to content

Commit 0a0fbdf

Browse files
committed
fix(storefront-data): avoid hooks-in-callback by resolving region in cart/product hooks
1 parent 9bb074d commit 0a0fbdf

2 files changed

Lines changed: 5 additions & 7 deletions

File tree

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,14 +339,15 @@ export function createCartHooks<
339339
}
340340

341341
function useAddLineItem(options?: CartMutationOptions<TCart, TAddInput>) {
342+
const region = resolveRegion ? resolveRegion() : null
342343
const queryClient = useQueryClient()
343344
return useMutation({
344345
mutationFn: async (input: TAddInput) => {
345346
if (!service.addLineItem) {
346347
throw new Error("addLineItem service is not configured")
347348
}
348349

349-
const resolvedInput = applyRegion(input, resolveRegion?.() ?? undefined)
350+
const resolvedInput = applyRegion(input, region ?? undefined)
350351
let cartId = resolveCartId(resolvedInput.cartId)
351352
const autoCreate = resolvedInput.autoCreate ?? true
352353
const canCreate =
@@ -518,11 +519,11 @@ export function createCartHooks<
518519
skipIfCached?: boolean
519520
}) {
520521
const queryClient = useQueryClient()
522+
const region = resolveRegion ? resolveRegion() : null
521523
const cacheStrategy = options?.cacheStrategy ?? "realtime"
522524
const skipIfCached = options?.skipIfCached ?? true
523525

524526
const prefetchCart = async (input: CartInputBase) => {
525-
const region = resolveRegion ? resolveRegion() : null
526527
const resolvedInput = applyRegion(input, region ?? undefined)
527528
const cartId = resolveCartId(resolvedInput.cartId)
528529
const autoCreate = resolvedInput.autoCreate ?? true

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ export function createProductHooks<
261261
skipIfCached?: boolean
262262
}) {
263263
const queryClient = useQueryClient()
264+
const region = resolveRegion ? resolveRegion() : null
264265
const timeoutsRef = useRef<Map<string, ReturnType<typeof setTimeout>>>(
265266
new Map()
266267
)
@@ -272,7 +273,6 @@ export function createProductHooks<
272273
input: TListInput,
273274
prefetchOptions?: PrefetchListOptions
274275
) => {
275-
const region = resolveRegion ? resolveRegion() : null
276276
const resolvedInput = applyRegion(input, region ?? undefined)
277277
if (requireRegion && !resolvedInput.region_id) {
278278
return
@@ -306,7 +306,6 @@ export function createProductHooks<
306306
input: TListInput,
307307
prefetchOptions?: PrefetchListOptions
308308
) => {
309-
const region = resolveRegion ? resolveRegion() : null
310309
const resolvedInput = applyRegion(input, region ?? undefined)
311310
if (requireRegion && !resolvedInput.region_id) {
312311
return
@@ -341,7 +340,6 @@ export function createProductHooks<
341340
delay = defaultDelay,
342341
prefetchId?: string
343342
) => {
344-
const region = resolveRegion ? resolveRegion() : null
345343
const resolvedInput = applyRegion(input, region ?? undefined)
346344
const listParams = buildList(resolvedInput)
347345
const queryKey = resolvedQueryKeys.list(listParams)
@@ -382,6 +380,7 @@ export function createProductHooks<
382380
skipIfCached?: boolean
383381
}) {
384382
const queryClient = useQueryClient()
383+
const region = resolveRegion ? resolveRegion() : null
385384
const timeoutsRef = useRef<Map<string, ReturnType<typeof setTimeout>>>(
386385
new Map()
387386
)
@@ -393,7 +392,6 @@ export function createProductHooks<
393392
input: TDetailInput,
394393
prefetchOptions?: PrefetchProductOptions
395394
) => {
396-
const region = resolveRegion ? resolveRegion() : null
397395
const resolvedInput = applyRegion(input, region ?? undefined)
398396
if (requireRegion && !resolvedInput.region_id) {
399397
return
@@ -425,7 +423,6 @@ export function createProductHooks<
425423
delay = defaultDelay,
426424
prefetchId?: string
427425
) => {
428-
const region = resolveRegion ? resolveRegion() : null
429426
const resolvedInput = applyRegion(input, region ?? undefined)
430427
const detailParams = buildDetail(resolvedInput)
431428
const queryKey = resolvedQueryKeys.detail(detailParams)

0 commit comments

Comments
 (0)