mirror of
https://github.com/vercel/commerce.git
synced 2025-07-27 04:01:23 +00:00
Added initial version of useAddItem
This commit is contained in:
@@ -1,12 +1,12 @@
|
||||
import { Cart } from '@commerce/types'
|
||||
import { CommerceError, ValidationError } from '@commerce/utils/errors'
|
||||
import { ValidationError } from '@commerce/utils/errors'
|
||||
import { normalizeCart } from '@framework/utils/normalize'
|
||||
import { Checkout, Maybe, UserError } from '@framework/schema'
|
||||
import { Checkout, UserError } from '@framework/schema'
|
||||
|
||||
const checkoutToCart = (checkoutResponse?: {
|
||||
const checkoutToCart = (checkoutResponse: {
|
||||
checkout: Checkout
|
||||
userErrors?: UserError[]
|
||||
}): Maybe<Cart> => {
|
||||
}): Cart => {
|
||||
const checkout = checkoutResponse?.checkout
|
||||
const userErrors = checkoutResponse?.userErrors
|
||||
|
||||
@@ -16,12 +16,6 @@ const checkoutToCart = (checkoutResponse?: {
|
||||
})
|
||||
}
|
||||
|
||||
if (!checkout) {
|
||||
throw new CommerceError({
|
||||
message: 'Missing checkout details from response cart Response',
|
||||
})
|
||||
}
|
||||
|
||||
return normalizeCart(checkout)
|
||||
}
|
||||
|
||||
|
@@ -5,22 +5,22 @@ import { FetchCartInput } from '@commerce/cart/use-cart'
|
||||
|
||||
const fetcher: HookFetcherFn<Cart | null, FetchCartInput> = async ({
|
||||
options,
|
||||
input: { cartId },
|
||||
input: { cartId: checkoutId },
|
||||
fetch,
|
||||
}) => {
|
||||
let checkout
|
||||
|
||||
if (cartId) {
|
||||
if (checkoutId) {
|
||||
const data = await fetch({
|
||||
...options,
|
||||
variables: {
|
||||
cartId,
|
||||
checkoutId,
|
||||
},
|
||||
})
|
||||
checkout = data?.node
|
||||
}
|
||||
|
||||
if (checkout?.completedAt || !cartId) {
|
||||
if (checkout?.completedAt || !checkoutId) {
|
||||
checkout = await checkoutCreate(fetch)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user