2018-06-17 19:13:55 +03:00
|
|
|
import React from 'react';
|
|
|
|
import { Link } from 'react-router-dom';
|
|
|
|
import { Pagination, PaginationItem, PaginationLink } from 'reactstrap';
|
2018-08-26 00:39:27 +03:00
|
|
|
import PropTypes from 'prop-types';
|
2020-03-28 19:43:09 +03:00
|
|
|
import { isPageDisabled, keyForPage, progressivePagination } from '../utils/helpers/pagination';
|
2020-04-10 14:42:16 +03:00
|
|
|
import './Paginator.scss';
|
2018-06-17 19:13:55 +03:00
|
|
|
|
2018-08-26 00:39:27 +03:00
|
|
|
const propTypes = {
|
|
|
|
serverId: PropTypes.string.isRequired,
|
|
|
|
paginator: PropTypes.shape({
|
|
|
|
currentPage: PropTypes.number,
|
|
|
|
pagesCount: PropTypes.number,
|
|
|
|
}),
|
|
|
|
};
|
2018-06-17 19:13:55 +03:00
|
|
|
|
2020-03-28 19:19:33 +03:00
|
|
|
const Paginator = ({ paginator = {}, serverId }) => {
|
2018-08-26 00:39:27 +03:00
|
|
|
const { currentPage, pagesCount = 0 } = paginator;
|
|
|
|
|
|
|
|
if (pagesCount <= 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
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)}
|
|
|
|
disabled={isPageDisabled(pageNumber)}
|
2020-03-28 19:19:33 +03:00
|
|
|
active={currentPage === pageNumber}
|
|
|
|
>
|
2018-08-26 00:45:03 +03:00
|
|
|
<PaginationLink
|
|
|
|
tag={Link}
|
2018-09-16 13:18:02 +03:00
|
|
|
to={`/server/${serverId}/list-short-urls/${pageNumber}`}
|
2018-08-26 00:45:03 +03:00
|
|
|
>
|
2018-09-16 13:18:02 +03:00
|
|
|
{pageNumber}
|
2018-08-26 00:45:03 +03:00
|
|
|
</PaginationLink>
|
|
|
|
</PaginationItem>
|
|
|
|
));
|
2018-08-26 00:39:27 +03:00
|
|
|
|
|
|
|
return (
|
2020-04-10 14:42:16 +03:00
|
|
|
<Pagination className="short-urls-paginator" listClassName="flex-wrap justify-content-center mb-0">
|
2018-08-26 00:39:27 +03: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 19:19:33 +03:00
|
|
|
};
|
2018-08-26 00:39:27 +03:00
|
|
|
|
|
|
|
Paginator.propTypes = propTypes;
|
2020-03-28 19:19:33 +03:00
|
|
|
|
|
|
|
export default Paginator;
|