All files / pages/Admin UsersIndexPage.jsx

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

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            2x     41x 41x       41x                       41x   41x 1x 1x     41x                           200x                   2x                         4x                        
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 pageSizeOptions = [10, 25, 50, 100, 500];
 
export default function UsersIndexPage() {
  const [pageSize, setPageSize] = useState(10);
  const [currentPage, setCurrentPage] = useState(0);
 
  // The backend returns one page of users at a time (a Spring Page object:
  // { content: [...users], totalPages, totalElements, number, size, ... }).
  const { data: usersPage } = useBackend(
    // Stryker disable next-line StringLiteral: query key caching detail
    ["/api/admin/users/paged", currentPage, pageSize],
    {
      // Stryker disable next-line StringLiteral: axios treats a falsy method as "GET"
      method: "GET",
      url: "/api/admin/users/paged",
      params: { page: currentPage, size: pageSize },
    },
    { content: [], totalPages: 0 },
  );
 
  const totalPages = usersPage.totalPages;
 
  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
            // Stryker disable next-line ObjectLiteral,StringLiteral: style is a visual detail
            style={{ width: "auto" }}
            value={pageSize}
            onChange={handlePageSizeChange}
            data-testid="UsersIndexPage-pageSize"
          >
            {pageSizeOptions.map((size) => (
              <option key={size} value={size}>
                {size}
              </option>
            ))}
          </Form.Select>
        </Form.Group>
        <UsersTable users={usersPage.content} />
        <div className="d-flex align-items-center gap-2 mt-2">
          <button
            className="btn btn-outline-secondary"
            onClick={() => setCurrentPage((p) => Math.max(p - 1, 0))}
            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={() =>
              // Stryker disable next-line ArithmeticOperator: next button is disabled at last page, so + 1 is unreachable in practice
              setCurrentPage((p) => Math.min(p + 1, totalPages - 1))
            }
            disabled={currentPage >= totalPages - 1}
            data-testid="UsersIndexPage-next"
          >
            Next
          </button>
        </div>
      </div>
    </BasicLayout>
  );
}