All files / components/Projects ProjectCollaboratorTable.jsx

94.73% Statements 18/19
100% Branches 6/6
85.71% Functions 6/7
94.44% Lines 17/18

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                          15x 15x   15x                 15x 1x 1x 1x     15x               15x 2x 2x     15x                                     15x 11x         15x                                             1x           1x                    
import OurTable, { ButtonColumn } from "main/components/Common/OurTable";
import { useBackendMutation } from "main/utils/useBackend";
import { toast } from "react-toastify";
import Modal from "react-bootstrap/Modal";
import { Button } from "react-bootstrap";
import { useState } from "react";
 
export default function ProjectCollaboratorTable({
  collaborators,
  projectId,
  isOwner = true,
  testIdPrefix = "ProjectCollaboratorTable",
}) {
  const [showDeleteModal, setShowDeleteModal] = useState(false);
  const [collaboratorToDelete, setCollaboratorToDelete] = useState(null);
 
  const cellToAxiosParamsDelete = (collaborator) => ({
    url: "/api/projectcollaborators/delete",
    method: "DELETE",
    params: {
      id: collaborator.id,
      projectId: projectId,
    },
  });
 
  const onDeleteSuccess = () => {
    toast("Collaborator deleted successfully.");
    setShowDeleteModal(false);
    setCollaboratorToDelete(null);
  };
 
  const deleteMutation = useBackendMutation(
    cellToAxiosParamsDelete,
    {
      onSuccess: onDeleteSuccess,
    },
    [`/api/projectcollaborators/project?projectId=${projectId}`],
  );
 
  const deleteCallback = (cell) => {
    setCollaboratorToDelete(cell.row.original);
    setShowDeleteModal(true);
  };
 
  const columns = [
    {
      header: "id",
      accessorKey: "id",
    },
    {
      header: "First Name",
      accessorKey: "firstName",
    },
    {
      header: "Last Name",
      accessorKey: "lastName",
    },
    {
      header: "Email",
      accessorKey: "email",
    },
  ];
 
  if (isOwner) {
    columns.push(
      ButtonColumn("Delete", "danger", deleteCallback, testIdPrefix),
    );
  }
 
  return (
    <>
      <Modal
        data-testid={`${testIdPrefix}-delete-modal`}
        show={showDeleteModal}
        onHide={() => setShowDeleteModal(false)}
        centered
      >
        <Modal.Header closeButton>
          <Modal.Title>Confirm Delete</Modal.Title>
        </Modal.Header>
        <Modal.Body>
          {collaboratorToDelete && (
            <p>
              Please confirm that you really want to remove{" "}
              <strong>
                {collaboratorToDelete.firstName} {collaboratorToDelete.lastName}
              </strong>{" "}
              as a collaborator. This action cannot be undone.
            </p>
          )}
        </Modal.Body>
        <Modal.Footer>
          <Button variant="secondary" onClick={() => setShowDeleteModal(false)}>
            Do not delete
          </Button>
          <Button
            variant="danger"
            data-testid={`${testIdPrefix}-delete-modal-confirm-button`}
            onClick={() => deleteMutation.mutate(collaboratorToDelete)}
          >
            Yes, Delete
          </Button>
        </Modal.Footer>
      </Modal>
      <OurTable data={collaborators} columns={columns} testid={testIdPrefix} />
    </>
  );
}