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 ( -