mirror of
https://github.com/vercel/commerce.git
synced 2025-09-01 21:40:14 +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
bc-image-src.ts
browser.ts
colors.ts
get-pathname.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
52 lines
1.2 KiB
TypeScript
52 lines
1.2 KiB
TypeScript
import { useCallback } from 'react'
|
|
import { HookFetcher } from '@lib/commerce/utils/types'
|
|
import useAction from '@lib/commerce/utils/use-action'
|
|
import type { RemoveItemBody } from '../api/wishlist'
|
|
import useWishlist, { Wishlist } from './use-wishlist'
|
|
|
|
const defaultOpts = {
|
|
url: '/api/bigcommerce/wishlists',
|
|
method: 'DELETE',
|
|
}
|
|
|
|
export type RemoveItemInput = {
|
|
id: string
|
|
}
|
|
|
|
export const fetcher: HookFetcher<Wishlist | null, RemoveItemBody> = (
|
|
options,
|
|
{ wishlistId, itemId },
|
|
fetch
|
|
) => {
|
|
return fetch({
|
|
...defaultOpts,
|
|
...options,
|
|
body: { wishlistId, itemId },
|
|
})
|
|
}
|
|
|
|
export function extendHook(customFetcher: typeof fetcher) {
|
|
const useRemoveItem = (wishlistId: string, item?: any) => {
|
|
const { mutate } = useWishlist(wishlistId)
|
|
const fn = useAction<Wishlist | null, RemoveItemBody>(
|
|
defaultOpts,
|
|
customFetcher
|
|
)
|
|
|
|
return useCallback(
|
|
async function removeItem(input: RemoveItemInput) {
|
|
const data = await fn({ wishlistId, itemId: input.id ?? item?.id })
|
|
await mutate(data, false)
|
|
return data
|
|
},
|
|
[fn, mutate]
|
|
)
|
|
}
|
|
|
|
useRemoveItem.extend = extendHook
|
|
|
|
return useRemoveItem
|
|
}
|
|
|
|
export default extendHook(fetcher)
|