From 4492f3051ca26a0604b43ae18d4cc3ddad2d1028 Mon Sep 17 00:00:00 2001 From: Zaiste Date: Thu, 10 Jun 2021 21:40:12 +0200 Subject: [PATCH] fix/saleor: handle non-existing checkout --- .../saleor/api/operations/get-site-info.ts | 2 +- framework/saleor/cart/use-add-item.tsx | 43 +++++++++++++------ framework/saleor/cart/use-cart.tsx | 42 ++++++++++-------- 3 files changed, 56 insertions(+), 31 deletions(-) diff --git a/framework/saleor/api/operations/get-site-info.ts b/framework/saleor/api/operations/get-site-info.ts index eca0f2246..917b96ce4 100644 --- a/framework/saleor/api/operations/get-site-info.ts +++ b/framework/saleor/api/operations/get-site-info.ts @@ -18,7 +18,7 @@ export default function getSiteInfoOperation({ commerce }: OperationContext preview?: boolean - variables?: any + variables?: any } = {}): Promise { const cfg = commerce.getConfig(config) diff --git a/framework/saleor/cart/use-add-item.tsx b/framework/saleor/cart/use-add-item.tsx index 3af368e70..199979b9c 100644 --- a/framework/saleor/cart/use-add-item.tsx +++ b/framework/saleor/cart/use-add-item.tsx @@ -6,7 +6,7 @@ import useCart from './use-cart' import * as mutation from '../utils/mutations' -import { getCheckoutId, checkoutToCart } from '../utils' +import { getCheckoutId, checkoutToCart, checkoutCreate } from '../utils' import { Mutation, MutationCheckoutLinesAddArgs } from '../schema' import { AddItemHook } from '@commerce/types/cart' @@ -35,20 +35,39 @@ export const handler: MutationHook = { }, }) + if (checkoutLinesAdd?.errors) { + await checkoutCreate(fetch) + + const { checkoutLinesAdd } = await fetch({ + ...options, + variables: { + checkoutId: getCheckoutId().checkoutId, + lineItems: [ + { + variantId: item.variantId, + quantity: item.quantity ?? 1, + }, + ], + }, + }) + + return checkoutToCart(checkoutLinesAdd) + } + return checkoutToCart(checkoutLinesAdd) }, useHook: ({ fetch }) => - () => { - const { mutate } = useCart() + () => { + 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/saleor/cart/use-cart.tsx b/framework/saleor/cart/use-cart.tsx index ab80ea395..23b5a24d8 100644 --- a/framework/saleor/cart/use-cart.tsx +++ b/framework/saleor/cart/use-cart.tsx @@ -16,12 +16,18 @@ export const handler: SWRHook = { let checkout if (checkoutId) { - const checkoutId = getCheckoutId().checkoutToken + const r = getCheckoutId() + const checkoutToken = r.checkoutToken + const data = await fetch({ ...options, - variables: { checkoutId }, + variables: { checkoutId: checkoutToken }, }) + if (!data.checkout) { + checkout = await checkoutCreate(fetch) + } + checkout = data } @@ -33,21 +39,21 @@ export const handler: SWRHook = { }, 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 + (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] + ) + }, }