-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
load more data project - complete the products generate from api and …
…it's responsive yay ._.
- Loading branch information
Showing
3 changed files
with
65 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <div>Loading data ! Please wait man</div>; | ||
} | ||
|
||
return ( | ||
<div | ||
className={`container flex flex-1 flex-col items-center justify-start gap-8 my-10 w-full`} | ||
> | ||
<div className="grid grid-cols-1 md:grid-cols-3 lg:grid-cols-4 gap-3"> | ||
{products && products.length | ||
? products.map((item) => ( | ||
<div | ||
className="p-5 border-border border-2 flex flex-col items-center justify-center gap-3" | ||
key={item.id} | ||
> | ||
<img src={item.thumbnail} alt={item.title} /> | ||
<p className="font-mono">{item.title}</p> | ||
</div> | ||
)) | ||
: null} | ||
</div> | ||
<div className="btn-container"> | ||
<Button variant="secondary">Load More Products</Button> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default LoadMoreData; |