2018-06-17 19:13:55 +03:00
|
|
|
import { Link } from 'react-router-dom';
|
|
|
|
import { Pagination, PaginationItem, PaginationLink } from 'reactstrap';
|
2023-07-29 11:43:15 +03:00
|
|
|
import type { ShlinkPaginator } from '../api-contract';
|
2023-02-18 12:40:37 +03:00
|
|
|
import type {
|
2023-07-29 11:43:15 +03:00
|
|
|
NumberOrEllipsis } from '../utils/helpers/pagination';
|
2021-11-09 00:13:37 +03:00
|
|
|
import {
|
|
|
|
keyForPage,
|
2023-02-18 13:11:01 +03:00
|
|
|
pageIsEllipsis,
|
2021-11-09 00:13:37 +03:00
|
|
|
prettifyPageNumber,
|
2023-02-18 13:11:01 +03:00
|
|
|
progressivePagination,
|
2023-07-29 11:43:15 +03:00
|
|
|
} from '../utils/helpers/pagination';
|
2023-07-24 18:30:58 +03:00
|
|
|
import { useRoutesPrefix } from '../utils/routesPrefix';
|
2018-06-17 19:13:55 +03:00
|
|
|
|
2020-08-30 20:45:17 +03:00
|
|
|
interface PaginatorProps {
|
|
|
|
paginator?: ShlinkPaginator;
|
2021-11-09 00:13:37 +03:00
|
|
|
currentQueryString?: string;
|
2020-08-30 20:45:17 +03:00
|
|
|
}
|
2018-06-17 19:13:55 +03:00
|
|
|
|
2023-07-24 18:30:58 +03:00
|
|
|
export const Paginator = ({ paginator, currentQueryString = '' }: PaginatorProps) => {
|
2020-08-30 20:45:17 +03:00
|
|
|
const { currentPage = 0, pagesCount = 0 } = paginator ?? {};
|
2023-07-24 18:30:58 +03:00
|
|
|
const routesPrefix = useRoutesPrefix();
|
2021-11-09 00:13:37 +03:00
|
|
|
const urlForPage = (pageNumber: NumberOrEllipsis) =>
|
2023-07-24 18:30:58 +03:00
|
|
|
`${routesPrefix}/list-short-urls/${pageNumber}${currentQueryString}`;
|
2018-08-26 00:39:27 +03:00
|
|
|
|
|
|
|
if (pagesCount <= 1) {
|
2022-12-18 12:12:34 +03:00
|
|
|
return <div className="pb-3" />; // Return some space
|
2018-08-26 00:39:27 +03:00
|
|
|
}
|
|
|
|
|
2018-08-26 00:45:03 +03:00
|
|
|
const renderPages = () =>
|
2020-03-28 19:19:33 +03:00
|
|
|
progressivePagination(currentPage, pagesCount).map((pageNumber, index) => (
|
|
|
|
<PaginationItem
|
2020-03-28 19:43:09 +03:00
|
|
|
key={keyForPage(pageNumber, index)}
|
2020-08-28 21:05:01 +03:00
|
|
|
disabled={pageIsEllipsis(pageNumber)}
|
2020-03-28 19:19:33 +03:00
|
|
|
active={currentPage === pageNumber}
|
|
|
|
>
|
2021-11-09 00:13:37 +03:00
|
|
|
<PaginationLink tag={Link} to={urlForPage(pageNumber)}>
|
2020-09-13 11:03:02 +03:00
|
|
|
{prettifyPageNumber(pageNumber)}
|
2018-08-26 00:45:03 +03:00
|
|
|
</PaginationLink>
|
|
|
|
</PaginationItem>
|
|
|
|
));
|
2018-08-26 00:39:27 +03:00
|
|
|
|
|
|
|
return (
|
2022-12-18 12:12:34 +03:00
|
|
|
<Pagination className="sticky-card-paginator py-3" listClassName="flex-wrap justify-content-center mb-0">
|
2018-08-26 00:39:27 +03:00
|
|
|
<PaginationItem disabled={currentPage === 1}>
|
2021-11-09 00:13:37 +03:00
|
|
|
<PaginationLink previous tag={Link} to={urlForPage(currentPage - 1)} />
|
2018-08-26 00:39:27 +03:00
|
|
|
</PaginationItem>
|
|
|
|
{renderPages()}
|
|
|
|
<PaginationItem disabled={currentPage >= pagesCount}>
|
2021-11-09 00:13:37 +03:00
|
|
|
<PaginationLink next tag={Link} to={urlForPage(currentPage + 1)} />
|
2018-08-26 00:39:27 +03:00
|
|
|
</PaginationItem>
|
|
|
|
</Pagination>
|
|
|
|
);
|
2020-03-28 19:19:33 +03:00
|
|
|
};
|