mirror of
https://github.com/vercel/commerce.git
synced 2025-03-28 08:15:54 +00:00
71 lines
1.7 KiB
TypeScript
71 lines
1.7 KiB
TypeScript
import { useCallback } from 'react'
|
|
import type { MutationHook } from '@commerce/utils/types'
|
|
import { CommerceError } from '@commerce/utils/errors'
|
|
import useAddItem, { UseAddItem } from '@commerce/cart/use-add-item'
|
|
import type { AddItemHook } from '../types/cart'
|
|
import useCart from './use-cart'
|
|
|
|
import {
|
|
checkoutLineItemAddMutation,
|
|
getCheckoutId,
|
|
checkoutToCart,
|
|
checkoutCreate,
|
|
} from '../utils'
|
|
import { Mutation, MutationCheckoutLineItemsAddArgs } from '../schema'
|
|
|
|
export default useAddItem as UseAddItem<typeof handler>
|
|
|
|
export const handler: MutationHook<AddItemHook> = {
|
|
fetchOptions: {
|
|
query: checkoutLineItemAddMutation,
|
|
},
|
|
async fetcher({ input: item, options, fetch }) {
|
|
if (
|
|
item.quantity &&
|
|
(!Number.isInteger(item.quantity) || item.quantity! < 1)
|
|
) {
|
|
throw new CommerceError({
|
|
message: 'The item quantity has to be a valid integer greater than 0',
|
|
})
|
|
}
|
|
|
|
const lineItems = [
|
|
{
|
|
variantId: item.variantId,
|
|
quantity: item.quantity ?? 1,
|
|
},
|
|
]
|
|
|
|
let checkoutId = getCheckoutId()
|
|
|
|
if (!checkoutId) {
|
|
return checkoutToCart(await checkoutCreate(fetch, lineItems))
|
|
} else {
|
|
const { checkoutLineItemsAdd } = await fetch<
|
|
Mutation,
|
|
MutationCheckoutLineItemsAddArgs
|
|
>({
|
|
...options,
|
|
variables: {
|
|
checkoutId,
|
|
lineItems,
|
|
},
|
|
})
|
|
return checkoutToCart(checkoutLineItemsAdd)
|
|
}
|
|
},
|
|
useHook:
|
|
({ fetch }) =>
|
|
() => {
|
|
const { mutate } = useCart()
|
|
return useCallback(
|
|
async function addItem(input) {
|
|
const data = await fetch({ input })
|
|
await mutate(data, false)
|
|
return data
|
|
},
|
|
[fetch, mutate]
|
|
)
|
|
},
|
|
}
|