Skip to content

Commit

Permalink
set limit for api request and desable button after limit
Browse files Browse the repository at this point in the history
  • Loading branch information
No0ne003 committed Mar 20, 2024
1 parent abe19a5 commit 4f5daf5
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/pages/LoadMoreData.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ function LoadMoreData() {
const [loading, setLoading] = useState(false);
const [products, setProducts] = useState([]);
const [count, setCount] = useState(0);
const [disableButton, setDisableButton] = useState(false);

async function fetchProducts() {
try {
Expand All @@ -19,7 +20,6 @@ function LoadMoreData() {
setProducts((prevData) => [...prevData, ...result.products]);
setLoading(false);
}
console.log(result)
} catch (e) {
console.log(e);
setLoading(false);
Expand All @@ -29,10 +29,14 @@ function LoadMoreData() {
useEffect(() => {
fetchProducts();
}, [count]);
useEffect(() => {
if (products && products.length === 100) setDisableButton(true);
}, [products]);

if (loading) {
return <div>Loading data ! Please wait man</div>;
}
console.log(products)

return (
<div
Expand All @@ -52,9 +56,14 @@ function LoadMoreData() {
: null}
</div>
<div className="btn-container">
<Button onClick={() => setCount(count + 1)} variant="secondary">
<Button
disabled={disableButton}
onClick={() => setCount(count + 1)}
variant="secondary"
>
Load More Products
</Button>
{disableButton ? <p>you have reached to 100 products</p> : null}
</div>
</div>
);
Expand Down

0 comments on commit 4f5daf5

Please sign in to comment.