diff --git a/framework/bigcommerce/wishlist/use-add-item.tsx b/framework/bigcommerce/wishlist/use-add-item.tsx index eb961951a..402e7da8b 100644 --- a/framework/bigcommerce/wishlist/use-add-item.tsx +++ b/framework/bigcommerce/wishlist/use-add-item.tsx @@ -1,43 +1,24 @@ import { useCallback } from 'react' -import { HookFetcher } from '@commerce/utils/types' +import type { MutationHook } from '@commerce/utils/types' import { CommerceError } from '@commerce/utils/errors' -import useWishlistAddItem, { - AddItemInput, -} from '@commerce/wishlist/use-add-item' -import { UseWishlistInput } from '@commerce/wishlist/use-wishlist' +import useAddItem, { UseAddItem } from '@commerce/wishlist/use-add-item' import type { ItemBody, AddItemBody } from '../api/wishlist' import useCustomer from '../customer/use-customer' import useWishlist from './use-wishlist' -import type { BigcommerceProvider } from '..' -const defaultOpts = { - url: '/api/bigcommerce/wishlist', - method: 'POST', -} +export default useAddItem as UseAddItem -// export type AddItemInput = ItemBody - -export const fetcher: HookFetcher = ( - options, - { item }, - fetch -) => { - // TODO: add validations before doing the fetch - return fetch({ - ...defaultOpts, - ...options, - body: { item }, - }) -} - -export function extendHook(customFetcher: typeof fetcher) { - const useAddItem = (opts?: UseWishlistInput) => { +export const handler: MutationHook = { + fetchOptions: { + url: '/api/bigcommerce/wishlist', + method: 'POST', + }, + useHook: ({ fetch }) => () => { const { data: customer } = useCustomer() - const { revalidate } = useWishlist(opts) - const fn = useWishlistAddItem(defaultOpts, customFetcher) + const { revalidate } = useWishlist() return useCallback( - async function addItem(input: AddItemInput) { + async function addItem(item) { if (!customer) { // A signed customer is required in order to have a wishlist throw new CommerceError({ @@ -45,17 +26,12 @@ export function extendHook(customFetcher: typeof fetcher) { }) } - const data = await fn({ item: input }) + // TODO: add validations before doing the fetch + const data = await fetch({ input: { item } }) await revalidate() return data }, - [fn, revalidate, customer] + [fetch, revalidate, customer] ) - } - - useAddItem.extend = extendHook - - return useAddItem + }, } - -export default extendHook(fetcher) diff --git a/framework/commerce/index.tsx b/framework/commerce/index.tsx index 22b8de8ed..bcaecc1a3 100644 --- a/framework/commerce/index.tsx +++ b/framework/commerce/index.tsx @@ -22,6 +22,8 @@ export type Provider = CommerceConfig & { } wishlist?: { useWishlist?: SWRHook + useAddItem?: MutationHook + useRemoveItem?: MutationHook } customer: { useCustomer?: SWRHook diff --git a/framework/commerce/utils/use-hook.ts b/framework/commerce/utils/use-hook.ts index 0ee1f852e..79c0808be 100644 --- a/framework/commerce/utils/use-hook.ts +++ b/framework/commerce/utils/use-hook.ts @@ -1,6 +1,6 @@ import { useCallback } from 'react' -import type { MutationHook, PickRequired, SWRHook } from './types' import { Provider, useCommerce } from '..' +import type { MutationHook, PickRequired, SWRHook } from './types' import useData from './use-data' export function useFetcher() { diff --git a/framework/commerce/wishlist/use-add-item.tsx b/framework/commerce/wishlist/use-add-item.tsx index d9b513694..11c8cc241 100644 --- a/framework/commerce/wishlist/use-add-item.tsx +++ b/framework/commerce/wishlist/use-add-item.tsx @@ -1,12 +1,19 @@ -import useAction from '../utils/use-action' -import type { CartItemBody } from '../types' +import { useHook, useMutationHook } from '../utils/use-hook' +import { mutationFetcher } from '../utils/default-fetcher' +import type { MutationHook } from '../utils/types' +import type { Provider } from '..' -// Input expected by the action returned by the `useAddItem` hook -// export interface AddItemInput { -// includeProducts?: boolean -// } -export type AddItemInput = T +export type UseAddItem< + H extends MutationHook = MutationHook +> = ReturnType -const useAddItem = useAction +export const fetcher = mutationFetcher + +const fn = (provider: Provider) => provider.wishlist?.useAddItem! + +const useAddItem: UseAddItem = (...args) => { + const hook = useHook(fn) + return useMutationHook({ fetcher, ...hook })(...args) +} export default useAddItem