shlink-web-client/src/short-urls/Paginator.tsx

58 lines
1.7 KiB
TypeScript
Raw Normal View History

2018-06-17 19:13:55 +03:00
import React from 'react';
import { Link } from 'react-router-dom';
import { Pagination, PaginationItem, PaginationLink } from 'reactstrap';
2020-08-28 21:05:01 +03:00
import { pageIsEllipsis, keyForPage, progressivePagination } from '../utils/helpers/pagination';
import { ShlinkPaginator } from '../utils/services/types';
import './Paginator.scss';
2018-06-17 19:13:55 +03:00
interface PaginatorProps {
paginator?: ShlinkPaginator;
serverId: string;
}
2018-06-17 19:13:55 +03:00
const Paginator = ({ paginator, serverId }: PaginatorProps) => {
const { currentPage = 0, pagesCount = 0 } = paginator ?? {};
if (pagesCount <= 1) {
return null;
}
2018-08-26 00:45:03 +03:00
const renderPages = () =>
progressivePagination(currentPage, pagesCount).map((pageNumber, index) => (
<PaginationItem
key={keyForPage(pageNumber, index)}
2020-08-28 21:05:01 +03:00
disabled={pageIsEllipsis(pageNumber)}
active={currentPage === pageNumber}
>
2018-08-26 00:45:03 +03:00
<PaginationLink
tag={Link}
to={`/server/${serverId}/list-short-urls/${pageNumber}`}
2018-08-26 00:45:03 +03:00
>
{pageNumber}
2018-08-26 00:45:03 +03:00
</PaginationLink>
</PaginationItem>
));
return (
<Pagination className="short-urls-paginator" listClassName="flex-wrap justify-content-center mb-0">
<PaginationItem disabled={currentPage === 1}>
<PaginationLink
previous
tag={Link}
to={`/server/${serverId}/list-short-urls/${currentPage - 1}`}
/>
</PaginationItem>
{renderPages()}
<PaginationItem disabled={currentPage >= pagesCount}>
<PaginationLink
next
tag={Link}
to={`/server/${serverId}/list-short-urls/${currentPage + 1}`}
/>
</PaginationItem>
</Pagination>
);
};
export default Paginator;