All files / pages/Admin UsersIndexPage.jsx

100% Statements 16/16
100% Branches 0/0
100% Functions 7/7
100% Lines 14/14

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            2x 2x     42x 42x   42x                         42x 42x   42x 1x 1x     42x                         205x                       2x                         5x                      
import { useState } from "react";
import { Form } from "react-bootstrap";
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import UsersTable from "main/components/Users/UsersTable";
import { useBackend } from "main/utils/useBackend";
 
const usersEndpoint = "/api/admin/users/paged";
const pageSizeOptions = [10, 25, 50, 100, 500];
 
export default function UsersIndexPage() {
  const [pageSize, setPageSize] = useState(10);
  const [currentPage, setCurrentPage] = useState(0);
 
  const { data: users } = useBackend(
    [usersEndpoint, currentPage, pageSize],
    {
      url: usersEndpoint,
      params: {
        page: currentPage,
        size: pageSize,
        sort: "id,asc",
      },
    },
    { content: [], totalPages: 0 },
  );
 
  const totalPages = users.totalPages;
  const userList = users.content;
 
  const handlePageSizeChange = (e) => {
    setPageSize(Number(e.target.value));
    setCurrentPage(0);
  };
 
  return (
    <BasicLayout>
      <div className="pt-2">
        <h1>Users</h1>
 
        <Form.Group className="mb-3 d-flex align-items-center gap-2">
          <Form.Label className="mb-0">Page Size:</Form.Label>
          <Form.Select
            value={pageSize}
            onChange={handlePageSizeChange}
            data-testid="UsersIndexPage-pageSize"
          >
            {pageSizeOptions.map((size) => (
              <option key={size} value={size}>
                {size}
              </option>
            ))}
          </Form.Select>
        </Form.Group>
 
        <UsersTable users={userList} />
 
        <div className="d-flex align-items-center gap-2 mt-2">
          <button
            className="btn btn-outline-secondary"
            onClick={() => setCurrentPage((p) => p - 1)}
            disabled={currentPage === 0}
            data-testid="UsersIndexPage-prev"
          >
            Previous
          </button>
 
          <span data-testid="UsersIndexPage-page-info">
            Page {currentPage + 1} of {Math.max(totalPages, 1)}
          </span>
 
          <button
            className="btn btn-outline-secondary"
            onClick={() => setCurrentPage((p) => p + 1)}
            disabled={currentPage >= totalPages - 1}
            data-testid="UsersIndexPage-next"
          >
            Next
          </button>
        </div>
      </div>
    </BasicLayout>
  );
}