forked from crowetic/commerce
* Updated log * Updates to root * Updates to pnpm * successfully moved to pnpm * type issue * Local as the default provider * Upgrade dependencies * Revert to local * Upgrade React * Update node-fetch deps * Fix types * Ignore warnings * Fix missing dependency * Update pnpm-lock.yaml * Add missing @types/cookie * Upgrade dependencies * Fix missing dependencies * Update README.md Co-authored-by: Bel Curcio <curciobel@gmail.com>
50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
import { useCallback } from 'react'
|
|
import type {
|
|
MutationHookContext,
|
|
HookFetcherContext,
|
|
MutationHook,
|
|
} from '@vercel/commerce/utils/types'
|
|
import useRemoveItem, {
|
|
UseRemoveItem,
|
|
} from '@vercel/commerce/cart/use-remove-item'
|
|
import useCart from './use-cart'
|
|
import * as mutation from '../utils/mutations'
|
|
import { getCheckoutId, checkoutToCart } from '../utils'
|
|
import { Mutation, MutationCheckoutLineDeleteArgs } from '../../schema'
|
|
import { LineItem, RemoveItemHook } from '../types/cart'
|
|
|
|
export default useRemoveItem as UseRemoveItem<typeof handler>
|
|
|
|
export const handler = {
|
|
fetchOptions: { query: mutation.CheckoutLineDelete },
|
|
async fetcher({
|
|
input: { itemId },
|
|
options,
|
|
fetch,
|
|
}: HookFetcherContext<RemoveItemHook>) {
|
|
const data = await fetch<Mutation, MutationCheckoutLineDeleteArgs>({
|
|
...options,
|
|
variables: {
|
|
checkoutId: getCheckoutId().checkoutId,
|
|
lineId: itemId,
|
|
},
|
|
})
|
|
return checkoutToCart(data.checkoutLineDelete)
|
|
},
|
|
useHook:
|
|
({ fetch }: MutationHookContext<RemoveItemHook>) =>
|
|
<T extends LineItem | undefined = undefined>() => {
|
|
const { mutate } = useCart()
|
|
|
|
return useCallback(
|
|
async function removeItem(input: { id: string }) {
|
|
const data = await fetch({ input: { itemId: input.id } })
|
|
await mutate(data, false)
|
|
|
|
return data
|
|
},
|
|
[fetch, mutate]
|
|
)
|
|
},
|
|
}
|