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 (