Skip to content

Commit

Permalink
create calculatePages
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikzogg committed Mar 10, 2024
1 parent 286fd42 commit 035433d
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/component/partial/pagination.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import type { FC } from 'react';

export type PaginationProps = {
submitPage: (page: number) => void;
currentPage: number;
totalPages: number;
maxPages: number;
};

export const Pagination: FC<PaginationProps> = ({ submitPage, currentPage, totalPages, maxPages }: PaginationProps) => {
if (totalPages <= 1 || maxPages <= 1) {
return <div></div>;
const calculatePages = (currentPage: number, totalPages: number, maxPages: number) => {
if (totalPages <= 1 || maxPages <= 1 || currentPage > totalPages) {
return [];
}

const pages = [currentPage];

for (let i = 1; ; i++) {
if (currentPage - i >= 1) {
pages.push(currentPage - i);
Expand All @@ -33,6 +27,23 @@ export const Pagination: FC<PaginationProps> = ({ submitPage, currentPage, total

pages.sort((a, b) => a - b);

return pages;
};

export type PaginationProps = {
submitPage: (page: number) => void;
currentPage: number;
totalPages: number;
maxPages: number;
};

export const Pagination: FC<PaginationProps> = ({ submitPage, currentPage, totalPages, maxPages }: PaginationProps) => {
const pages = calculatePages(currentPage, totalPages, maxPages);

if (pages.length === 0) {
return <div></div>;
}

return (
<ul className="w-fit border-y border-l border-gray-300">
{currentPage > 2 ? (
Expand Down

0 comments on commit 035433d

Please sign in to comment.