mirror of
https://github.com/vercel/commerce.git
synced 2025-06-19 13:41:22 +00:00
93 lines
2.5 KiB
TypeScript
93 lines
2.5 KiB
TypeScript
import { useCallback } from 'react'
|
|
import { HookFetcherContext, MutationHookContext } from '@commerce/utils/types'
|
|
import { CommerceError, ValidationError } from '@commerce/utils/errors'
|
|
import useUpdateItem, { UseUpdateItem } from '@commerce/cart/use-update-item'
|
|
import {
|
|
Cart,
|
|
CartItemBody,
|
|
LineItem,
|
|
UpdateCartItemBody,
|
|
} from '@commerce/types'
|
|
import useCart from './use-cart'
|
|
import {
|
|
AdjustOrderLineMutation,
|
|
AdjustOrderLineMutationVariables,
|
|
} from '../schema'
|
|
import { cartFragment } from '../api/fragments/cart'
|
|
import { normalizeCart } from '../lib/normalize'
|
|
|
|
export const adjustOrderLineMutation = /* GraphQL */ `
|
|
mutation adjustOrderLine($orderLineId: ID!, $quantity: Int!) {
|
|
adjustOrderLine(orderLineId: $orderLineId, quantity: $quantity) {
|
|
__typename
|
|
...Cart
|
|
... on ErrorResult {
|
|
errorCode
|
|
message
|
|
}
|
|
}
|
|
}
|
|
${cartFragment}
|
|
`
|
|
|
|
export default useUpdateItem as UseUpdateItem<typeof handler>
|
|
|
|
export const handler = {
|
|
fetchOptions: {
|
|
query: adjustOrderLineMutation,
|
|
},
|
|
async fetcher(context: HookFetcherContext<UpdateCartItemBody<CartItemBody>>) {
|
|
const { input, options, fetch } = context
|
|
const variables: AdjustOrderLineMutationVariables = {
|
|
quantity: input.item.quantity || 1,
|
|
orderLineId: input.itemId,
|
|
}
|
|
const { adjustOrderLine } = await fetch<AdjustOrderLineMutation>({
|
|
...options,
|
|
variables,
|
|
})
|
|
|
|
if (adjustOrderLine.__typename === 'Order') {
|
|
return normalizeCart(adjustOrderLine)
|
|
}
|
|
throw new CommerceError(adjustOrderLine)
|
|
},
|
|
useHook: ({
|
|
fetch,
|
|
}: MutationHookContext<Cart | null, UpdateCartItemBody<CartItemBody>>) => (
|
|
ctx: {
|
|
item?: LineItem
|
|
wait?: number
|
|
} = {}
|
|
) => {
|
|
const { item } = ctx
|
|
const { mutate } = useCart()
|
|
|
|
return useCallback(
|
|
async function addItem(input: CartItemBody) {
|
|
const itemId = item?.id
|
|
const productId = input.productId ?? item?.productId
|
|
const variantId = input.productId ?? item?.variantId
|
|
if (!itemId || !productId || !variantId) {
|
|
throw new ValidationError({
|
|
message: 'Invalid input used for this operation',
|
|
})
|
|
}
|
|
const data = await fetch({
|
|
input: {
|
|
item: {
|
|
productId,
|
|
variantId,
|
|
quantity: input.quantity,
|
|
},
|
|
itemId,
|
|
},
|
|
})
|
|
await mutate(data, false)
|
|
return data
|
|
},
|
|
[fetch, mutate]
|
|
)
|
|
},
|
|
}
|