2024-10-13 11:41:36 -04:00
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
import ProductCollection from "./components/productCollection";
|
2024-10-14 07:23:02 -04:00
|
|
|
import { useOutletContext } from "react-router-dom";
|
2024-10-13 11:41:36 -04:00
|
|
|
|
2024-10-14 07:23:02 -04:00
|
|
|
export default function Store() {
|
2024-10-14 20:56:58 -04:00
|
|
|
const [cart, setCart, items, setItems] = useOutletContext();
|
2024-10-13 11:41:36 -04:00
|
|
|
const [loading, setLoading] = useState(true);
|
2024-10-14 20:56:58 -04:00
|
|
|
const url = 'https://fakestoreapi.com/products?limit=5'
|
2024-10-13 11:41:36 -04:00
|
|
|
|
2024-10-14 20:56:58 -04:00
|
|
|
console.table(items)
|
2024-10-13 11:41:36 -04:00
|
|
|
useEffect(() => {
|
2024-10-14 20:56:58 -04:00
|
|
|
if (items === null) {
|
|
|
|
|
|
|
|
fetch(url, { mode: "cors" })
|
2024-10-13 11:41:36 -04:00
|
|
|
.then((response) => {
|
|
|
|
if (response.status >= 400) {
|
2024-10-14 07:23:02 -04:00
|
|
|
throw new Error("unable to fetch items");
|
2024-10-13 11:41:36 -04:00
|
|
|
}
|
|
|
|
return response.json();
|
|
|
|
})
|
|
|
|
.then((response) => {
|
|
|
|
const arr = [];
|
|
|
|
response.forEach((item) => {
|
2024-10-14 07:23:02 -04:00
|
|
|
arr.push({
|
|
|
|
title: item.title,
|
|
|
|
price: item.price,
|
|
|
|
image: item.image,
|
|
|
|
id: crypto.randomUUID(),
|
|
|
|
});
|
2024-10-13 11:41:36 -04:00
|
|
|
});
|
|
|
|
setItems(arr);
|
|
|
|
})
|
|
|
|
.catch((error) => console.log(error))
|
|
|
|
.finally(() => setLoading(false));
|
2024-10-14 20:56:58 -04:00
|
|
|
} else {
|
|
|
|
setLoading(false);
|
|
|
|
}
|
2024-10-13 11:41:36 -04:00
|
|
|
}, []);
|
|
|
|
|
2024-10-14 20:56:58 -04:00
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<h1>Smig.Tech Coaching Store</h1>
|
|
|
|
<ProductCollection
|
|
|
|
loading={loading}
|
|
|
|
items={items}
|
|
|
|
cart={cart}
|
|
|
|
setCart={setCart}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
2024-10-13 11:41:36 -04:00
|
|
|
}
|
2024-10-14 20:56:58 -04:00
|
|
|
|
|
|
|
// 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 };
|
|
|
|
// }
|