mirror of
https://github.com/vercel/commerce.git
synced 2025-07-03 11:41:21 +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 useCommerceCart, { UseCart } from '@commerce/cart/use-cart'
|
||||
|
||||
import { SWRHook } from '@commerce/utils/types'
|
||||
import { GetCartHook } from '../types/cart'
|
||||
import { GetCartQueryVariables, QueryRoot } from '../schema'
|
||||
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>
|
||||
|
||||
@ -14,12 +15,16 @@ export const handler: SWRHook<GetCartHook> = {
|
||||
},
|
||||
async fetcher({ input: { cartId }, options, fetch }) {
|
||||
if (cartId) {
|
||||
const { cart } = await fetch<QueryRoot, GetCartQueryVariables>({
|
||||
let { cart } = await fetch<QueryRoot, GetCartQueryVariables>({
|
||||
...options,
|
||||
variables: { cartId },
|
||||
})
|
||||
setCheckoutUrlCookie(cart?.checkoutUrl)
|
||||
if (cart) {
|
||||
setCheckoutUrlCookie(cart.checkoutUrl)
|
||||
return normalizeCart(cart)
|
||||
} else {
|
||||
Cookies.remove(SHOPIFY_CART_ID_COOKIE)
|
||||
}
|
||||
}
|
||||
return null
|
||||
},
|
||||
|
Loading…
x
Reference in New Issue
Block a user