From 0ad9ac0d5d863f48ba59da44fc669e5551ba617c Mon Sep 17 00:00:00 2001 From: Luis Alvarez Date: Sun, 4 Oct 2020 13:12:41 -0500 Subject: [PATCH] Hook changes --- lib/bigcommerce/{cart.tsx => cart/index.tsx} | 0 lib/bigcommerce/cart/use-add-item.tsx | 19 +++++++++++++++++++ lib/bigcommerce/index.tsx | 20 +++++++++----------- lib/commerce/{cart.tsx => cart/index.tsx} | 5 +++-- lib/commerce/cart/use-add-item.tsx | 16 ++++++++++++++++ lib/commerce/index.tsx | 16 ++++++++++++++-- 6 files changed, 61 insertions(+), 15 deletions(-) rename lib/bigcommerce/{cart.tsx => cart/index.tsx} (100%) create mode 100644 lib/bigcommerce/cart/use-add-item.tsx rename lib/commerce/{cart.tsx => cart/index.tsx} (85%) create mode 100644 lib/commerce/cart/use-add-item.tsx diff --git a/lib/bigcommerce/cart.tsx b/lib/bigcommerce/cart/index.tsx similarity index 100% rename from lib/bigcommerce/cart.tsx rename to lib/bigcommerce/cart/index.tsx diff --git a/lib/bigcommerce/cart/use-add-item.tsx b/lib/bigcommerce/cart/use-add-item.tsx new file mode 100644 index 000000000..6f84ea1a0 --- /dev/null +++ b/lib/bigcommerce/cart/use-add-item.tsx @@ -0,0 +1,19 @@ +import { Fetcher } from '@lib/commerce' +import { default as useCartAddItem } from '@lib/commerce/cart/use-add-item' +import { Cart } from '.' + +async function fetcher(fetch: Fetcher, { item }: { item: any }) { + const res = await fetch({ url: '/api/cart' }) + + // { + // method: 'POST', + // headers: { + // 'Content-Type': 'application/json', + // }, + // body: JSON.stringify({ product }), + // } +} + +export default function useAddItem() { + return useCartAddItem(fetcher) +} diff --git a/lib/bigcommerce/index.tsx b/lib/bigcommerce/index.tsx index 006d5389c..c72d90a2a 100644 --- a/lib/bigcommerce/index.tsx +++ b/lib/bigcommerce/index.tsx @@ -21,19 +21,17 @@ async function getError(res: Response) { return { message: await getText(res) } } -async function fetcher(url: string, query: string) { - const res = await fetch(url) - - if (res.ok) { - return res.json() - } - - throw await getError(res) -} - export const bigcommerceConfig: CommerceConfig = { locale: 'en-us', - fetcher, + async fetcher({ url, query }) { + const res = await fetch(url!) + + if (res.ok) { + return res.json() + } + + throw await getError(res) + }, } export type BigcommerceConfig = Partial diff --git a/lib/commerce/cart.tsx b/lib/commerce/cart/index.tsx similarity index 85% rename from lib/commerce/cart.tsx rename to lib/commerce/cart/index.tsx index c11ecd017..dc90eb89b 100644 --- a/lib/commerce/cart.tsx +++ b/lib/commerce/cart/index.tsx @@ -1,6 +1,6 @@ import { createContext, useContext, FC } from 'react' import useSWR, { responseInterface } from 'swr' -import { useCommerce } from '.' +import { useCommerce } from '..' export type CartResponse = responseInterface & { isEmpty: boolean @@ -18,7 +18,8 @@ function getCartCookie() { } const CartProvider: FC = ({ children, query, url }) => { - const { fetcher } = useCommerce() + const { fetcher: fetch } = useCommerce() + const fetcher = (url?: string, query?: string) => fetch({ url, query }) const cartId = getCartCookie() const response = useSWR(() => (cartId ? [url, query] : null), fetcher) diff --git a/lib/commerce/cart/use-add-item.tsx b/lib/commerce/cart/use-add-item.tsx new file mode 100644 index 000000000..05ca475de --- /dev/null +++ b/lib/commerce/cart/use-add-item.tsx @@ -0,0 +1,16 @@ +import { Fetcher, useCommerce } from '..' + +export default function useAddItem( + fetcher: (fetch: Fetcher, input: Input) => T | Promise +) { + const { fetcher: fetch } = useCommerce() + + return async function addItem(input: Input) { + const data = fetcher(fetch, input) + + // TODO: Using the state of the cart provider, update the saved cart + // return mutate('/api/cart') + + return data + } +} diff --git a/lib/commerce/index.tsx b/lib/commerce/index.tsx index 2659def45..eebeded90 100644 --- a/lib/commerce/index.tsx +++ b/lib/commerce/index.tsx @@ -1,4 +1,11 @@ -import { createContext, ReactNode, useContext } from 'react' +import { + createContext, + ReactNode, + useCallback, + useContext, + useMemo, +} from 'react' +import useSWR from 'swr' const Commerce = createContext(null) @@ -12,7 +19,12 @@ export type CommerceConfig = { locale: string } -export type Fetcher = (...args: any) => T | Promise +export type Fetcher = (options: FetcherOptions) => T | Promise + +export type FetcherOptions = { + url?: string + query?: string +} export function CommerceProvider({ children, config }: CommerceProps) { if (!config) {