mirror of
https://github.com/vercel/commerce.git
synced 2025-09-18 13:50:15 +00:00
.vscode
assets
components
config
docs
framework
bigcommerce
api
auth
cart
index.ts
use-add-item.tsx
use-cart.tsx
use-remove-item.tsx
use-update-item.tsx
common
customer
lib
product
scripts
types
wishlist
.env.template
README.md
commerce.config.json
fetcher.ts
index.tsx
next.config.js
provider.ts
schema.d.ts
schema.graphql
commerce
shopify
lib
pages
public
.editorconfig
.env.template
.gitignore
.prettierignore
.prettierrc
README.md
codegen.json
commerce.config.json
global.d.ts
license.md
next-env.d.ts
next.config.js
package.json
postcss.config.js
tailwind.config.js
tsconfig.json
yarn.lock
57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
import { useCallback } from 'react'
|
|
import type {
|
|
MutationHookContext,
|
|
HookFetcherContext,
|
|
} from '@commerce/utils/types'
|
|
import { ValidationError } from '@commerce/utils/errors'
|
|
import useRemoveItem, { UseRemoveItem } from '@commerce/cart/use-remove-item'
|
|
import type { Cart, LineItem, RemoveItemHook } from '../types/cart'
|
|
import useCart from './use-cart'
|
|
|
|
export type RemoveItemFn<T = any> = T extends LineItem
|
|
? (input?: RemoveItemActionInput<T>) => Promise<Cart | null>
|
|
: (input: RemoveItemActionInput<T>) => Promise<Cart | null>
|
|
|
|
export type RemoveItemActionInput<T = any> = T extends LineItem
|
|
? Partial<RemoveItemHook['actionInput']>
|
|
: RemoveItemHook['actionInput']
|
|
|
|
export default useRemoveItem as UseRemoveItem<typeof handler>
|
|
|
|
export const handler = {
|
|
fetchOptions: {
|
|
url: '/api/bigcommerce/cart',
|
|
method: 'DELETE',
|
|
},
|
|
async fetcher({
|
|
input: { itemId },
|
|
options,
|
|
fetch,
|
|
}: HookFetcherContext<RemoveItemHook>) {
|
|
return await fetch({ ...options, body: { itemId } })
|
|
},
|
|
useHook: ({ fetch }: MutationHookContext<RemoveItemHook>) => <
|
|
T extends LineItem | undefined = undefined
|
|
>(
|
|
ctx: { item?: T } = {}
|
|
) => {
|
|
const { item } = ctx
|
|
const { mutate } = useCart()
|
|
const removeItem: RemoveItemFn<LineItem> = async (input) => {
|
|
const itemId = input?.id ?? item?.id
|
|
|
|
if (!itemId) {
|
|
throw new ValidationError({
|
|
message: 'Invalid input used for this operation',
|
|
})
|
|
}
|
|
|
|
const data = await fetch({ input: { itemId } })
|
|
await mutate(data, false)
|
|
return data
|
|
}
|
|
|
|
return useCallback(removeItem as RemoveItemFn<T>, [fetch, mutate])
|
|
},
|
|
}
|