mirror of
https://github.com/vercel/commerce.git
synced 2025-03-31 17:25:53 +00:00
* Packages Bump * Code Updated * More API Changes * Working updates * Updated Tailwind Config * SWR API updates * More changes * Commercejs Types * Commercejs Types * Commercejs Types
41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import { useCallback } from 'react'
|
|
import type { MutationHook } from '@commerce/utils/types'
|
|
import { CommerceError } from '@commerce/utils/errors'
|
|
import useRemoveItem, {
|
|
UseRemoveItem,
|
|
} from '@commerce/wishlist/use-remove-item'
|
|
import type { RemoveItemHook } from '@commerce/types/wishlist'
|
|
import useCustomer from '../customer/use-customer'
|
|
import useWishlist from './use-wishlist'
|
|
|
|
export default useRemoveItem as UseRemoveItem<typeof handler>
|
|
|
|
export const handler: MutationHook<RemoveItemHook> = {
|
|
fetchOptions: {
|
|
url: '/api/wishlist',
|
|
method: 'DELETE',
|
|
},
|
|
useHook:
|
|
({ fetch }) =>
|
|
({ wishlist } = {}) => {
|
|
const { data: customer } = useCustomer()
|
|
const { mutate } = useWishlist(wishlist)
|
|
|
|
return useCallback(
|
|
async function removeItem(input) {
|
|
if (!customer) {
|
|
// A signed customer is required in order to have a wishlist
|
|
throw new CommerceError({
|
|
message: 'Signed customer not found',
|
|
})
|
|
}
|
|
|
|
const data = await fetch({ input: { itemId: String(input.id) } })
|
|
await mutate()
|
|
return data
|
|
},
|
|
[fetch, mutate, customer]
|
|
)
|
|
},
|
|
}
|