2022-11-25 20:25:03 +00:00
|
|
|
import * as React from 'react';
|
2020-07-15 03:48:41 +00:00
|
|
|
import { PaginatedResult } from '@/api/http';
|
|
|
|
import tw from 'twin.macro';
|
2022-11-25 20:25:03 +00:00
|
|
|
import styled from 'styled-components';
|
2020-07-15 03:48:41 +00:00
|
|
|
import Button from '@/components/elements/Button';
|
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import { faAngleDoubleLeft, faAngleDoubleRight } from '@fortawesome/free-solid-svg-icons';
|
|
|
|
|
|
|
|
interface RenderFuncProps<T> {
|
|
|
|
items: T[];
|
|
|
|
isLastPage: boolean;
|
|
|
|
isFirstPage: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Props<T> {
|
|
|
|
data: PaginatedResult<T>;
|
|
|
|
showGoToLast?: boolean;
|
|
|
|
showGoToFirst?: boolean;
|
|
|
|
onPageSelect: (page: number) => void;
|
|
|
|
children: (props: RenderFuncProps<T>) => React.ReactNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Block = styled(Button)`
|
|
|
|
${tw`p-0 w-10 h-10`}
|
2022-06-26 19:13:52 +00:00
|
|
|
|
2020-07-15 03:48:41 +00:00
|
|
|
&:not(:last-of-type) {
|
|
|
|
${tw`mr-2`};
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
2022-06-26 19:13:52 +00:00
|
|
|
function Pagination<T>({ data: { items, pagination }, onPageSelect, children }: Props<T>) {
|
2020-07-15 03:48:41 +00:00
|
|
|
const isFirstPage = pagination.currentPage === 1;
|
|
|
|
const isLastPage = pagination.currentPage >= pagination.totalPages;
|
|
|
|
|
|
|
|
const pages = [];
|
|
|
|
|
|
|
|
// Start two spaces before the current page. If that puts us before the starting page default
|
|
|
|
// to the first page as the starting point.
|
|
|
|
const start = Math.max(pagination.currentPage - 2, 1);
|
|
|
|
const end = Math.min(pagination.totalPages, pagination.currentPage + 5);
|
|
|
|
|
|
|
|
for (let i = start; i <= end; i++) {
|
|
|
|
pages.push(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{children({ items, isFirstPage, isLastPage })}
|
2022-06-26 19:13:52 +00:00
|
|
|
{pages.length > 1 && (
|
|
|
|
<div css={tw`mt-4 flex justify-center`}>
|
2022-11-25 20:25:03 +00:00
|
|
|
{(pages?.[0] ?? 0) > 1 && !isFirstPage && (
|
2022-06-26 19:13:52 +00:00
|
|
|
<Block isSecondary color={'primary'} onClick={() => onPageSelect(1)}>
|
|
|
|
<FontAwesomeIcon icon={faAngleDoubleLeft} />
|
|
|
|
</Block>
|
|
|
|
)}
|
2022-11-25 20:25:03 +00:00
|
|
|
{pages.map(i => (
|
2020-07-15 03:48:41 +00:00
|
|
|
<Block
|
|
|
|
isSecondary={pagination.currentPage !== i}
|
|
|
|
color={'primary'}
|
|
|
|
key={`block_page_${i}`}
|
|
|
|
onClick={() => onPageSelect(i)}
|
|
|
|
>
|
|
|
|
{i}
|
|
|
|
</Block>
|
2022-06-26 19:13:52 +00:00
|
|
|
))}
|
2022-11-25 20:25:03 +00:00
|
|
|
{(pages?.[4] ?? 0) < pagination.totalPages && !isLastPage && (
|
2022-06-26 19:13:52 +00:00
|
|
|
<Block isSecondary color={'primary'} onClick={() => onPageSelect(pagination.totalPages)}>
|
|
|
|
<FontAwesomeIcon icon={faAngleDoubleRight} />
|
|
|
|
</Block>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)}
|
2020-07-15 03:48:41 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Pagination;
|