fix/saleor: handle non-existing checkout

This commit is contained in:
Zaiste 2021-06-10 21:40:12 +02:00
parent 62f5dead0d
commit 4492f3051c
No known key found for this signature in database
GPG Key ID: 15DF7EBC7F2FFE35
3 changed files with 56 additions and 31 deletions

View File

@ -18,7 +18,7 @@ export default function getSiteInfoOperation({ commerce }: OperationContext<Prov
query?: string query?: string
config?: Partial<SaleorConfig> config?: Partial<SaleorConfig>
preview?: boolean preview?: boolean
variables?: any variables?: any
} = {}): Promise<GetSiteInfoResult> { } = {}): Promise<GetSiteInfoResult> {
const cfg = commerce.getConfig(config) const cfg = commerce.getConfig(config)

View File

@ -6,7 +6,7 @@ import useCart from './use-cart'
import * as mutation from '../utils/mutations' import * as mutation from '../utils/mutations'
import { getCheckoutId, checkoutToCart } from '../utils' import { getCheckoutId, checkoutToCart, checkoutCreate } from '../utils'
import { Mutation, MutationCheckoutLinesAddArgs } from '../schema' import { Mutation, MutationCheckoutLinesAddArgs } from '../schema'
import { AddItemHook } from '@commerce/types/cart' import { AddItemHook } from '@commerce/types/cart'
@ -35,20 +35,39 @@ export const handler: MutationHook<AddItemHook> = {
}, },
}) })
if (checkoutLinesAdd?.errors) {
await checkoutCreate(fetch)
const { checkoutLinesAdd } = await fetch<Mutation, MutationCheckoutLinesAddArgs>({
...options,
variables: {
checkoutId: getCheckoutId().checkoutId,
lineItems: [
{
variantId: item.variantId,
quantity: item.quantity ?? 1,
},
],
},
})
return checkoutToCart(checkoutLinesAdd)
}
return checkoutToCart(checkoutLinesAdd) return checkoutToCart(checkoutLinesAdd)
}, },
useHook: useHook:
({ fetch }) => ({ fetch }) =>
() => { () => {
const { mutate } = useCart() const { mutate } = useCart()
return useCallback( return useCallback(
async function addItem(input) { async function addItem(input) {
const data = await fetch({ input }) const data = await fetch({ input })
await mutate(data, false) await mutate(data, false)
return data return data
}, },
[fetch, mutate] [fetch, mutate]
) )
}, },
} }

View File

@ -16,12 +16,18 @@ export const handler: SWRHook<GetCartHook> = {
let checkout let checkout
if (checkoutId) { if (checkoutId) {
const checkoutId = getCheckoutId().checkoutToken const r = getCheckoutId()
const checkoutToken = r.checkoutToken
const data = await fetch({ const data = await fetch({
...options, ...options,
variables: { checkoutId }, variables: { checkoutId: checkoutToken },
}) })
if (!data.checkout) {
checkout = await checkoutCreate(fetch)
}
checkout = data checkout = data
} }
@ -33,21 +39,21 @@ export const handler: SWRHook<GetCartHook> = {
}, },
useHook: useHook:
({ useData }) => ({ useData }) =>
(input) => { (input) => {
const response = useData({ const response = useData({
swrOptions: { revalidateOnFocus: false, ...input?.swrOptions }, swrOptions: { revalidateOnFocus: false, ...input?.swrOptions },
}) })
return useMemo( return useMemo(
() => () =>
Object.create(response, { Object.create(response, {
isEmpty: { isEmpty: {
get() { get() {
return (response.data?.lineItems.length ?? 0) <= 0 return (response.data?.lineItems.length ?? 0) <= 0
},
enumerable: true,
}, },
enumerable: true, }),
}, [response]
}), )
[response] },
)
},
} }