mirror of
https://github.com/vercel/commerce.git
synced 2025-03-27 15:55:54 +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
39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
import { useCallback } from 'react'
|
|
import type { MutationHook } from '@commerce/utils/types'
|
|
import { CommerceError } from '@commerce/utils/errors'
|
|
import useAddItem, { UseAddItem } from '@commerce/wishlist/use-add-item'
|
|
import type { AddItemHook } from '@commerce/types/wishlist'
|
|
import useCustomer from '../customer/use-customer'
|
|
import useWishlist from './use-wishlist'
|
|
|
|
export default useAddItem as UseAddItem<typeof handler>
|
|
|
|
export const handler: MutationHook<AddItemHook> = {
|
|
fetchOptions: {
|
|
url: '/api/wishlist',
|
|
method: 'POST',
|
|
},
|
|
useHook:
|
|
({ fetch }) =>
|
|
() => {
|
|
const { data: customer } = useCustomer()
|
|
const { mutate } = useWishlist()
|
|
|
|
return useCallback(
|
|
async function addItem(item) {
|
|
if (!customer) {
|
|
// A signed customer is required in order to have a wishlist
|
|
throw new CommerceError({
|
|
message: 'Signed customer not found',
|
|
})
|
|
}
|
|
// TODO: add validations before doing the fetch
|
|
const data = await fetch({ input: { item } })
|
|
await mutate()
|
|
return data
|
|
},
|
|
[fetch, mutate, customer]
|
|
)
|
|
},
|
|
}
|