mirror of
https://github.com/vercel/commerce.git
synced 2025-10-08 15:12:40 +00:00
assets
components
config
lib
bigcommerce
api
cart
products
scripts
wishlist
use-add-item.tsx
use-remove-item.tsx
use-wishlist-actions.tsx
use-wishlist.tsx
index.tsx
schema.d.ts
schema.graphql
use-customer.tsx
use-login.tsx
use-logout.tsx
use-price.tsx
use-signup.tsx
commerce
browser.ts
colors.ts
logger.ts
range-map.ts
to-pixels.ts
pages
public
utils
.gitignore
.prettierignore
README.md
codegen.json
global.d.ts
next-env.d.ts
next.config.js
package.json
postcss.config.js
tailwind.config.js
tsconfig.json
yarn.lock
47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
import { useCallback } from 'react'
|
|
import { HookFetcher } from '@lib/commerce/utils/types'
|
|
import useAction from '@lib/commerce/utils/use-action'
|
|
import type { ItemBody, AddItemBody } from '../api/wishlist'
|
|
import useWishlist, { Wishlist } from './use-wishlist'
|
|
|
|
const defaultOpts = {
|
|
url: '/api/bigcommerce/wishlist',
|
|
method: 'POST',
|
|
}
|
|
|
|
export type AddItemInput = ItemBody
|
|
|
|
export const fetcher: HookFetcher<Wishlist, AddItemBody> = (
|
|
options,
|
|
{ wishlistId, item },
|
|
fetch
|
|
) => {
|
|
return fetch({
|
|
...defaultOpts,
|
|
...options,
|
|
body: { wishlistId, item },
|
|
})
|
|
}
|
|
|
|
export function extendHook(customFetcher: typeof fetcher) {
|
|
const useAddItem = (wishlistId: string) => {
|
|
const { mutate } = useWishlist(wishlistId)
|
|
const fn = useAction<Wishlist, AddItemBody>(defaultOpts, customFetcher)
|
|
|
|
return useCallback(
|
|
async function addItem(input: AddItemInput) {
|
|
const data = await fn({ wishlistId, item: input })
|
|
await mutate(data, false)
|
|
return data
|
|
},
|
|
[fn, mutate]
|
|
)
|
|
}
|
|
|
|
useAddItem.extend = extendHook
|
|
|
|
return useAddItem
|
|
}
|
|
|
|
export default extendHook(fetcher)
|