From 904741b1c533a939d125a6a71aa9c08d15a8e683 Mon Sep 17 00:00:00 2001 From: cond0r Date: Thu, 5 Aug 2021 07:39:41 +0300 Subject: [PATCH] Create checkout on add to cart --- framework/bigcommerce/cart/use-add-item.tsx | 24 ++++---- framework/shopify/cart/use-add-item.tsx | 34 ++++++++---- framework/shopify/cart/use-cart.tsx | 61 +++++++++------------ framework/shopify/utils/checkout-create.ts | 6 +- tsconfig.json | 4 +- 5 files changed, 65 insertions(+), 64 deletions(-) diff --git a/framework/bigcommerce/cart/use-add-item.tsx b/framework/bigcommerce/cart/use-add-item.tsx index 1ac6ac6f8..bdad1862c 100644 --- a/framework/bigcommerce/cart/use-add-item.tsx +++ b/framework/bigcommerce/cart/use-add-item.tsx @@ -29,16 +29,18 @@ export const handler: MutationHook = { return data }, - useHook: ({ fetch }) => () => { - const { mutate } = useCart() + useHook: + ({ fetch }) => + () => { + const { mutate } = useCart() - return useCallback( - async function addItem(input) { - const data = await fetch({ input }) - await mutate(data, false) - return data - }, - [fetch, mutate] - ) - }, + return useCallback( + async function addItem(input) { + const data = await fetch({ input }) + await mutate(data, false) + return data + }, + [fetch, mutate] + ) + }, } diff --git a/framework/shopify/cart/use-add-item.tsx b/framework/shopify/cart/use-add-item.tsx index 5f0809d01..36cd1d1f4 100644 --- a/framework/shopify/cart/use-add-item.tsx +++ b/framework/shopify/cart/use-add-item.tsx @@ -9,6 +9,7 @@ import { checkoutLineItemAddMutation, getCheckoutId, checkoutToCart, + checkoutCreate, } from '../utils' import { Mutation, MutationCheckoutLineItemsAddArgs } from '../schema' @@ -28,13 +29,20 @@ export const handler: MutationHook = { }) } + let checkoutId = getCheckoutId() + + if (!checkoutId) { + const checkout = await checkoutCreate(fetch) + checkoutId = checkout.id + } + const { checkoutLineItemsAdd } = await fetch< Mutation, MutationCheckoutLineItemsAddArgs >({ ...options, variables: { - checkoutId: getCheckoutId(), + checkoutId, lineItems: [ { variantId: item.variantId, @@ -46,16 +54,18 @@ export const handler: MutationHook = { return checkoutToCart(checkoutLineItemsAdd) }, - useHook: ({ fetch }) => () => { - const { mutate } = useCart() + useHook: + ({ fetch }) => + () => { + const { mutate } = useCart() - return useCallback( - async function addItem(input) { - const data = await fetch({ input }) - await mutate(data, false) - return data - }, - [fetch, mutate] - ) - }, + return useCallback( + async function addItem(input) { + const data = await fetch({ input }) + await mutate(data, false) + return data + }, + [fetch, mutate] + ) + }, } diff --git a/framework/shopify/cart/use-cart.tsx b/framework/shopify/cart/use-cart.tsx index d920d058a..968a429a9 100644 --- a/framework/shopify/cart/use-cart.tsx +++ b/framework/shopify/cart/use-cart.tsx @@ -6,52 +6,43 @@ import { checkoutCreate, checkoutToCart } from '../utils' import getCheckoutQuery from '../utils/queries/get-checkout-query' import { GetCartHook } from '../types/cart' -import { - GetCheckoutQuery, - GetCheckoutQueryVariables, - CheckoutDetailsFragment, -} from '../schema' - export default useCommerceCart as UseCart export const handler: SWRHook = { fetchOptions: { query: getCheckoutQuery, }, - async fetcher({ input: { cartId: checkoutId }, options, fetch }) { - let checkout - - if (checkoutId) { - const data = await fetch({ + async fetcher({ input: { cartId }, options, fetch }) { + if (cartId) { + const { node } = await fetch({ ...options, variables: { - checkoutId: checkoutId, + checkoutId: cartId, }, }) - checkout = data.node + return checkoutToCart({ + checkout: node?.completedAt ? await checkoutCreate(fetch) : node, + }) } - - if (checkout?.completedAt || !checkoutId) { - checkout = await checkoutCreate(fetch) - } - - return checkoutToCart({ checkout }) + return null }, - useHook: ({ useData }) => (input) => { - const response = useData({ - swrOptions: { revalidateOnFocus: false, ...input?.swrOptions }, - }) - return useMemo( - () => - Object.create(response, { - isEmpty: { - get() { - return (response.data?.lineItems.length ?? 0) <= 0 + useHook: + ({ useData }) => + (input) => { + const response = useData({ + swrOptions: { revalidateOnFocus: false, ...input?.swrOptions }, + }) + return useMemo( + () => + Object.create(response, { + isEmpty: { + get() { + return (response.data?.lineItems.length ?? 0) <= 0 + }, + enumerable: true, }, - enumerable: true, - }, - }), - [response] - ) - }, + }), + [response] + ) + }, } diff --git a/framework/shopify/utils/checkout-create.ts b/framework/shopify/utils/checkout-create.ts index 359d16315..aa3844f11 100644 --- a/framework/shopify/utils/checkout-create.ts +++ b/framework/shopify/utils/checkout-create.ts @@ -7,11 +7,9 @@ import { } from '../const' import checkoutCreateMutation from './mutations/checkout-create' -import { CheckoutCreatePayload } from '../schema' +import { Checkout } from '../schema' -export const checkoutCreate = async ( - fetch: any -): Promise => { +export const checkoutCreate = async (fetch: any): Promise => { const data = await fetch({ query: checkoutCreateMutation, }) diff --git a/tsconfig.json b/tsconfig.json index 340929669..c37583a0a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -23,8 +23,8 @@ "@components/*": ["components/*"], "@commerce": ["framework/commerce"], "@commerce/*": ["framework/commerce/*"], - "@framework": ["framework/local"], - "@framework/*": ["framework/local/*"] + "@framework": ["framework/shopify"], + "@framework/*": ["framework/shopify/*"] } }, "include": ["next-env.d.ts", "**/*.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"],