From 3364d13451cd0436160f66fb67b4aef34277d25b Mon Sep 17 00:00:00 2001 From: cond0r Date: Tue, 10 Aug 2021 16:37:22 +0300 Subject: [PATCH] Remove cart cookie when response is null It means cart id is no longer valid, after cart expiration or checkout complete --- framework/shopify/cart/use-cart.tsx | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/framework/shopify/cart/use-cart.tsx b/framework/shopify/cart/use-cart.tsx index 7b4d5c314..22db9fb80 100644 --- a/framework/shopify/cart/use-cart.tsx +++ b/framework/shopify/cart/use-cart.tsx @@ -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 @@ -14,12 +15,16 @@ export const handler: SWRHook = { }, async fetcher({ input: { cartId }, options, fetch }) { if (cartId) { - const { cart } = await fetch({ + let { cart } = await fetch({ ...options, variables: { cartId }, }) - setCheckoutUrlCookie(cart?.checkoutUrl) - return normalizeCart(cart) + if (cart) { + setCheckoutUrlCookie(cart.checkoutUrl) + return normalizeCart(cart) + } else { + Cookies.remove(SHOPIFY_CART_ID_COOKIE) + } } return null },