Skip to content

Commit

Permalink
feat: pagination component logic (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
prathameshbelurkar committed Jun 23, 2024
1 parent 1f6794f commit 65af52d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/features/bookings/BookingTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Table from "../../ui/Table";
import Menus from "../../ui/Menus";
import Empty from "../../ui/Empty";
import Spinner from "../../ui/Spinner";
import Pagination from "../../ui/Pagination";

import BookingRow from "./BookingRow";

Expand Down Expand Up @@ -31,6 +32,10 @@ function BookingTable() {
<BookingRow key={booking.id} booking={booking} />
)}
/>

<Table.Footer>
<Pagination count={45} />
</Table.Footer>
</Table>
</Menus>
);
Expand Down
2 changes: 2 additions & 0 deletions src/features/bookings/useBookings.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export function useBookings() {
const [field, direction] = sortByRaw.split("-");
const sortBy = { field, direction };

// PAGINATION

const {
isLoading,
error,
Expand Down
56 changes: 56 additions & 0 deletions src/ui/Pagination.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import styled from "styled-components";
import { HiChevronLeft, HiChevronRight } from "react-icons/hi2";
import { useSearchParams } from "react-router-dom";

const StyledPagination = styled.div`
width: 100%;
Expand Down Expand Up @@ -55,3 +57,57 @@ const PaginationButton = styled.button`
color: var(--color-brand-50);
}
`;

const PAGE_SIZE = 10;

function Pagination({ count }) {
const [searchParams, setSearchParams] = useSearchParams();
let currentPage = !searchParams.get("page")
? 1
: Number(searchParams.get("page"));

const pageCount = Math.ceil(count / PAGE_SIZE);

function nextPage() {
const next = currentPage === pageCount ? currentPage : currentPage + 1;
searchParams.set("page", next);
setSearchParams(searchParams);
}

function prevPage() {
const prev = currentPage === 1 ? currentPage : currentPage - 1;
searchParams.set("page", prev);
setSearchParams(searchParams);
}

if (pageCount <= 1) return null;

return (
<StyledPagination>
<P>
Showing <span>{(currentPage - 1) * PAGE_SIZE + 1}</span> to{" "}
<span>
{currentPage === pageCount ? count : currentPage * PAGE_SIZE}
</span>{" "}
of <span>{count}</span> results
</P>

<Buttons>
<PaginationButton onClick={prevPage} disabled={currentPage === 1}>
<HiChevronLeft />
<span>Previous</span>
</PaginationButton>

<PaginationButton
onClick={nextPage}
disabled={currentPage === pageCount}
>
<span>Next</span>
<HiChevronRight />
</PaginationButton>
</Buttons>
</StyledPagination>
);
}

export default Pagination;

0 comments on commit 65af52d

Please sign in to comment.