All files / components/TabComponent NewAssignmentsTabComponent.jsx

100% Statements 17/17
100% Branches 5/5
100% Functions 7/7
100% Lines 16/16

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                                                71x   71x   71x                       71x                   71x 6x 6x     71x           71x 7x     71x 71x       71x       1x                                     4x                 5x                                      
import React, { useState } from "react";
import { Button, Col, Row } from "react-bootstrap";
import Modal from "react-bootstrap/Modal";
import { toast } from "react-toastify";
 
import { useBackend, useBackendMutation } from "main/utils/useBackend";
import NewAssignmentsTable from "main/components/NewAssignments/NewAssignmentsTable";
import NewIndividualAssignmentForm from "main/components/NewAssignments/NewIndividualAssignmentForm";
import NewTeamAssignmentForm from "main/components/NewAssignments/NewTeamAssignmentForm";
import {
  JOBS_QUERY_KEY,
  NEW_ASSIGNMENTS_URL,
  assignmentsQueryKey,
  formDataToParams,
  jobStartedMessage,
  onAssignmentMutationError,
} from "main/utils/newAssignmentsUtils";
 
export default function NewAssignmentsTabComponent({
  courseId,
  courseViewPath = "instructor",
  testIdPrefix,
}) {
  // which kind of assignment the create modal is for, or null when it is closed
  const [createType, setCreateType] = useState(null);
 
  const queryKey = assignmentsQueryKey(courseId);
 
  const { data: assignments } = useBackend(
    [queryKey],
    {
      // Stryker disable next-line StringLiteral : GET and empty string are equivalent
      method: "GET",
      url: NEW_ASSIGNMENTS_URL,
      params: { courseId },
    },
    [],
    true,
  );
 
  const objectToAxiosParamsPost = (formData) => ({
    url: `${NEW_ASSIGNMENTS_URL}/post`,
    method: "POST",
    params: {
      courseId: courseId,
      asnType: createType,
      ...formDataToParams(createType, formData),
    },
  });
 
  const onCreateSuccess = (data) => {
    toast(jobStartedMessage("created", data));
    setCreateType(null);
  };
 
  const createMutation = useBackendMutation(
    objectToAxiosParamsPost,
    { onSuccess: onCreateSuccess, onError: onAssignmentMutationError },
    [queryKey, JOBS_QUERY_KEY],
  );
 
  const handleCreateSubmit = (formData) => {
    createMutation.mutate(formData);
  };
 
  const creatingTeam = createType === "TEAM";
  const CreateForm = creatingTeam
    ? NewTeamAssignmentForm
    : NewIndividualAssignmentForm;
 
  return (
    <div data-testid={`${testIdPrefix}-new-assignments-tab-component`}>
      <Modal
        show={createType !== null}
        onHide={() => setCreateType(null)}
        centered={true}
        data-testid={`${testIdPrefix}-create-assignment-modal`}
      >
        <Modal.Header closeButton>
          <Modal.Title>
            Create {creatingTeam ? "Team" : "Individual"} Assignment
          </Modal.Title>
        </Modal.Header>
        <Modal.Body>
          <p data-testid={`${testIdPrefix}-create-assignment-modal-note`}>
            Saving starts a job that creates the repositories.
          </p>
          <CreateForm submitAction={handleCreateSubmit} />
        </Modal.Body>
      </Modal>
      <Row sm={4} className="p-2 g-3">
        <Col>
          <Button
            onClick={() => setCreateType("INDIVIDUAL")}
            data-testid={`${testIdPrefix}-create-individual-assignment-button`}
            className="w-100"
          >
            Create Individual Assignment
          </Button>
        </Col>
        <Col>
          <Button
            onClick={() => setCreateType("TEAM")}
            data-testid={`${testIdPrefix}-create-team-assignment-button`}
            className="w-100"
          >
            Create Team Assignment
          </Button>
        </Col>
      </Row>
      <Row>
        <NewAssignmentsTable
          assignments={assignments}
          courseId={courseId}
          courseViewPath={courseViewPath}
          testIdPrefix={`${testIdPrefix}-new-assignments-table`}
        />
      </Row>
    </div>
  );
}