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:
cond0r 2021-08-10 16:37:22 +03:00
parent 4704e2eac3
commit 3364d13451

View File

@ -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) {
setCheckoutUrlCookie(cart.checkoutUrl)
return normalizeCart(cart) return normalizeCart(cart)
} else {
Cookies.remove(SHOPIFY_CART_ID_COOKIE)
}
} }
return null return null
}, },