mirror of
https://github.com/vercel/commerce.git
synced 2025-07-25 19:21:23 +00:00
Checkout changes
This commit is contained in:
@@ -2,9 +2,15 @@ import { useMemo } from 'react'
|
||||
import useCommerceCart, { UseCart } from '@commerce/cart/use-cart'
|
||||
|
||||
import { SWRHook } from '@commerce/utils/types'
|
||||
import { checkoutCreate, checkoutToCart } from '../utils'
|
||||
import { checkoutToCart } from '../utils'
|
||||
import getCheckoutQuery from '../utils/queries/get-checkout-query'
|
||||
import { GetCartHook } from '../types/cart'
|
||||
import Cookies from 'js-cookie'
|
||||
|
||||
import {
|
||||
SHOPIFY_CHECKOUT_ID_COOKIE,
|
||||
SHOPIFY_CHECKOUT_URL_COOKIE,
|
||||
} from '../const'
|
||||
|
||||
export default useCommerceCart as UseCart<typeof handler>
|
||||
|
||||
@@ -14,15 +20,21 @@ export const handler: SWRHook<GetCartHook> = {
|
||||
},
|
||||
async fetcher({ input: { cartId }, options, fetch }) {
|
||||
if (cartId) {
|
||||
const { node } = await fetch({
|
||||
const { node: checkout } = await fetch({
|
||||
...options,
|
||||
variables: {
|
||||
checkoutId: cartId,
|
||||
},
|
||||
})
|
||||
return checkoutToCart({
|
||||
checkout: node?.completedAt ? await checkoutCreate(fetch) : node,
|
||||
})
|
||||
if (checkout?.completedAt) {
|
||||
Cookies.remove(SHOPIFY_CHECKOUT_ID_COOKIE)
|
||||
Cookies.remove(SHOPIFY_CHECKOUT_URL_COOKIE)
|
||||
return null
|
||||
} else {
|
||||
return checkoutToCart({
|
||||
checkout,
|
||||
})
|
||||
}
|
||||
}
|
||||
return null
|
||||
},
|
||||
|
Reference in New Issue
Block a user