From f34aa95bfe9a633e29d4146f6c187cbeb8b580cc Mon Sep 17 00:00:00 2001 From: Mike Smith <89040888+smiggiddy@users.noreply.github.com> Date: Mon, 14 Oct 2024 07:23:02 -0400 Subject: [PATCH] basic functionality complete --- shopping-cart/src/App.jsx | 10 +++---- shopping-cart/src/Cart.jsx | 27 ++++++++++++------- shopping-cart/src/Store.jsx | 21 ++++++++++++--- shopping-cart/src/components/navbar.jsx | 13 +++++++-- .../src/components/productCollection.jsx | 12 ++++++--- shopping-cart/src/components/products.jsx | 19 ++++++++++++- .../src/components/products.module.css | 12 ++++++--- shopping-cart/src/errorPage.jsx | 10 ++++--- shopping-cart/src/routes.jsx | 11 +++++--- 9 files changed, 98 insertions(+), 37 deletions(-) diff --git a/shopping-cart/src/App.jsx b/shopping-cart/src/App.jsx index 58625d5..0c720d4 100644 --- a/shopping-cart/src/App.jsx +++ b/shopping-cart/src/App.jsx @@ -1,16 +1,14 @@ import { useState } from "react"; -import { Outlet, useParams } from "react-router-dom"; -import Main from "./components/main"; +import { Outlet } from "react-router-dom"; import "./App.css"; import Navbar from "./components/navbar"; function App() { - const { path } = useParams(); - const [cartItems, setCartItems] = useState(0); + const [cart, setCart] = useState({}); return ( <> - - {path === "cart" ? : path === "store" ? :
} + + ); } diff --git a/shopping-cart/src/Cart.jsx b/shopping-cart/src/Cart.jsx index d42956f..f303374 100644 --- a/shopping-cart/src/Cart.jsx +++ b/shopping-cart/src/Cart.jsx @@ -1,14 +1,20 @@ import PropTypes from "prop-types"; +import styles from "./css/cart.module.css"; +import { useOutletContext } from "react-router-dom"; + +const Cart = () => { + const [cart, setCart] = useOutletContext(); + + const cartItems = Object.keys(cart); -const Cart = (props) => { return ( <>
- {props.cart ? ( + {cart ? (

Cart

- {props.cart.map((item) => ( - + {cartItems.map((item, index) => ( + ))}
) : ( @@ -20,15 +26,16 @@ const Cart = (props) => { }; function CartItem({ item }) { - const { name, price, img, qty } = item; + const { title, price, image, qty } = item; return ( -
- {name} -

{name}

+
+ {title} +

{title}

Qty: {qty}

-

{price}

-

{price * qty}

+

Price: {price}

+

Total: {price * qty}

+

{item.id}

); } diff --git a/shopping-cart/src/Store.jsx b/shopping-cart/src/Store.jsx index cb6c631..2992cd7 100644 --- a/shopping-cart/src/Store.jsx +++ b/shopping-cart/src/Store.jsx @@ -1,12 +1,20 @@ import { useEffect, useState } from "react"; import ProductCollection from "./components/productCollection"; +import { useOutletContext } from "react-router-dom"; -export default function Store(props) { +export default function Store() { + const [cart, setCart] = useOutletContext(); const { items, loading } = useFakeStoreAPI(); + return (

Smig.Tech Coaching Store

- +
); } @@ -19,14 +27,19 @@ function useFakeStoreAPI() { fetch("https://fakestoreapi.com/products?limit=5", { mode: "cors" }) .then((response) => { if (response.status >= 400) { - return { error: "unable to fetch items" }; + throw new Error("unable to fetch items"); } return response.json(); }) .then((response) => { const arr = []; response.forEach((item) => { - arr.push({ title: item.title, price: item.price, image: item.image }); + arr.push({ + title: item.title, + price: item.price, + image: item.image, + id: crypto.randomUUID(), + }); }); setItems(arr); }) diff --git a/shopping-cart/src/components/navbar.jsx b/shopping-cart/src/components/navbar.jsx index 5be9659..929c809 100644 --- a/shopping-cart/src/components/navbar.jsx +++ b/shopping-cart/src/components/navbar.jsx @@ -3,11 +3,20 @@ import { Link } from "react-router-dom"; import PropTypes from "prop-types"; export default function Navbar({ cartItems }) { + const sumCartItems = Object.keys(cartItems).length; + return (