) : (
@@ -20,15 +26,16 @@ const Cart = (props) => {
};
function CartItem({ item }) {
- const { name, price, img, qty } = item;
+ const { title, price, image, qty } = item;
return (
-
-
-
{name}
+
+
+
{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 (
);
}
@@ -26,5 +35,5 @@ function Nav() {
}
Navbar.propTypes = {
- cartItems: PropTypes.number,
+ cartItems: PropTypes.object,
};
diff --git a/shopping-cart/src/components/productCollection.jsx b/shopping-cart/src/components/productCollection.jsx
index 5fbe13f..c1db95a 100644
--- a/shopping-cart/src/components/productCollection.jsx
+++ b/shopping-cart/src/components/productCollection.jsx
@@ -1,12 +1,16 @@
import Product from "./products";
import PropTypes from "prop-types";
-export default function ProductCollection({ loading, items }) {
+import styles from "./productCollection.module.css";
+
+export default function ProductCollection({ loading, items, cart, setCart }) {
return (
-
+
{!loading
? items.map((item, index) => {
- return
;
+ return (
+
+ );
})
: null}
@@ -16,4 +20,6 @@ export default function ProductCollection({ loading, items }) {
ProductCollection.propTypes = {
loading: PropTypes.bool,
items: PropTypes.array,
+ cart: PropTypes.object,
+ setCart: PropTypes.func,
};
diff --git a/shopping-cart/src/components/products.jsx b/shopping-cart/src/components/products.jsx
index 344d9f0..9d42e8c 100644
--- a/shopping-cart/src/components/products.jsx
+++ b/shopping-cart/src/components/products.jsx
@@ -2,16 +2,33 @@ import PropTypes from "prop-types";
import styles from "./products.module.css";
-export default function Product({ item }) {
+export default function Product({ item, cart, setCart }) {
return (
{item.title}
${item.price}
+
);
}
Product.propTypes = {
item: PropTypes.object,
+ cart: PropTypes.object,
+ setCart: PropTypes.func,
};
diff --git a/shopping-cart/src/components/products.module.css b/shopping-cart/src/components/products.module.css
index a8c0d8a..b139a38 100644
--- a/shopping-cart/src/components/products.module.css
+++ b/shopping-cart/src/components/products.module.css
@@ -1,11 +1,15 @@
.card {
display: flex;
flex-direction: column;
- max-width: 350px;
- justify-content: center;
- align-items: center;
+ max-width: calc(100% / 5);
+ justify-content: end;
+ /*flex: 1;*/
+ padding: 1.5rem;
+ margin: 1.5rem;
}
.img {
- max-width: 250px;
+ width: 100%;
+ height: auto;
+ vertical-align: middle;
}
diff --git a/shopping-cart/src/errorPage.jsx b/shopping-cart/src/errorPage.jsx
index 129d8a2..82280e1 100644
--- a/shopping-cart/src/errorPage.jsx
+++ b/shopping-cart/src/errorPage.jsx
@@ -3,10 +3,12 @@ import { Link } from "react-router-dom";
function ErrorPage() {
return (
-
Oh no, this route doesn't exist!
-
- You can go back to the home page by clicking here, though!
-
+
Oh no, this route doesn't exist!
+
+ You can go back to the home page by clicking
+ here
+ though!
+
);
}
diff --git a/shopping-cart/src/routes.jsx b/shopping-cart/src/routes.jsx
index 57905bd..0e83a26 100644
--- a/shopping-cart/src/routes.jsx
+++ b/shopping-cart/src/routes.jsx
@@ -1,5 +1,6 @@
import App from "./App";
import Cart from "./Cart";
+import Main from "./components/main";
import Store from "./Store";
import ErrorPage from "./errorPage";
@@ -10,12 +11,16 @@ const routes = [
errorElement:
,
children: [
{
- path: ":path",
- element:
,
+ path: "/",
+ element:
,
index: true,
},
{
- path: "store/:path",
+ path: "store",
+ element:
,
+ },
+ {
+ path: "bag",
element:
,
},
],