mirror of
https://github.com/vercel/commerce.git
synced 2025-07-27 04:01:23 +00:00
Moved handler to each hook
This commit is contained in:
@@ -1,4 +1,44 @@
|
||||
import useCommerceCart, { UseCart } from '@commerce/cart/use-cart'
|
||||
import { useMemo } from 'react'
|
||||
import type { ShopifyProvider } from '..'
|
||||
|
||||
import useCommerceCart, {
|
||||
FetchCartInput,
|
||||
UseCart,
|
||||
} from '@commerce/cart/use-cart'
|
||||
|
||||
import { Cart } from '@commerce/types'
|
||||
import { HookHandler } from '@commerce/utils/types'
|
||||
|
||||
import fetcher from './utils/fetcher'
|
||||
import getCheckoutQuery from '@framework/utils/queries/get-checkout-query'
|
||||
|
||||
export default useCommerceCart as UseCart<ShopifyProvider>
|
||||
|
||||
export const handler: HookHandler<
|
||||
Cart | null,
|
||||
{},
|
||||
FetchCartInput,
|
||||
{ isEmpty?: boolean }
|
||||
> = {
|
||||
fetchOptions: {
|
||||
query: getCheckoutQuery,
|
||||
},
|
||||
fetcher,
|
||||
useHook({ input, useData }) {
|
||||
const response = useData({
|
||||
swrOptions: { revalidateOnFocus: false, ...input.swrOptions },
|
||||
})
|
||||
return useMemo(
|
||||
() =>
|
||||
Object.create(response, {
|
||||
isEmpty: {
|
||||
get() {
|
||||
return (response.data?.lineItems.length ?? 0) <= 0
|
||||
},
|
||||
enumerable: true,
|
||||
},
|
||||
}),
|
||||
[response]
|
||||
)
|
||||
},
|
||||
}
|
||||
|
Reference in New Issue
Block a user