2018-06-17 18:13:55 +02:00
|
|
|
import { Link } from 'react-router-dom';
|
|
|
|
import { Pagination, PaginationItem, PaginationLink } from 'reactstrap';
|
2020-09-13 10:03:02 +02:00
|
|
|
import { pageIsEllipsis, keyForPage, progressivePagination, prettifyPageNumber } from '../utils/helpers/pagination';
|
2020-12-22 09:49:13 +01:00
|
|
|
import { ShlinkPaginator } from '../api/types';
|
2018-06-17 18:13:55 +02:00
|
|
|
|
2020-08-30 19:45:17 +02:00
|
|
|
interface PaginatorProps {
|
|
|
|
paginator?: ShlinkPaginator;
|
|
|
|
serverId: string;
|
|
|
|
}
|
2018-06-17 18:13:55 +02:00
|
|
|
|
2020-08-30 19:45:17 +02:00
|
|
|
const Paginator = ({ paginator, serverId }: PaginatorProps) => {
|
|
|
|
const { currentPage = 0, pagesCount = 0 } = paginator ?? {};
|
2018-08-25 23:39:27 +02:00
|
|
|
|
|
|
|
if (pagesCount <= 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-08-25 23:45:03 +02:00
|
|
|
const renderPages = () =>
|
2020-03-28 17:19:33 +01:00
|
|
|
progressivePagination(currentPage, pagesCount).map((pageNumber, index) => (
|
|
|
|
<PaginationItem
|
2020-03-28 17:43:09 +01:00
|
|
|
key={keyForPage(pageNumber, index)}
|
2020-08-28 20:05:01 +02:00
|
|
|
disabled={pageIsEllipsis(pageNumber)}
|
2020-03-28 17:19:33 +01:00
|
|
|
active={currentPage === pageNumber}
|
|
|
|
>
|
2018-08-25 23:45:03 +02:00
|
|
|
<PaginationLink
|
|
|
|
tag={Link}
|
2018-09-16 12:18:02 +02:00
|
|
|
to={`/server/${serverId}/list-short-urls/${pageNumber}`}
|
2018-08-25 23:45:03 +02:00
|
|
|
>
|
2020-09-13 10:03:02 +02:00
|
|
|
{prettifyPageNumber(pageNumber)}
|
2018-08-25 23:45:03 +02:00
|
|
|
</PaginationLink>
|
|
|
|
</PaginationItem>
|
|
|
|
));
|
2018-08-25 23:39:27 +02:00
|
|
|
|
|
|
|
return (
|
2021-09-25 09:34:38 +02:00
|
|
|
<Pagination className="sticky-card-paginator" listClassName="flex-wrap justify-content-center mb-0">
|
2018-08-25 23:39:27 +02:00
|
|
|
<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>
|
|
|
|
);
|
2020-03-28 17:19:33 +01:00
|
|
|
};
|
2018-08-25 23:39:27 +02:00
|
|
|
|
2020-03-28 17:19:33 +01:00
|
|
|
export default Paginator;
|