Skip to content

Commit

Permalink
Adding paginator for multiple results
Browse files Browse the repository at this point in the history
  • Loading branch information
Codeyg12 committed Jun 7, 2024
1 parent 5447223 commit b7e7ac4
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 13 deletions.
29 changes: 16 additions & 13 deletions frontend/src/App.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { useEffect, useState } from "react";
import Navbar from "./components/Navbar";
import Result from "./components/Result";
import ResultsCart from "./components/ResultsCart";
import SearchBar from "./components/SearchBar";
import ResultsPaginator from "./components/ResultsPaginator";

function App() {
const [results, setResults] = useState([]);
Expand All @@ -13,6 +13,12 @@ function App() {
{ title: "Result 2", summary: "This is the second result" },
{ title: "Result 3", summary: "This is the third result" },
{ title: "Result 4", summary: "This is the last result" },
{ title: "Result 5", summary: "This is the last result" },
{ title: "Result 6", summary: "This is the last result" },
{ title: "Result 7", summary: "This is the last result" },
{ title: "Result 8", summary: "This is the last result" },
{ title: "Result 9", summary: "This is the last result" },
{ title: "Result 10", summary: "This is the last result" },
];

useEffect(() => {
Expand All @@ -30,25 +36,22 @@ function App() {
<main id="home">
<section className="grid1">
<SearchBar />
<div className="resultsDisplay">
{results.map((result, index) => (
<Result
key={index}
title={result.title}
summary={result.summary}
handleAddToCart={handleAddToCart}
/>
))}
</div>
<ResultsPaginator
results={results}
handleAddToCart={handleAddToCart}
/>
</section>
<aside className="grid2">
<ResultsCart cart={cart} />
</aside>
</main>
<footer id="contact">
<p>
Github: <a href="https://github.com/s33chin/GovSync">GovSync</a> by
Microsoft
Github:{" "}
<a href="https://github.com/s33chin/GovSync" target="_blank">
GovSync
</a>{" "}
by Microsoft
</p>
</footer>
</div>
Expand Down
28 changes: 28 additions & 0 deletions frontend/src/components/Paginator.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from "react";

const Paginator = ({ resultsPerPage, totalResults, paginate, currentPage }) => {
const pageNumbers = [];

for (let i = 1; i <= Math.ceil(totalResults / resultsPerPage); i++) {
pageNumbers.push(i);
}

return (
<nav>
<ul className="pagination">
{pageNumbers.map((num) => (
<li
key={num}
className={`page-item ${currentPage === num ? "active" : ""}`}
>
<a onClick={() => paginate(num)} href="!#" className="page-link">
{num}
</a>
</li>
))}
</ul>
</nav>
);
};

export default Paginator;
36 changes: 36 additions & 0 deletions frontend/src/components/ResultsPaginator.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React, { useState } from "react";
import Paginator from "./Paginator";
import Result from "./Result";

const ResultsPaginator = ({ results, handleAddToCart }) => {
const [currentPage, setCurrentPage] = useState(1);
const resultsPerPage = 6;

const indexOfLastResult = currentPage * resultsPerPage;
const indexOfFirstResult = indexOfLastResult - resultsPerPage;
const currentResults = results.slice(indexOfFirstResult, indexOfLastResult);

const paginate = (pageNumber) => setCurrentPage(pageNumber);
return (
<>
<div className="resultsDisplay">
{currentResults.map((result, index) => (
<Result
key={index}
title={result.title}
summary={result.summary}
handleAddToCart={handleAddToCart}
/>
))}
</div>
<Paginator
resultsPerPage={resultsPerPage}
totalResults={results.length}
paginate={paginate}
currentPage={currentPage}
/>
</>
);
};

export default ResultsPaginator;

0 comments on commit b7e7ac4

Please sign in to comment.