All files / components/NewAssignments NewAssignmentsTable.jsx

100% Statements 55/55
100% Branches 16/16
100% Functions 23/23
100% Lines 52/52

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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255                                                                  128x 128x 128x 128x 128x 128x     128x                   128x           128x           128x 4x     128x 6x 6x     128x 3x     128x           128x           128x           128x 5x     128x 3x 3x     128x 9x 9x     128x 5x 5x     128x 7x     128x 4x     128x               114x         114x         114x                 318x                           114x         114x                 318x 318x 106x   212x       3x 3x                           128x 128x       128x       1x                                                                         1x                  
import React, { useState } from "react";
import Modal from "react-bootstrap/Modal";
import { toast } from "react-toastify";
 
import OurTable, { ButtonColumn } from "main/components/OurTable";
import ConfirmationModal from "main/components/Common/ConfirmationModal";
import NewIndividualAssignmentForm from "main/components/NewAssignments/NewIndividualAssignmentForm";
import NewTeamAssignmentForm from "main/components/NewAssignments/NewTeamAssignmentForm";
import JobLogModal from "main/components/Jobs/JobLogModal";
import { useBackendMutation } from "main/utils/useBackend";
import { jobLogPagePath } from "main/utils/jobLogUtils";
import {
  ASSIGNMENT_TYPE_LABELS,
  CREATE_REPOS_FOR_LABELS,
  JOBS_QUERY_KEY,
  NEW_ASSIGNMENTS_URL,
  PERMISSION_LABELS,
  SIGNED_COMMITS_REQUIRED_ICON,
  VISIBILITY_LABELS,
  assignmentToFormData,
  assignmentsQueryKey,
  formDataToParams,
  jobLaunchedMessage,
  jobStartedMessage,
  onAssignmentMutationError,
} from "main/utils/newAssignmentsUtils";
 
