From da6d76e59899290f97ff53fb70b75709915f6d7e Mon Sep 17 00:00:00 2001
From: Mike Smith <89040888+smiggiddy@users.noreply.github.com>
Date: Tue, 15 Oct 2024 06:47:25 -0400
Subject: [PATCH] adding more logic and components
---
shopping-cart/src/Store.jsx | 73 ++++++-------------
shopping-cart/src/components/navbar.jsx | 2 +-
.../src/components/navbar.module.css | 8 +-
.../src/components/productCollection.jsx | 9 ++-
.../components/productCollection.module.css | 16 ++++
.../src/components/productDetails.jsx | 47 ++++++++++++
shopping-cart/src/components/products.jsx | 19 +----
.../src/components/products.module.css | 10 ---
shopping-cart/src/css/cart.module.css | 3 +
shopping-cart/src/routes.jsx | 5 ++
10 files changed, 109 insertions(+), 83 deletions(-)
create mode 100644 shopping-cart/src/components/productCollection.module.css
create mode 100644 shopping-cart/src/components/productDetails.jsx
create mode 100644 shopping-cart/src/css/cart.module.css
diff --git a/shopping-cart/src/Store.jsx b/shopping-cart/src/Store.jsx
index 1492297..c0dc81e 100644
--- a/shopping-cart/src/Store.jsx
+++ b/shopping-cart/src/Store.jsx
@@ -5,13 +5,30 @@ import { useOutletContext } from "react-router-dom";
export default function Store() {
const [cart, setCart, items, setItems] = useOutletContext();
const [loading, setLoading] = useState(true);
- const url = 'https://fakestoreapi.com/products?limit=5'
- console.table(items)
+ useFakeStoreAPIData(items, setItems, loading, setLoading);
+
+ return (
+
+
Smig.Tech Coaching Store
+
+
+ );
+}
+
+function useFakeStoreAPIData(items, setItems, loading, setLoading) {
useEffect(() => {
- if (items === null) {
+ if (items !== null) {
+ setLoading(false);
+ return;
+ }
- fetch(url, { mode: "cors" })
+ fetch("https://fakestoreapi.com/products?limit=5", { mode: "cors" })
.then((response) => {
if (response.status >= 400) {
throw new Error("unable to fetch items");
@@ -32,51 +49,5 @@ export default function Store() {
})
.catch((error) => console.log(error))
.finally(() => setLoading(false));
- } else {
- setLoading(false);
- }
- }, []);
-
- return (
-
-
Smig.Tech Coaching Store
-
-
- );
+ }, [items, setItems, loading, setLoading]);
}
-
-// function useFakeStoreAPI() {
-// const [items, setItems] = useState(null);
-// const [loading, setLoading] = useState(true);
-
-// useEffect(() => {
-// fetch("https://fakestoreapi.com/products?limit=5", { mode: "cors" })
-// .then((response) => {
-// if (response.status >= 400) {
-// 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,
-// id: crypto.randomUUID(),
-// });
-// });
-// setItems(arr);
-// })
-// .catch((error) => console.log(error))
-// .finally(() => setLoading(false));
-// }, []);
-
-// return { items, loading };
-// }
diff --git a/shopping-cart/src/components/navbar.jsx b/shopping-cart/src/components/navbar.jsx
index 929c809..8871061 100644
--- a/shopping-cart/src/components/navbar.jsx
+++ b/shopping-cart/src/components/navbar.jsx
@@ -6,7 +6,7 @@ export default function Navbar({ cartItems }) {
const sumCartItems = Object.keys(cartItems).length;
return (
-