mirror of
https://github.com/vercel/commerce.git
synced 2025-06-07 16:56:59 +00:00
Update use-cart.tsx
This commit is contained in:
parent
10f9d193fd
commit
f8a1fb3d7f
@ -6,28 +6,27 @@ import { getCartQuery, normalizeCart } from '../utils'
|
|||||||
import { GetCartHook } from '../types/cart'
|
import { GetCartHook } from '../types/cart'
|
||||||
import Cookies from 'js-cookie'
|
import Cookies from 'js-cookie'
|
||||||
|
|
||||||
import { SHOPIFY_CART_ID_COOKIE, SHOPIFY_CART_URL_COOKIE } from '../const'
|
import { GetCartQueryVariables, QueryRoot } from '../../schema'
|
||||||
|
|
||||||
|
import { SHOPIFY_CART_ID_COOKIE } from '../const'
|
||||||
|
import { setCartUrlCookie } from '../utils/set-cart-url-cookie'
|
||||||
|
|
||||||
export default useCommerceCart as UseCart<typeof handler>
|
export default useCommerceCart as UseCart<typeof handler>
|
||||||
|
|
||||||
export const handler: SWRHook<GetCartHook> = {
|
export const handler: SWRHook<GetCartHook> = {
|
||||||
fetchOptions: {
|
fetchOptions: {
|
||||||
query: getCartQuery,
|
query: getCartQuery,
|
||||||
},
|
},
|
||||||
async fetcher({ input: { cartId }, options, fetch }) {
|
async fetcher({ input: { cartId }, options, fetch }) {
|
||||||
if (cartId) {
|
if (cartId) {
|
||||||
const { node: cart } = await fetch({
|
let { cart } = await fetch<QueryRoot, GetCartQueryVariables>({
|
||||||
...options,
|
...options,
|
||||||
variables: {
|
variables: { cartId },
|
||||||
checkoutId: cartId,
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
if (cart?.completedAt) {
|
if (cart) {
|
||||||
Cookies.remove(SHOPIFY_CART_ID_COOKIE)
|
setCartUrlCookie(cart.checkoutUrl)
|
||||||
Cookies.remove(SHOPIFY_CART_URL_COOKIE)
|
|
||||||
return null
|
|
||||||
} else {
|
|
||||||
return normalizeCart(cart)
|
return normalizeCart(cart)
|
||||||
|
} else {
|
||||||
|
Cookies.remove(SHOPIFY_CART_ID_COOKIE)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
@ -43,7 +42,7 @@ export const handler: SWRHook<GetCartHook> = {
|
|||||||
Object.create(response, {
|
Object.create(response, {
|
||||||
isEmpty: {
|
isEmpty: {
|
||||||
get() {
|
get() {
|
||||||
return (response.data?.lineItems.length ?? 0) <= 0
|
return (response.data?.lineItems?.length ?? 0) <= 0
|
||||||
},
|
},
|
||||||
enumerable: true,
|
enumerable: true,
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user