From 1d79007171651a1da2857f0be689e9c957cbdb9b Mon Sep 17 00:00:00 2001 From: cond0r Date: Thu, 5 Aug 2021 22:04:37 +0300 Subject: [PATCH] Replace checkout reference with cart --- framework/shopify/cart/use-add-item.tsx | 14 +++++--- framework/shopify/cart/use-update-item.tsx | 4 +-- framework/shopify/fetcher.ts | 1 + framework/shopify/utils/cart-create.ts | 23 ++++++------- framework/shopify/utils/checkout-create.ts | 33 ------------------- framework/shopify/utils/get-cart-id.ts | 2 +- .../shopify/utils/mutations/cart-create.ts | 2 +- 7 files changed, 24 insertions(+), 55 deletions(-) delete mode 100644 framework/shopify/utils/checkout-create.ts diff --git a/framework/shopify/cart/use-add-item.tsx b/framework/shopify/cart/use-add-item.tsx index 6efa43a95..204c98ce8 100644 --- a/framework/shopify/cart/use-add-item.tsx +++ b/framework/shopify/cart/use-add-item.tsx @@ -9,6 +9,7 @@ import { normalizeCart, throwUserErrors, cartLineItemAddMutation, + cartCreate, } from '../utils' import { CartLinesAddMutation, CartLinesAddMutationVariables } from '../schema' @@ -28,13 +29,20 @@ export const handler: MutationHook = { }) } + let cartId = getCartId() + + if (!cartId) { + const { id } = await cartCreate(fetch) + cartId = id + } + const { cartLinesAdd } = await fetch< CartLinesAddMutation, CartLinesAddMutationVariables >({ ...options, variables: { - checkoutId: getCartId(), + cartId, lineItems: [ { variantId: item.variantId, @@ -46,10 +54,6 @@ export const handler: MutationHook = { throwUserErrors(cartLinesAdd?.userErrors) - if (!cartLinesAdd?.cart) { - throw new CommerceError({ message: 'Missing cart from response' }) - } - return normalizeCart(cartLinesAdd?.cart) }, useHook: diff --git a/framework/shopify/cart/use-update-item.tsx b/framework/shopify/cart/use-update-item.tsx index 7ce2e9060..3a7649eeb 100644 --- a/framework/shopify/cart/use-update-item.tsx +++ b/framework/shopify/cart/use-update-item.tsx @@ -54,8 +54,8 @@ export const handler = { >({ ...options, variables: { - checkoutId: getCartId(), - lineItems: [ + cartItems: getCartId(), + lines: [ { id: itemId, quantity: item.quantity, diff --git a/framework/shopify/fetcher.ts b/framework/shopify/fetcher.ts index 9a8d2d8d5..a0c3ff82f 100644 --- a/framework/shopify/fetcher.ts +++ b/framework/shopify/fetcher.ts @@ -9,6 +9,7 @@ const fetcher: Fetcher = async ({ query, }) => { const { locale, ...vars } = variables ?? {} + return handleFetchResponse( await fetch(url, { method, diff --git a/framework/shopify/utils/cart-create.ts b/framework/shopify/utils/cart-create.ts index 3baa3edcf..40ac633cd 100644 --- a/framework/shopify/utils/cart-create.ts +++ b/framework/shopify/utils/cart-create.ts @@ -10,11 +10,11 @@ import { CartDetailsFragment, } from '../schema' import { FetcherOptions } from '@commerce/utils/types' -import { FetcherError } from '@commerce/utils/errors' +import { CommerceError } from '@commerce/utils/errors' export const cartCreate = async ( fetch: (options: FetcherOptions) => Promise -): Promise<{ node: CartDetailsFragment }> => { +): Promise => { const { cartCreate } = await fetch< CartCreateMutation, CartCreateMutationVariables @@ -24,23 +24,20 @@ export const cartCreate = async ( const cart = cartCreate?.cart - if (!cart) { - throw new FetcherError({ - status: 500, + if (cart?.id) { + const options = { + expires: SHOPIFY_COOKIE_EXPIRE, + } + Cookies.set(SHOPIFY_CART_ID_COOKIE, cart.id, options) + } else { + throw new CommerceError({ errors: cartCreate?.userErrors?.map((e) => ({ message: e.message, })) ?? [{ message: 'Could not create cart' }], }) } - if (cart?.id) { - const options = { - expires: SHOPIFY_COOKIE_EXPIRE, - } - Cookies.set(SHOPIFY_CART_ID_COOKIE, cart.id, options) - } - - return { node: cart } + return cart } export default cartCreate diff --git a/framework/shopify/utils/checkout-create.ts b/framework/shopify/utils/checkout-create.ts deleted file mode 100644 index 359d16315..000000000 --- a/framework/shopify/utils/checkout-create.ts +++ /dev/null @@ -1,33 +0,0 @@ -import Cookies from 'js-cookie' - -import { - SHOPIFY_CHECKOUT_ID_COOKIE, - SHOPIFY_CHECKOUT_URL_COOKIE, - SHOPIFY_COOKIE_EXPIRE, -} from '../const' - -import checkoutCreateMutation from './mutations/checkout-create' -import { CheckoutCreatePayload } from '../schema' - -export const checkoutCreate = async ( - fetch: any -): Promise => { - const data = await fetch({ - query: checkoutCreateMutation, - }) - - const checkout = data.checkoutCreate?.checkout - const checkoutId = checkout?.id - - if (checkoutId) { - const options = { - expires: SHOPIFY_COOKIE_EXPIRE, - } - Cookies.set(SHOPIFY_CHECKOUT_ID_COOKIE, checkoutId, options) - Cookies.set(SHOPIFY_CHECKOUT_URL_COOKIE, checkout.webUrl, options) - } - - return checkout -} - -export default checkoutCreate diff --git a/framework/shopify/utils/get-cart-id.ts b/framework/shopify/utils/get-cart-id.ts index 0193edf31..fcc52cd25 100644 --- a/framework/shopify/utils/get-cart-id.ts +++ b/framework/shopify/utils/get-cart-id.ts @@ -2,7 +2,7 @@ import Cookies from 'js-cookie' import { SHOPIFY_CART_ID_COOKIE } from '../const' const getCartId = (id?: string) => { - return id ?? Cookies.get(SHOPIFY_CART_ID_COOKIE) + return id || Cookies.get(SHOPIFY_CART_ID_COOKIE) } export default getCartId diff --git a/framework/shopify/utils/mutations/cart-create.ts b/framework/shopify/utils/mutations/cart-create.ts index eccf231e7..000fde8b1 100644 --- a/framework/shopify/utils/mutations/cart-create.ts +++ b/framework/shopify/utils/mutations/cart-create.ts @@ -4,7 +4,7 @@ const cartCreateMutation = /* GraphQL */ ` mutation cartCreate { cartCreate { cart { - ...cartDetails + id } userErrors { code