Skip to content

Commit

Permalink
Merge branch 'main' into change-search-bar
Browse files Browse the repository at this point in the history
  • Loading branch information
s33chin committed Jun 11, 2024
2 parents d3cbd48 + b7e7ac4 commit 87f5daf
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
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 87f5daf

Please sign in to comment.