mirror of
https://github.com/vercel/commerce.git
synced 2025-07-22 20:26:49 +00:00
@@ -1,20 +1,24 @@
|
||||
import { useCallback } from 'react'
|
||||
import { HookFetcherContext, MutationHookContext } from '@commerce/utils/types'
|
||||
import {
|
||||
HookFetcherContext,
|
||||
MutationHook,
|
||||
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 { CartItemBody, LineItem } from '@commerce/types/cart'
|
||||
import useCart from './use-cart'
|
||||
import {
|
||||
AdjustOrderLineMutation,
|
||||
AdjustOrderLineMutationVariables,
|
||||
} from '../schema'
|
||||
import { normalizeCart } from '../lib/normalize'
|
||||
import { adjustOrderLineMutation } from '../lib/mutations/adjust-order-line-mutation'
|
||||
import { normalizeCart } from '../utils/normalize'
|
||||
import { adjustOrderLineMutation } from '../utils/mutations/adjust-order-line-mutation'
|
||||
import { UpdateItemHook } from '../types/cart'
|
||||
|
||||
export type UpdateItemActionInput<T = any> = T extends LineItem
|
||||
? Partial<UpdateItemHook['actionInput']>
|
||||
: UpdateItemHook['actionInput']
|
||||
|
||||
export default useUpdateItem as UseUpdateItem<typeof handler>
|
||||
|
||||
@@ -22,7 +26,7 @@ export const handler = {
|
||||
fetchOptions: {
|
||||
query: adjustOrderLineMutation,
|
||||
},
|
||||
async fetcher(context: HookFetcherContext<UpdateCartItemBody<CartItemBody>>) {
|
||||
async fetcher(context: HookFetcherContext<UpdateItemHook>) {
|
||||
const { input, options, fetch } = context
|
||||
const variables: AdjustOrderLineMutationVariables = {
|
||||
quantity: input.item.quantity || 1,
|
||||
@@ -38,9 +42,7 @@ export const handler = {
|
||||
}
|
||||
throw new CommerceError(adjustOrderLine)
|
||||
},
|
||||
useHook: ({
|
||||
fetch,
|
||||
}: MutationHookContext<Cart | null, UpdateCartItemBody<CartItemBody>>) => (
|
||||
useHook: ({ fetch }: MutationHookContext<UpdateItemHook>) => (
|
||||
ctx: {
|
||||
item?: LineItem
|
||||
wait?: number
|
||||
@@ -50,7 +52,7 @@ export const handler = {
|
||||
const { mutate } = useCart()
|
||||
|
||||
return useCallback(
|
||||
async function addItem(input: Partial<CartItemBody>) {
|
||||
async function addItem(input: UpdateItemActionInput) {
|
||||
const itemId = item?.id
|
||||
const productId = input.productId ?? item?.productId
|
||||
const variantId = input.productId ?? item?.variantId
|
||||
|
Reference in New Issue
Block a user