All files / brand-store/components/AppPagination AppPagination.tsx

80.95% Statements 17/21
75% Branches 3/4
42.85% Functions 3/7
80.95% Lines 17/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133                                            31x                                     31x 31x 31x 31x       31x 15x   15x 15x 15x 15x     31x 15x 15x         15x 15x     31x                                                                                                                                
import { useState, useEffect, ReactNode } from "react";
import { Button, Icon, Input, Select } from "@canonical/react-components";
 
import type {
  Model as ModelType,
  Policy,
  SigningKey,
} from "../../types/shared";
 
export type ItemType = SigningKey | Policy | ModelType;
 
type Props<T extends ItemType> = {
  keyword: string;
  items: T[];
  setItemsToShow: (items: T[]) => void;
};
 
function AppPagination<T extends ItemType>({
  keyword,
  items,
  setItemsToShow,
}: Props<T>): ReactNode {
  const paginationOptions = [
    {
      label: "25/page",
      value: 25,
    },
    {
      label: "50/page",
      value: 50,
    },
    {
      label: "100/page",
      value: 100,
    },
    {
      label: "200/page",
      value: 200,
    },
  ];
 
  const [pageSize, setPageSize] = useState<number>(paginationOptions[0].value);
  const [currentPage, setCurrentPage] = useState<number>(1);
  const [visibleItemsCount, setVisibleItemsCount] = useState<number>(0);
  const [totalPages, setTotalPages] = useState<number>(
    Math.ceil(items.length / pageSize),
  );
 
  useEffect(() => {
    const itemsToShow = items.slice(0, pageSize);
 
    setItemsToShow(itemsToShow);
    setCurrentPage(1);
    setVisibleItemsCount(itemsToShow.length);
    setTotalPages(Math.ceil(items.length / pageSize));
  }, [items, pageSize]);
 
  useEffect(() => {
    const multiplier = currentPage - 1;
    const itemsToShow = items.slice(
      pageSize * multiplier,
      pageSize * multiplier + pageSize,
    );
 
    setItemsToShow(itemsToShow);
    setVisibleItemsCount(itemsToShow.length);
  }, [currentPage]);
 
  return (
    <div className="app-pagination">
      <div className="app-pagination__description">
        Showing {pageSize * currentPage - pageSize + 1} to{" "}
        {pageSize * currentPage - pageSize + visibleItemsCount} out of{" "}
        {items.length} {keyword}
        {items.length > 1 ? "s" : ""}
      </div>
      <div className="app-pagination__controls">
        <Button
          aria-label="Previous page"
          appearance="base"
          hasIcon
          className="app-pagination__back"
          disabled={currentPage === 1}
          onClick={() => {
            setCurrentPage(currentPage - 1);
          }}
        >
          <Icon name="chevron-down" />
        </Button>
        <Input
          type="number"
          id="paginationPageInput"
          className="app-pagination__page-number u-no-margin--bottom"
          value={currentPage}
          label="Page number"
          labelClassName="u-off-screen u-off-screen--top"
          onChange={(e) => {
            setCurrentPage(
              Math.min(totalPages, Math.max(1, parseInt(e.target.value))),
            );
          }}
        />{" "}
        of {totalPages}
        <Button
          appearance="base"
          className="app-pagination__next"
          disabled={totalPages === 1 || currentPage === totalPages}
          aria-label="Next page"
          hasIcon
          onClick={() => {
            setCurrentPage(currentPage + 1);
          }}
        >
          <Icon name="chevron-down" />
        </Button>
        <Select
          label="Items per page"
          labelClassName="u-off-screen u-off-screen--top"
          id="itemsPerPage"
          options={paginationOptions}
          onChange={(e) => {
            setPageSize(parseInt(e.target.value));
          }}
          value={pageSize}
          className="app-pagination__page-size u-no-margin--bottom"
        />
      </div>
    </div>
  );
}
 
export default AppPagination;