4
0
forked from crowetic/commerce
commerce/lib/commerce/cart.tsx
2020-10-03 06:45:09 -05:00

39 lines
1010 B
TypeScript

import { createContext, useContext, FC } from 'react'
import useSWR, { responseInterface } from 'swr'
import { useCommerce } from '.'
export type CartResponse<C> = responseInterface<C, Error> & {
isEmpty: boolean
}
export type CartProviderProps =
| { query: string; url?: string }
| { query?: string; url: string }
const CartContext = createContext<CartResponse<any> | null>(null)
function getCartCookie() {
// TODO: Figure how the cart should be persisted
return null
}
const CartProvider: FC<CartProviderProps> = ({ children, query, url }) => {
const { fetcher } = useCommerce()
const cartId = getCartCookie()
const response = useSWR(() => (cartId ? [url, query] : null), fetcher)
// TODO: Do something to make this prop work
const isEmpty = true
return (
<CartContext.Provider value={{ ...response, isEmpty }}>
{children}
</CartContext.Provider>
)
}
function useCart<C>() {
return useContext(CartContext) as CartResponse<C>
}
export { CartProvider, useCart }