forked from crowetic/commerce
Moved wishlist use-add-item
This commit is contained in:
parent
9186fe5a66
commit
f4c067982a
@ -1,43 +1,24 @@
|
|||||||
import { useCallback } from 'react'
|
import { useCallback } from 'react'
|
||||||
import { HookFetcher } from '@commerce/utils/types'
|
import type { MutationHook } from '@commerce/utils/types'
|
||||||
import { CommerceError } from '@commerce/utils/errors'
|
import { CommerceError } from '@commerce/utils/errors'
|
||||||
import useWishlistAddItem, {
|
import useAddItem, { UseAddItem } from '@commerce/wishlist/use-add-item'
|
||||||
AddItemInput,
|
|
||||||
} from '@commerce/wishlist/use-add-item'
|
|
||||||
import { UseWishlistInput } from '@commerce/wishlist/use-wishlist'
|
|
||||||
import type { ItemBody, AddItemBody } from '../api/wishlist'
|
import type { ItemBody, AddItemBody } from '../api/wishlist'
|
||||||
import useCustomer from '../customer/use-customer'
|
import useCustomer from '../customer/use-customer'
|
||||||
import useWishlist from './use-wishlist'
|
import useWishlist from './use-wishlist'
|
||||||
import type { BigcommerceProvider } from '..'
|
|
||||||
|
|
||||||
const defaultOpts = {
|
export default useAddItem as UseAddItem<typeof handler>
|
||||||
url: '/api/bigcommerce/wishlist',
|
|
||||||
method: 'POST',
|
|
||||||
}
|
|
||||||
|
|
||||||
// export type AddItemInput = ItemBody
|
export const handler: MutationHook<any, {}, ItemBody, AddItemBody> = {
|
||||||
|
fetchOptions: {
|
||||||
export const fetcher: HookFetcher<any, AddItemBody> = (
|
url: '/api/bigcommerce/wishlist',
|
||||||
options,
|
method: 'POST',
|
||||||
{ item },
|
},
|
||||||
fetch
|
useHook: ({ fetch }) => () => {
|
||||||
) => {
|
|
||||||
// TODO: add validations before doing the fetch
|
|
||||||
return fetch({
|
|
||||||
...defaultOpts,
|
|
||||||
...options,
|
|
||||||
body: { item },
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
export function extendHook(customFetcher: typeof fetcher) {
|
|
||||||
const useAddItem = (opts?: UseWishlistInput<BigcommerceProvider>) => {
|
|
||||||
const { data: customer } = useCustomer()
|
const { data: customer } = useCustomer()
|
||||||
const { revalidate } = useWishlist(opts)
|
const { revalidate } = useWishlist()
|
||||||
const fn = useWishlistAddItem(defaultOpts, customFetcher)
|
|
||||||
|
|
||||||
return useCallback(
|
return useCallback(
|
||||||
async function addItem(input: AddItemInput<any>) {
|
async function addItem(item) {
|
||||||
if (!customer) {
|
if (!customer) {
|
||||||
// A signed customer is required in order to have a wishlist
|
// A signed customer is required in order to have a wishlist
|
||||||
throw new CommerceError({
|
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()
|
await revalidate()
|
||||||
return data
|
return data
|
||||||
},
|
},
|
||||||
[fn, revalidate, customer]
|
[fetch, revalidate, customer]
|
||||||
)
|
)
|
||||||
}
|
},
|
||||||
|
|
||||||
useAddItem.extend = extendHook
|
|
||||||
|
|
||||||
return useAddItem
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default extendHook(fetcher)
|
|
||||||
|
@ -22,6 +22,8 @@ export type Provider = CommerceConfig & {
|
|||||||
}
|
}
|
||||||
wishlist?: {
|
wishlist?: {
|
||||||
useWishlist?: SWRHook<Wishlist | null, any, any>
|
useWishlist?: SWRHook<Wishlist | null, any, any>
|
||||||
|
useAddItem?: MutationHook<any, any, any>
|
||||||
|
useRemoveItem?: MutationHook<any, any, any>
|
||||||
}
|
}
|
||||||
customer: {
|
customer: {
|
||||||
useCustomer?: SWRHook<Customer | null, any, any>
|
useCustomer?: SWRHook<Customer | null, any, any>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { useCallback } from 'react'
|
import { useCallback } from 'react'
|
||||||
import type { MutationHook, PickRequired, SWRHook } from './types'
|
|
||||||
import { Provider, useCommerce } from '..'
|
import { Provider, useCommerce } from '..'
|
||||||
|
import type { MutationHook, PickRequired, SWRHook } from './types'
|
||||||
import useData from './use-data'
|
import useData from './use-data'
|
||||||
|
|
||||||
export function useFetcher() {
|
export function useFetcher() {
|
||||||
|
@ -1,12 +1,19 @@
|
|||||||
import useAction from '../utils/use-action'
|
import { useHook, useMutationHook } from '../utils/use-hook'
|
||||||
import type { CartItemBody } from '../types'
|
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 type UseAddItem<
|
||||||
// export interface AddItemInput {
|
H extends MutationHook<any, any, any> = MutationHook<any, {}, {}>
|
||||||
// includeProducts?: boolean
|
> = ReturnType<H['useHook']>
|
||||||
// }
|
|
||||||
export type AddItemInput<T extends CartItemBody> = T
|
|
||||||
|
|
||||||
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
|
export default useAddItem
|
||||||
|
Loading…
x
Reference in New Issue
Block a user