From 30939edd9b3c1ad3dc1b666407da5217b0e3339b Mon Sep 17 00:00:00 2001 From: No0ne003 Date: Wed, 20 Mar 2024 17:34:14 +0000 Subject: [PATCH] load more data project - complete the products generate from api and it's responsive yay ._. --- src/App.jsx | 5 +++- src/pages/Home.jsx | 1 + src/pages/LoadMoreData.jsx | 60 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/pages/LoadMoreData.jsx diff --git a/src/App.jsx b/src/App.jsx index 671dbdc..9e3f740 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -9,6 +9,7 @@ import Accordion from "@/pages/accordion/accordion"; import RandomColor from "@/pages/RandomColor"; import StarRating from "@/pages/StarRating"; import ImageSlider from "@/pages/ImageSlider" +import LoadMoreData from "./pages/LoadMoreData"; function App() { return ( @@ -24,7 +25,9 @@ function App() { {/* Star Rating */} } /> {/* image Slider */} - } /> + } /> + {/* load more data button */} + } /> {/* Error Page */} } /> diff --git a/src/pages/Home.jsx b/src/pages/Home.jsx index b47c586..fe1b064 100644 --- a/src/pages/Home.jsx +++ b/src/pages/Home.jsx @@ -9,6 +9,7 @@ function Home() { random Color Generator star rating image slider + load more button ); } diff --git a/src/pages/LoadMoreData.jsx b/src/pages/LoadMoreData.jsx new file mode 100644 index 0000000..cfde629 --- /dev/null +++ b/src/pages/LoadMoreData.jsx @@ -0,0 +1,60 @@ +import { Button } from "@/components/ui/button"; +import { useEffect, useState } from "react"; + +function LoadMoreData() { + const [loading, setLoading] = useState(false); + const [products, setProducts] = useState([]); + const [count, setCount] = useState(0); + + async function fetchProducts() { + try { + setLoading(true); + const response = await fetch( + `https://dummyjson.com/products?limit=20&skip=${count === 0 ? 0 : count * 20}`, + ); + + const result = await response.json(); + + if (result && result.products && result.products.length) { + setProducts(result.products); + setLoading(false); + } + } catch (e) { + console.log(e); + setLoading(false); + } + } + + useEffect(() => { + fetchProducts(); + }, []); + + if (loading) { + return
Loading data ! Please wait man
; + } + + return ( +
+
+ {products && products.length + ? products.map((item) => ( +
+ {item.title} +

{item.title}

+
+ )) + : null} +
+
+ +
+
+ ); +} + +export default LoadMoreData;