From 0ea2490f2a7044d673784469fe6b064fdf89efeb Mon Sep 17 00:00:00 2001 From: cond0r Date: Thu, 5 Aug 2021 23:17:06 +0300 Subject: [PATCH] Checkout changes --- framework/shopify/cart/use-add-item.tsx | 41 ++++++++++----------- framework/shopify/cart/use-cart.tsx | 22 ++++++++--- framework/shopify/utils/checkout-create.ts | 30 +++++++++++---- framework/shopify/utils/checkout-to-cart.ts | 5 +-- 4 files changed, 61 insertions(+), 37 deletions(-) diff --git a/framework/shopify/cart/use-add-item.tsx b/framework/shopify/cart/use-add-item.tsx index 36cd1d1f4..367f90e24 100644 --- a/framework/shopify/cart/use-add-item.tsx +++ b/framework/shopify/cart/use-add-item.tsx @@ -29,36 +29,35 @@ export const handler: MutationHook = { }) } + const lineItems = [ + { + variantId: item.variantId, + quantity: item.quantity ?? 1, + }, + ] + let checkoutId = getCheckoutId() if (!checkoutId) { - const checkout = await checkoutCreate(fetch) - checkoutId = checkout.id + return checkoutToCart(await checkoutCreate(fetch, lineItems)) + } else { + const { checkoutLineItemsAdd } = await fetch< + Mutation, + MutationCheckoutLineItemsAddArgs + >({ + ...options, + variables: { + checkoutId, + lineItems, + }, + }) + return checkoutToCart(checkoutLineItemsAdd) } - - const { checkoutLineItemsAdd } = await fetch< - Mutation, - MutationCheckoutLineItemsAddArgs - >({ - ...options, - variables: { - checkoutId, - lineItems: [ - { - variantId: item.variantId, - quantity: item.quantity ?? 1, - }, - ], - }, - }) - - return checkoutToCart(checkoutLineItemsAdd) }, useHook: ({ fetch }) => () => { const { mutate } = useCart() - return useCallback( async function addItem(input) { const data = await fetch({ input }) diff --git a/framework/shopify/cart/use-cart.tsx b/framework/shopify/cart/use-cart.tsx index 968a429a9..0b8c6657b 100644 --- a/framework/shopify/cart/use-cart.tsx +++ b/framework/shopify/cart/use-cart.tsx @@ -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 @@ -14,15 +20,21 @@ export const handler: SWRHook = { }, 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 }, diff --git a/framework/shopify/utils/checkout-create.ts b/framework/shopify/utils/checkout-create.ts index aa3844f11..36624ed05 100644 --- a/framework/shopify/utils/checkout-create.ts +++ b/framework/shopify/utils/checkout-create.ts @@ -7,25 +7,39 @@ import { } from '../const' import checkoutCreateMutation from './mutations/checkout-create' -import { Checkout } from '../schema' +import { + CheckoutCreatePayload, + CheckoutLineItemInput, + Mutation, + MutationCheckoutCreateArgs, +} from '../schema' +import { FetcherOptions } from '@commerce/utils/types' -export const checkoutCreate = async (fetch: any): Promise => { - const data = await fetch({ +export const checkoutCreate = async ( + fetch: (options: FetcherOptions) => Promise, + lineItems: CheckoutLineItemInput[] +): Promise => { + const { checkoutCreate } = await fetch({ query: checkoutCreateMutation, + variables: { + input: { lineItems }, + }, }) - const checkout = data.checkoutCreate?.checkout - const checkoutId = checkout?.id + const checkout = checkoutCreate?.checkout - if (checkoutId) { + if (checkout) { + const checkoutId = checkout?.id const options = { expires: SHOPIFY_COOKIE_EXPIRE, } Cookies.set(SHOPIFY_CHECKOUT_ID_COOKIE, checkoutId, options) - Cookies.set(SHOPIFY_CHECKOUT_URL_COOKIE, checkout.webUrl, options) + if (checkout?.webUrl) { + Cookies.set(SHOPIFY_CHECKOUT_URL_COOKIE, checkout.webUrl, options) + } } - return checkout + return checkoutCreate! } export default checkoutCreate diff --git a/framework/shopify/utils/checkout-to-cart.ts b/framework/shopify/utils/checkout-to-cart.ts index e2531cc78..159cf0d7d 100644 --- a/framework/shopify/utils/checkout-to-cart.ts +++ b/framework/shopify/utils/checkout-to-cart.ts @@ -27,16 +27,15 @@ export type CheckoutPayload = | CheckoutQuery const checkoutToCart = (checkoutPayload?: Maybe): Cart => { - const checkout = checkoutPayload?.checkout throwUserErrors(checkoutPayload?.checkoutUserErrors) - if (!checkout) { + if (!checkoutPayload?.checkout) { throw new CommerceError({ message: 'Missing checkout object from response', }) } - return normalizeCart(checkout) + return normalizeCart(checkoutPayload?.checkout) } export default checkoutToCart