From 31446f3cd0d96464fa9a475aa78cace38bfe0583 Mon Sep 17 00:00:00 2001 From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com> Date: Thu, 7 Jul 2022 14:10:00 -0500 Subject: [PATCH] may need to backtrack? --- client/src/components/Cart/Cart.tsx | 58 +++++-------------------- client/src/components/Cart/CartItem.tsx | 8 ++-- 2 files changed, 15 insertions(+), 51 deletions(-) diff --git a/client/src/components/Cart/Cart.tsx b/client/src/components/Cart/Cart.tsx index d12a19a..263d949 100644 --- a/client/src/components/Cart/Cart.tsx +++ b/client/src/components/Cart/Cart.tsx @@ -12,62 +12,26 @@ function Cart() { const [data, setData] = useState(); const [subtotal, setSubtotal] = useState('loading...'); - // on mount - useEffect(() => { - if (!state) return; - dispatch({ type: ActionType.UPDATESUBTOTAL, payload: getSubtotal(data) }); - }, []); - - useEffect(() => { - console.log(state.cart); - setSubtotal(state.cart.subtotal); - }, [state.cart.subtotal]); - - useEffect(() => { - if (!state.cart.contents) return; - - let newProducts: Array = []; - for (let item of state.cart.contents) { - const withQuantity = { - ...item, - quantity: 1 - } - const foundItem = newProducts.findIndex((res) => res.name === item.name); - if (foundItem === -1) { - newProducts.push(withQuantity); - } else { - // @ts-ignore - newProducts[foundItem].quantity += 1; - } - } - - for (let item of newProducts) { - if (typeof item.quantity !== 'number') { - throw new Error("Quantity is possibly undefined in Cart.tsx"); - } - item.quantity = item.quantity / 2; - } - - setData(newProducts); - }, [state]); - - const updateQuantity = useCallback((product: Product, newQuantity: number) => { - const updated = product; - updated.quantity = newQuantity; - }, []); - return ( { state.user.firstName ? -

Hello, {state.user.firstName}!

: -

Your cart is empty! Please log in to build your own.

+

Please log in to start your cart.

}
- { data && data.map((product: Product) => ) } + { state.cart && + + <> +

You have {state.cart.contents.length} items in your cart!

+
+ {state.cart.contents.map((product: Product) => )} +
+ + + }
diff --git a/client/src/components/Cart/CartItem.tsx b/client/src/components/Cart/CartItem.tsx index 9561f25..3b1ad0a 100644 --- a/client/src/components/Cart/CartItem.tsx +++ b/client/src/components/Cart/CartItem.tsx @@ -1,12 +1,12 @@ import { useEffect, useState } from "react"; import { v4 } from "uuid"; -function CartItem({ product, updateQuantity }: any) { +function CartItem({ product }: any) { const [quantity, setQuantity] = useState(product.quantity || 0); - useEffect(() => { - updateQuantity(product, quantity); - }, [quantity]); + // useEffect(() => { + // updateQuantity(product, quantity); + // }, [quantity]); return (