All files / components/CourseStaff CourseStaffTable.jsx

100% Statements 34/34
100% Branches 7/7
100% Functions 11/11
100% Lines 33/33

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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163                                    63x 63x 63x 63x     1x                     63x                       63x 1x     63x 1x     63x 1x 1x     63x 1x 1x     63x             63x 1x 1x     63x 1x           63x           63x 2x 2x     63x 2x     63x                                       63x         60x 60x         63x                                                                
import React from "react";
import OurTable, { ButtonColumn } from "main/components/Common/OurTable";
import { Tooltip, OverlayTrigger } from "react-bootstrap";
 
import { useBackendMutation } from "main/utils/useBackend";
import { hasRole } from "main/utils/currentUser";
import Modal from "react-bootstrap/Modal";
import CourseStaffForm from "main/components/CourseStaff/CourseStaffForm";
import { toast } from "react-toastify";
import CourseStaffDeleteModal from "main/components/CourseStaff/CourseStaffDeleteModal";
 
export default function CourseStaffTable({
  staff,
  currentUser,
  courseId,
  testIdPrefix = "CourseStaffTable",
  isInstructor = true,
}) {
  const [showEditModal, setShowEditModal] = React.useState(false);
  const [editStaff, setEditStaff] = React.useState(null);
  const [showDeleteModal, setShowDeleteModal] = React.useState(false);
  const [deleteStaff, setDeleteStaff] = React.useState(null);
 
  function cellToAxiosParamsDelete(formData) {
    return {
      // Stryker disable next-line StringLiteral
      url: "/api/coursestaff/delete",
      method: "DELETE",
      params: {
        id: formData.id,
        courseId: courseId,
      },
    };
  }
 
  const cellToAxiosParamsEdit = (formData) => ({
    url: `/api/coursestaff`,
    method: "PUT",
    // Stryker disable next-line ObjectLiteral
    params: {
      firstName: formData.firstName,
      lastName: formData.lastName,
      id: formData.id,
      courseId: courseId,
    },
  });
 
  const hideModal = () => {
    setShowEditModal(false);
  };
 
  const hideDeleteModal = () => {
    setShowDeleteModal(false);
  };
 
  const onEditSuccess = () => {
    toast("Staff member updated successfully.");
    hideModal();
  };
 
  const onDeleteSuccess = () => {
    toast("Staff member deleted successfully.");
    hideDeleteModal();
  };
 
  const deleteMutation = useBackendMutation(
    cellToAxiosParamsDelete,
    { onSuccess: onDeleteSuccess },
    [`/api/coursestaff/course?courseId=${courseId}`],
  );
 
  // Stryker disable next-line all
  const deleteCallback = async (cell) => {
    setShowDeleteModal(true);
    setDeleteStaff(cell.row.original.id);
  };
 
  const submitDeleteForm = (data) => {
    deleteMutation.mutate({
      id: deleteStaff,
      ...data,
    });
  };
 
  const editMutation = useBackendMutation(
    cellToAxiosParamsEdit,
    { onSuccess: onEditSuccess },
    [`/api/coursestaff/course?courseId=${courseId}`],
  );
 
  const editCallback = (cell) => {
    setEditStaff(cell.row.original);
    setShowEditModal(true);
  };
 
  const submitEditForm = (data) => {
    editMutation.mutate(data);
  };
 
  const columns = [
    {
      header: "id",
      accessorKey: "id",
      id: "id",
    },
    {
      header: "First Name",
      accessorKey: "firstName",
    },
    {
      header: "Last Name",
      accessorKey: "lastName",
    },
    {
      header: "Email",
      accessorKey: "email",
    },
  ];
 
  if (
    isInstructor &&
    (hasRole(currentUser, "ROLE_INSTRUCTOR") ||
      hasRole(currentUser, "ROLE_ADMIN"))
  ) {
    columns.push(ButtonColumn("Edit", "primary", editCallback, testIdPrefix));
    columns.push(
      ButtonColumn("Delete", "danger", deleteCallback, testIdPrefix),
    );
  }
 
  return (
    <>
      <Modal show={showEditModal} onHide={hideModal}>
        <Modal.Header closeButton>
          <Modal.Title>Edit Staff Member</Modal.Title>
        </Modal.Header>
        <Modal.Body
          className={"pb-3"}
          data-testid={`${testIdPrefix}-modal-body`}
        >
          <CourseStaffForm
            initialContents={editStaff}
            submitAction={submitEditForm}
            buttonLabel={"Update"}
            cancelDisabled={true}
          />
        </Modal.Body>
      </Modal>
      <CourseStaffDeleteModal
        showModal={showDeleteModal}
        toggleShowModal={setShowDeleteModal}
        onSubmitAction={submitDeleteForm}
      />
      <OurTable data={staff} columns={columns} testid={testIdPrefix} />
      <div
        style={{ display: "none" }}
        data-testid={`${testIdPrefix}-courseId`}
        data-course-id={`${courseId}`}
      />
    </>
  );
}