export default function NewAssignmentsTable({
  assignments,
  courseId,
  courseViewPath = "instructor",
  testIdPrefix = "NewAssignmentsTable",
}) {
  const [showEditModal, setShowEditModal] = useState(false);
  const [editAssignment, setEditAssignment] = useState(null);
  const [showDeleteModal, setShowDeleteModal] = useState(false);
  const [deleteAssignment, setDeleteAssignment] = useState(null);
  const [showLogModal, setShowLogModal] = useState(false);
  const [logJobId, setLogJobId] = useState(null);
 
  // The type of an assignment cannot be changed, so it is not sent when editing
  const cellToAxiosParamsEdit = (formData) => ({
    url: `${NEW_ASSIGNMENTS_URL}/put`,
    method: "PUT",
    params: {
      courseId: courseId,
      assignmentId: editAssignment.id,
      ...formDataToParams(editAssignment.asnType, formData),
    },
  });
 
  const cellToAxiosParamsDelete = (assignment) => ({
    url: `${NEW_ASSIGNMENTS_URL}/${assignment.id}`,
    method: "DELETE",
    params: { courseId: courseId },
  });
 
  const cellToAxiosParamsLaunch = (assignment) => ({
    url: `${NEW_ASSIGNMENTS_URL}/launch`,
    method: "POST",
    params: { courseId: courseId, assignmentId: assignment.id },
  });
 
  const onLaunchSuccess = (data) => {
    toast(jobLaunchedMessage(data));
  };
 
  const onEditSuccess = (data) => {
    toast(jobStartedMessage("updated", data));
    setShowEditModal(false);
  };
 
  const onDeleteSuccess = () => {
    toast("Assignment deleted successfully.");
  };
 
  const editMutation = useBackendMutation(
    cellToAxiosParamsEdit,
    { onSuccess: onEditSuccess, onError: onAssignmentMutationError },
    [assignmentsQueryKey(courseId), JOBS_QUERY_KEY],
  );
 
  const launchMutation = useBackendMutation(
    cellToAxiosParamsLaunch,
    { onSuccess: onLaunchSuccess, onError: onAssignmentMutationError },
    [assignmentsQueryKey(courseId), JOBS_QUERY_KEY],
  );
 
  const deleteMutation = useBackendMutation(
    cellToAxiosParamsDelete,
    { onSuccess: onDeleteSuccess, onError: onAssignmentMutationError },
    [assignmentsQueryKey(courseId)],
  );
 
  const launchCallback = (cell) => {
    launchMutation.mutate(cell.row.original);
  };
 
  const showLog = (jobId) => {
    setLogJobId(jobId);
    setShowLogModal(true);
  };
 
  const editCallback = (cell) => {
    setEditAssignment(cell.row.original);
    setShowEditModal(true);
  };
 
  const deleteCallback = (cell) => {
    setDeleteAssignment(cell.row.original);
    setShowDeleteModal(true);
  };
 
  const submitEditForm = (data) => {
    editMutation.mutate(data);
  };
 
  const confirmDelete = () => {
    deleteMutation.mutate(deleteAssignment);
  };
 
  const columns = [
    {
      header: "Repository Prefix",
      accessorKey: "repoPrefix",
      id: "repoPrefix",
    },
    {
      header: "Type",
      accessorFn: (row) => ASSIGNMENT_TYPE_LABELS[row.asnType],
      id: "asnType",
    },
    {
      header: "Visibility",
      accessorFn: (row) => VISIBILITY_LABELS[row.visibility],
      id: "visibility",
    },
    {
      header: "Permission",
      accessorFn: (row) => PERMISSION_LABELS[row.permission],
      id: "permission",
    },
    {
      header: "Signed Commits",
      accessorKey: "requireSignedCommit",
      id: "requireSignedCommit",
      // a checkmark for an assignment whose repositories require signed commits
      cell: ({ cell }) =>
        cell.getValue() ? (
          <span
            role="img"
            aria-label="Signed commits are required"
            title="Signed commits are required"
          >
            {SIGNED_COMMITS_REQUIRED_ICON}
          </span>
        ) : (
          ""
        ),
    },
    {
      header: "Repositories For",
      accessorFn: (row) => CREATE_REPOS_FOR_LABELS[row.createReposFor] ?? "",
      id: "createReposFor",
    },
    {
      header: "Team Regex",
      accessorFn: (row) => row.teamRegex ?? "",
      id: "teamRegex",
    },
    {
      header: "Job Log",
      accessorKey: "lastJobId",
      id: "lastJobId",
      // the number of the job is a link that shows the log of the job in a modal
      cell: ({ cell }) => {
        const jobId = cell.getValue();
        if (jobId === null) {
          return "";
        }
        return (
          <a
            href={jobLogPagePath(courseId, jobId, courseViewPath)}
            onClick={(event) => {
              event.preventDefault();
              showLog(jobId);
            }}
            data-testid={`${testIdPrefix}-cell-row-${cell.row.index}-col-lastJobId-link`}
          >
            {jobId}
          </a>
        );
      },
    },
    ButtonColumn("Refresh", "success", launchCallback, testIdPrefix),
    ButtonColumn("Edit", "primary", editCallback, testIdPrefix),
    ButtonColumn("Delete", "danger", deleteCallback, testIdPrefix),
  ];
 
  const editingTeam = editAssignment?.asnType === "TEAM";
  const EditForm = editingTeam
    ? NewTeamAssignmentForm
    : NewIndividualAssignmentForm;
 
  return (
    <>
      <Modal
        show={showEditModal}
        onHide={() => setShowEditModal(false)}
        centered={true}
        data-testid={`${testIdPrefix}-edit-modal`}
      >
        <Modal.Header closeButton>
          <Modal.Title>
            Edit {editingTeam ? "Team" : "Individual"} Assignment
          </Modal.Title>
        </Modal.Header>
        <Modal.Body data-testid={`${testIdPrefix}-edit-modal-body`}>
          <p data-testid={`${testIdPrefix}-edit-modal-note`}>
            The type of an assignment cannot be changed; to change it, delete
            the assignment and create a new one. Saving starts a job that
            creates the repositories.
          </p>
          <EditForm
            initialContents={
              editAssignment && assignmentToFormData(editAssignment)
            }
            submitAction={submitEditForm}
            buttonLabel="Update"
          />
        </Modal.Body>
      </Modal>
      <ConfirmationModal
        showModal={showDeleteModal}
        setShowModal={setShowDeleteModal}
        onYes={confirmDelete}
      >
        <span data-testid={`${testIdPrefix}-delete-confirmation-message`}>
          Are you sure you want to delete the assignment{" "}
          {deleteAssignment?.repoPrefix}? Repositories that have already been
          created are not deleted.
        </span>
      </ConfirmationModal>
      <JobLogModal
        show={showLogModal}
        onHide={() => setShowLogModal(false)}
        courseId={courseId}
        jobId={logJobId}
        testIdPrefix={`${testIdPrefix}-job-log-modal`}
      />
      <OurTable data={assignments} columns={columns} testid={testIdPrefix} />
    </>
  );
}