All files / pages/Admin ModeratorsIndexPage.jsx

100% Statements 21/21
100% Branches 5/5
100% Functions 7/7
100% Lines 21/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                1x     51x 51x   51x             51x 5x             1x 1x 1x     4x 4x         1x   4x               51x 6x 6x     51x 1x 1x     51x                                           5x                
import { useState } from "react";
import { Button } from "react-bootstrap";
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import RoleEmailTable from "main/components/Users/RoleEmailTable";
import RoleEmailAddModal from "main/components/Users/RoleEmailAddModal";
import { useBackend, useBackendMutation } from "main/utils/useBackend";
import { toast } from "react-toastify";
 
const getEndpoint = "/api/admin/moderators/all";
 
export default function ModeratorsIndexPage() {
  const [showAddModal, setShowAddModal] = useState(false);
  const [addServerError, setAddServerError] = useState(null);
 
  const { data: moderators } = useBackend(
    [getEndpoint],
    // Stryker disable next-line StringLiteral: axios treats a falsy method as "GET"
    { method: "GET", url: getEndpoint },
    [],
  );
 
  const addMutation = useBackendMutation(
    (email) => ({
      url: "/api/admin/moderators/post",
      method: "POST",
      params: { email },
    }),
    {
      onSuccess: (moderator) => {
        toast(`New moderator added - email: ${moderator.email}`);
        setShowAddModal(false);
        setAddServerError(null);
      },
      onError: (error) => {
        let message = "Unable to add moderator.";
        if (
          error.response &&
          error.response.data &&
          error.response.data.message
        ) {
          message = error.response.data.message;
        }
        setAddServerError(message);
      },
    },
    // Stryker disable next-line ArrayDeclaration: invalidateQueries([]) invalidates every
    // query, which is behaviorally identical here since this is the only active query.
    [getEndpoint],
  );
 
  const openAddModal = () => {
    setAddServerError(null);
    setShowAddModal(true);
  };
 
  const closeAddModal = () => {
    setShowAddModal(false);
    setAddServerError(null);
  };
 
  return (
    <BasicLayout>
      <div className="pt-2">
        <Button
          className="btn btn-primary"
          style={{ float: "right" }}
          onClick={openAddModal}
          data-testid="ModeratorsIndexPage-add-button"
        >
          Add Moderator
        </Button>
        <h1>Moderators</h1>
        <RoleEmailTable
          data={moderators}
          deleteEndpoint="/api/admin/moderators/delete"
          getEndpoint={getEndpoint}
          testIdPrefix="ModeratorsIndexPage"
        />
      </div>
      <RoleEmailAddModal
        show={showAddModal}
        onHide={closeAddModal}
        onSubmitEmail={(email) => addMutation.mutate(email)}
        title="Add Moderator"
        buttonLabel="Add"
        serverError={addServerError}
      />
    </BasicLayout>
  );
}