2021-05-21 00:50:55 -07:00
|
|
|
import React from "react";
|
2021-06-21 10:21:25 -07:00
|
|
|
import { Box, Button, Flex } from "@chakra-ui/react";
|
2021-05-21 00:50:55 -07:00
|
|
|
import { Link, useLocation } from "react-router-dom";
|
|
|
|
|
2021-06-21 10:21:25 -07:00
|
|
|
const PER_PAGE = 30;
|
|
|
|
|
2021-05-21 00:50:55 -07:00
|
|
|
function PaginationToolbar({ isLoading, totalCount, ...props }) {
|
|
|
|
const { search } = useLocation();
|
|
|
|
|
|
|
|
const currentOffset =
|
|
|
|
parseInt(new URLSearchParams(search).get("offset")) || 0;
|
|
|
|
|
2021-06-21 10:21:25 -07:00
|
|
|
const currentPageIndex = Math.floor(currentOffset / PER_PAGE);
|
|
|
|
const currentPageNumber = currentPageIndex + 1;
|
|
|
|
const numTotalPages = totalCount ? Math.ceil(totalCount / PER_PAGE) : null;
|
|
|
|
|
2021-05-21 00:50:55 -07:00
|
|
|
const prevPageSearchParams = new URLSearchParams(search);
|
2021-06-21 10:21:25 -07:00
|
|
|
const prevPageOffset = currentOffset - PER_PAGE;
|
2021-05-21 00:50:55 -07:00
|
|
|
prevPageSearchParams.set("offset", prevPageOffset);
|
|
|
|
const prevPageUrl = "?" + prevPageSearchParams.toString();
|
|
|
|
const prevPageIsDisabled = isLoading || prevPageOffset < 0;
|
|
|
|
|
|
|
|
const nextPageSearchParams = new URLSearchParams(search);
|
2021-06-21 10:21:25 -07:00
|
|
|
const nextPageOffset = currentOffset + PER_PAGE;
|
2021-05-21 00:50:55 -07:00
|
|
|
nextPageSearchParams.set("offset", nextPageOffset);
|
|
|
|
const nextPageUrl = "?" + nextPageSearchParams.toString();
|
|
|
|
const nextPageIsDisabled = isLoading || nextPageOffset >= totalCount;
|
|
|
|
|
|
|
|
return (
|
2021-06-21 10:21:25 -07:00
|
|
|
<Flex align="center" justify="space-between" {...props}>
|
2021-05-21 00:50:55 -07:00
|
|
|
<Button
|
|
|
|
as={prevPageIsDisabled ? "button" : Link}
|
|
|
|
to={prevPageIsDisabled ? undefined : prevPageUrl}
|
|
|
|
_disabled={{ cursor: isLoading ? "wait" : "not-allowed", opacity: 0.4 }}
|
|
|
|
isDisabled={prevPageIsDisabled}
|
|
|
|
>
|
|
|
|
← Prev
|
|
|
|
</Button>
|
2021-06-21 10:21:25 -07:00
|
|
|
{numTotalPages && (
|
|
|
|
<Box>
|
|
|
|
Page {currentPageNumber} of {numTotalPages}
|
|
|
|
</Box>
|
|
|
|
)}
|
2021-05-21 00:50:55 -07:00
|
|
|
<Button
|
|
|
|
as={nextPageIsDisabled ? "button" : Link}
|
|
|
|
to={nextPageIsDisabled ? undefined : nextPageUrl}
|
|
|
|
_disabled={{ cursor: isLoading ? "wait" : "not-allowed", opacity: 0.4 }}
|
|
|
|
isDisabled={nextPageIsDisabled}
|
|
|
|
>
|
|
|
|
Next →
|
|
|
|
</Button>
|
|
|
|
</Flex>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default PaginationToolbar;
|