4
0
forked from crowetic/commerce

Fixed bugs and updated hooks

This commit is contained in:
Luis Alvarez 2020-10-05 00:39:54 -05:00
parent a3cf05fbd2
commit f8df1c1879
5 changed files with 17 additions and 14 deletions

View File

@ -36,8 +36,6 @@ const ProductView: FC<Props> = ({ product, productData, className }) => {
}) })
} }
console.log('PRODUCT', product)
return ( return (
<div className={cn(s.root, className)}> <div className={cn(s.root, className)}>
<div className="absolute"> <div className="absolute">

View File

@ -1,7 +1,7 @@
import type { Fetcher } from '@lib/commerce' import type { Fetcher } from '@lib/commerce'
import { default as useCartAddItem } from '@lib/commerce/cart/use-add-item' import { default as useCartAddItem } from '@lib/commerce/cart/use-add-item'
import type { Item } from '../api/cart' import type { Item } from '../api/cart'
import { Cart } from '.' import { Cart, useCart } from '.'
export type { Item } export type { Item }
@ -19,5 +19,13 @@ function fetcher(fetch: Fetcher<Cart>, { item }: { item: Item }) {
} }
export default function useAddItem() { export default function useAddItem() {
return useCartAddItem<Cart, { item: Item }>(fetcher) const { mutate } = useCart()
const fn = useCartAddItem<Cart, { item: Item }>(fetcher)
const addItem: typeof fn = async (input) => {
const data = await fn(input)
mutate(data)
return data
}
return addItem
} }

View File

@ -34,7 +34,6 @@ export const bigcommerceConfig: CommerceConfig = {
if (res.ok) { if (res.ok) {
const { data } = await res.json() const { data } = await res.json()
console.log('DATA', data)
return data return data
} }

View File

@ -1,4 +1,4 @@
import { createContext, useContext, FC } from 'react' import { createContext, useContext, FC, useCallback } from 'react'
import useSWR, { responseInterface } from 'swr' import useSWR, { responseInterface } from 'swr'
import Cookies from 'js-cookie' import Cookies from 'js-cookie'
import { useCommerce } from '..' import { useCommerce } from '..'
@ -17,10 +17,13 @@ const CartProvider: FC<CartProviderProps> = ({ children, query, url }) => {
const { fetcher: fetch, cartCookie } = useCommerce() const { fetcher: fetch, cartCookie } = useCommerce()
const fetcher = (url?: string, query?: string) => fetch({ url, query }) const fetcher = (url?: string, query?: string) => fetch({ url, query })
const cartId = Cookies.get(cartCookie) const cartId = Cookies.get(cartCookie)
const response = useSWR(() => (cartId ? [url, query] : null), fetcher) const response = useSWR(() => (cartId ? [url, query] : null), fetcher, {
revalidateOnFocus: false,
})
return ( return (
<CartContext.Provider value={{ ...response, isEmpty: true }}> // Avoid destructuring in `response` so we don't trigger the getters early
<CartContext.Provider value={Object.assign(response, { isEmpty: true })}>
{children} {children}
</CartContext.Provider> </CartContext.Provider>
) )

View File

@ -6,11 +6,6 @@ export default function useAddItem<T, Input>(
const { fetcher: fetch } = useCommerce() const { fetcher: fetch } = useCommerce()
return async function addItem(input: Input) { return async function addItem(input: Input) {
const data = fetcher(fetch, input) return fetcher(fetch, input)
// TODO: Using the state of the cart provider, update the saved cart
// return mutate('/api/cart')
return data
} }
} }