Checkout changes

This commit is contained in:
cond0r
2021-08-05 23:17:06 +03:00
parent 904741b1c5
commit 0ea2490f2a
4 changed files with 61 additions and 37 deletions

View File

@@ -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
},