mirror of
https://github.com/vercel/commerce.git
synced 2025-07-03 19:51:22 +00:00
Remove cart cookie when response is null
It means cart id is no longer valid, after cart expiration or checkout complete
This commit is contained in:
parent
4704e2eac3
commit
3364d13451
@ -1,10 +1,11 @@
|
|||||||
import { useMemo } from 'react'
|
import { useMemo } from 'react'
|
||||||
import useCommerceCart, { UseCart } from '@commerce/cart/use-cart'
|
import useCommerceCart, { UseCart } from '@commerce/cart/use-cart'
|
||||||
|
|
||||||
import { SWRHook } from '@commerce/utils/types'
|
import { SWRHook } from '@commerce/utils/types'
|
||||||
import { GetCartHook } from '../types/cart'
|
import { GetCartHook } from '../types/cart'
|
||||||
import { GetCartQueryVariables, QueryRoot } from '../schema'
|
import { GetCartQueryVariables, QueryRoot } from '../schema'
|
||||||
import { normalizeCart, getCartQuery, setCheckoutUrlCookie } from '../utils'
|
import { normalizeCart, getCartQuery, setCheckoutUrlCookie } from '../utils'
|
||||||
|
import Cookies from 'js-cookie'
|
||||||
|
import { SHOPIFY_CART_ID_COOKIE } from '@framework/const'
|
||||||
|
|
||||||
export default useCommerceCart as UseCart<typeof handler>
|
export default useCommerceCart as UseCart<typeof handler>
|
||||||
|
|
||||||
@ -14,12 +15,16 @@ export const handler: SWRHook<GetCartHook> = {
|
|||||||
},
|
},
|
||||||
async fetcher({ input: { cartId }, options, fetch }) {
|
async fetcher({ input: { cartId }, options, fetch }) {
|
||||||
if (cartId) {
|
if (cartId) {
|
||||||
const { cart } = await fetch<QueryRoot, GetCartQueryVariables>({
|
let { cart } = await fetch<QueryRoot, GetCartQueryVariables>({
|
||||||
...options,
|
...options,
|
||||||
variables: { cartId },
|
variables: { cartId },
|
||||||
})
|
})
|
||||||
setCheckoutUrlCookie(cart?.checkoutUrl)
|
if (cart) {
|
||||||
return normalizeCart(cart)
|
setCheckoutUrlCookie(cart.checkoutUrl)
|
||||||
|
return normalizeCart(cart)
|
||||||
|
} else {
|
||||||
|
Cookies.remove(SHOPIFY_CART_ID_COOKIE)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user