mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 07:26:59 +00:00
- Changed the loging fetch - Store the token and user id in cookie - Ue the cookie to make user calls like get user
36 lines
981 B
TypeScript
36 lines
981 B
TypeScript
import { useMemo } from 'react'
|
|
import { SWRHook } from '@commerce/utils/types'
|
|
import useCart, { UseCart } from '@commerce/cart/use-cart'
|
|
import epClient from '../utils/ep-client'
|
|
import normalizeCart from '../utils/normalize-cart'
|
|
|
|
export default useCart as UseCart<typeof handler>
|
|
|
|
export const handler: SWRHook<any> = {
|
|
fetchOptions: {
|
|
query: '',
|
|
},
|
|
async fetcher({fetch}) {
|
|
const {data:cartData} = await epClient.Cart().Get();
|
|
const {data:cartItems} = await epClient.Cart().Items();
|
|
return normalizeCart(cartData, cartItems);
|
|
},
|
|
useHook: ({ useData }) => (input) => {
|
|
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]
|
|
)
|
|
},
|
|
}
|