Implement cart

This commit is contained in:
goncy
2021-08-25 11:14:27 -03:00
parent 7bdefa3f48
commit 72cc34d8c7
14 changed files with 579 additions and 162 deletions

View File

@@ -1,42 +1,33 @@
import type { GetCartHook } from '@commerce/types/cart'
import { useMemo } from 'react'
import { SWRHook } from '@commerce/utils/types'
import useCart, { UseCart } from '@commerce/cart/use-cart'
export default useCart as UseCart<typeof handler>
export const handler: SWRHook<any> = {
export const handler: SWRHook<GetCartHook> = {
fetchOptions: {
query: '',
url: '/api/cart',
method: 'GET',
},
async fetcher() {
return {
id: '',
createdAt: '',
currency: { code: '' },
taxesIncluded: '',
lineItems: [],
lineItemsSubtotalPrice: '',
subtotalPrice: 0,
totalPrice: 0,
}
},
useHook:
({ useData }) =>
(input) => {
useHook: ({ useData }) =>
function useHook(input) {
const response = useData({
swrOptions: { revalidateOnFocus: false, ...input?.swrOptions },
})
return useMemo(
() =>
Object.create(
{},
{
isEmpty: {
get() {
return true
},
enumerable: true,
Object.create(response, {
isEmpty: {
get() {
return (response.data?.lineItems?.length ?? 0) <= 0
},
}
),
[]
enumerable: true,
},
}),
[response]
)
},
}