All files / components/Users RoleEmailTable.jsx

100% Statements 25/25
100% Branches 6/6
100% Functions 8/8
100% Lines 25/25

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                            127x 127x   127x 4x             4x               127x 5x 5x     127x 5x 5x     127x 4x 4x     127x                 109x 27x               82x     5x                   127x                                 3x 3x 3x 3x 3x  
import { useState } from "react";
import { Button } from "react-bootstrap";
import OurTable from "main/components/OurTable";
import RoleEmailDeleteModal from "main/components/Users/RoleEmailDeleteModal";
import { useBackendMutation } from "main/utils/useBackend";
import { toast } from "react-toastify";
import PropTypes from "prop-types";
 
export default function RoleEmailTable({
  data,
  deleteEndpoint,
  getEndpoint,
  testIdPrefix,
}) {
  const [showDeleteModal, setShowDeleteModal] = useState(false);
  const [pendingEmail, setPendingEmail] = useState(null);
 
  const deleteMutation = useBackendMutation(
    (email) => ({
      url: deleteEndpoint,
      method: "DELETE",
      params: { email },
    }),
    {
      onSuccess: (deleted) => {
        toast(deleted?.message ?? "Deleted");
      },
    },
    // Stryker disable next-line ArrayDeclaration: invalidateQueries([]) invalidates every
    // query, which is behaviorally identical here since this is the only active query.
    [getEndpoint],
  );
 
  const openDeleteModal = (email) => {
    setPendingEmail(email);
    setShowDeleteModal(true);
  };
 
  const closeDeleteModal = () => {
    setShowDeleteModal(false);
    setPendingEmail(null);
  };
 
  const confirmDelete = () => {
    deleteMutation.mutate(pendingEmail);
    closeDeleteModal();
  };
 
  const columns = [
    {
      Header: "Email",
      accessor: "email",
    },
    {
      Header: "Delete",
      id: "delete",
      Cell: (cell) => {
        if (cell.row.original.isInAdminEmails) {
          return (
            <span
              data-testid={`${testIdPrefix}-cell-row-${cell.row.index}-cannot-delete`}
            >
              In <code>ADMIN_EMAILS</code>
            </span>
          );
        }
        return (
          <Button
            className="btn btn-danger"
            onClick={() => openDeleteModal(cell.row.original.email)}
            data-testid={`${testIdPrefix}-cell-row-${cell.row.index}-col-delete-button`}
          >
            Delete
          </Button>
        );
      },
    },
  ];
 
  return (
    <>
      <OurTable
        data={Array.isArray(data) ? data : []}
        columns={columns}
        testid={testIdPrefix}
      />
      <RoleEmailDeleteModal
        show={showDeleteModal}
        onHide={closeDeleteModal}
        email={pendingEmail}
        onConfirm={confirmDelete}
      />
    </>
  );
}
 
RoleEmailTable.propTypes = Object.create(null);
RoleEmailTable.propTypes.data = PropTypes.array;
RoleEmailTable.propTypes.deleteEndpoint = PropTypes.string.isRequired;
RoleEmailTable.propTypes.getEndpoint = PropTypes.string.isRequired;
RoleEmailTable.propTypes.testIdPrefix = PropTypes.string.isRequired;