diff --git a/framework/bigcommerce/provider.tsx b/framework/bigcommerce/provider.tsx index 0d794edd9..364e87539 100644 --- a/framework/bigcommerce/provider.tsx +++ b/framework/bigcommerce/provider.tsx @@ -54,12 +54,11 @@ const useCart: HookHandler< url: '/api/bigcommerce/cart', method: 'GET', }, - swrOptions: { - revalidateOnFocus: false, - }, normalizer: normalizeCart, useHook({ input, useData }) { - const response = useData({ input }) + const response = useData({ + swrOptions: { revalidateOnFocus: false, ...input.swrOptions }, + }) return useMemo( () => @@ -74,16 +73,6 @@ const useCart: HookHandler< [response] ) }, - onResponse(response) { - return Object.create(response, { - isEmpty: { - get() { - return (response.data?.lineItems.length ?? 0) <= 0 - }, - enumerable: true, - }, - }) - }, } const useWishlist: HookHandler< @@ -98,19 +87,6 @@ const useWishlist: HookHandler< url: '/api/bigcommerce/wishlist', method: 'GET', }, - swrOptions: { - revalidateOnFocus: false, - }, - onResponse(response) { - return Object.create(response, { - isEmpty: { - get() { - return (response.data?.lineItems.length ?? 0) <= 0 - }, - enumerable: true, - }, - }) - }, } export const bigcommerceProvider = { diff --git a/framework/commerce/cart/use-cart.tsx b/framework/commerce/cart/use-cart.tsx index 4ca20df69..9915c7478 100644 --- a/framework/commerce/cart/use-cart.tsx +++ b/framework/commerce/cart/use-cart.tsx @@ -1,7 +1,11 @@ -import { useMemo } from 'react' import Cookies from 'js-cookie' import type { Cart } from '../types' -import type { Prop, HookFetcherFn, UseHookInput } from '../utils/types' +import type { + Prop, + HookFetcherFn, + UseHookInput, + UseHookResponse, +} from '../utils/types' import useData from '../utils/use-data-2' import { Provider, useCommerce } from '..' @@ -14,12 +18,12 @@ export type UseCartHandler
= Prop< 'useCart' > -export type CartResponse
= ReturnType<
- Prop = UseHookInput = UseHookResponse<
+ UseCartHandler
+>
+
export type UseCart = Partial<
UseCartInput
> extends UseCartInput
@@ -36,8 +40,6 @@ export const fetcher: HookFetcherFn (
input: UseCartInput = {}
) {
@@ -46,49 +48,24 @@ export default function useCart (
const provider = providerRef.current
const opts = provider.cart?.useCart
- const { swrOptions, ...hookInput } = input
+ const fetcherFn = opts?.fetcher ?? fetcher
+ const useHook = opts?.useHook ?? ((ctx) => ctx.useData())
- return opts?.useHook!({
- input: hookInput,
- swrOptions,
+ const wrapper: typeof fetcher = (context) => {
+ context.input.cartId = Cookies.get(cartCookie)
+ return fetcherFn(context)
+ }
+
+ return useHook({
+ input,
useData(ctx) {
- const fetcherFn = opts?.fetcher ?? fetcher
- const wrapper: typeof fetcher = (context) => {
- context.input.cartId = Cookies.get(cartCookie)
- return fetcherFn(context)
- }
const response = useData(
- {
- ...opts,
- fetcher: wrapper,
- swrOptions: {
- ...opts.swrOptions,
- ...(ctx?.swrOptions ?? swrOptions),
- },
- },
+ { ...opts, fetcher: wrapper },
ctx?.input ?? [],
- provider.fetcher ?? fetcherRef.current
+ provider.fetcher ?? fetcherRef.current,
+ ctx?.swrOptions ?? input.swrOptions
)
return response
},
})
-
- // console.log(i)
-
- // const fetcherFn = opts?.fetcher ?? fetcher
- // const wrapper: typeof fetcher = (context) => {
- // context.input.cartId = Cookies.get(cartCookie)
- // return fetcherFn(context)
- // }
- // const response = useData(
- // { ...opts, fetcher: wrapper },
- // opts?.input ? opts.input(input ?? {}) : [],
- // provider.fetcher ?? fetcherRef.current
- // )
- // const memoizedResponse = useMemo(
- // () => (opts?.onResponse ? opts.onResponse(response) : response),
- // [response]
- // )
-
- // return memoizedResponse as CartResponse
}
diff --git a/framework/commerce/utils/types.ts b/framework/commerce/utils/types.ts
index d4d767ae1..7a4b73f93 100644
--- a/framework/commerce/utils/types.ts
+++ b/framework/commerce/utils/types.ts
@@ -71,19 +71,13 @@ export type HookHandler<
// Custom state added to the response object of SWR
State = {}
> = {
- input?(
- input: Input & { swrOptions?: SwrOptions }
- ): HookFetchInput | HookSwrInput
useHook?(context: {
- input: Input
- swrOptions?: SwrOptions
+ input: Input & { swrOptions?: SwrOptions }
useData(context?: {
input?: HookFetchInput | HookSwrInput
swrOptions?: SwrOptions
}): ResponseState
}): ResponseState & State
- swrOptions?: SwrOptions
- onResponse?(response: ResponseState): ResponseState & State
fetchOptions?: HookFetcherOptions
fetcher?: HookFetcherFn
normalizer?(data: Result): Data
@@ -104,8 +98,10 @@ export type UseHookParameters