mirror of
https://github.com/vercel/commerce.git
synced 2025-06-19 21:51:21 +00:00
fix/saleor: handle non-existing checkout
This commit is contained in:
parent
62f5dead0d
commit
4492f3051c
@ -18,7 +18,7 @@ export default function getSiteInfoOperation({ commerce }: OperationContext<Prov
|
||||
query?: string
|
||||
config?: Partial<SaleorConfig>
|
||||
preview?: boolean
|
||||
variables?: any
|
||||
variables?: any
|
||||
} = {}): Promise<GetSiteInfoResult> {
|
||||
const cfg = commerce.getConfig(config)
|
||||
|
||||
|
@ -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<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)
|
||||
},
|
||||
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]
|
||||
)
|
||||
},
|
||||
}
|
||||
|
@ -16,12 +16,18 @@ export const handler: SWRHook<GetCartHook> = {
|
||||
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<GetCartHook> = {
|
||||
},
|
||||
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]
|
||||
)
|
||||
},
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user