All files / components/TabComponent AssignmentTabComponent.jsx

100% Statements 22/22
100% Branches 0/0
100% Functions 10/10
100% Lines 19/19

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                28x 2x     28x 2x     28x 1x     28x                       28x         28x 2x     28x                     28x         28x 2x     28x                 28x             28x 1x     28x                                                                                                              
import IndividualAssignmentForm from "main/components/Assignments/IndividualAssignmentForm";
import TeamRepositoryAssignmentForm from "main/components/Assignments/TeamRepositoryAssignmentForm";
import DeleteReposForm from "main/components/Assignments/DeleteReposForm";
import { Card, Row, Col, OverlayTrigger, Tooltip } from "react-bootstrap";
import { useBackendMutation } from "main/utils/useBackend";
import { toast } from "react-toastify";
 
export default function AssignmentTabComponent({ courseId }) {
  const onSuccessAssignment = () => {
    toast("Repository creation successfully started.");
  };
 
  const onSuccessTeamAssignment = () => {
    toast("Team repository creation successfully started.");
  };
 
  const onSuccessDeleteRepos = () => {
    toast("Delete repositories job successfully started.");
  };
 
  const objectToAxiosParamsIndividualAssignment = (assignment) => ({
    url: `/api/repos/createRepos`,
    method: "POST",
    params: {
      courseId: courseId,
      repoPrefix: assignment.repoPrefix,
      isPrivate: assignment.assignmentPrivacy,
      permissions: assignment.permissions,
      creationOption: assignment.creationOption,
    },
  });
 
  const indvidiualAssignmentMutation = useBackendMutation(
    objectToAxiosParamsIndividualAssignment,
    { onSuccess: onSuccessAssignment },
  );
 
  const postIndividualAssignment = (assignment) => {
    indvidiualAssignmentMutation.mutate(assignment);
  };
 
  const objectToAxiosParamsTeamAssignment = (teamAssignment) => ({
    url: `/api/repos/createTeamRepos`,
    method: "POST",
    params: {
      courseId: courseId,
      repoPrefix: teamAssignment.repoPrefix,
      isPrivate: teamAssignment.assignmentPrivacy,
      permissions: teamAssignment.permissions,
    },
  });
 
  const teamAssignmentMutation = useBackendMutation(
    objectToAxiosParamsTeamAssignment,
    { onSuccess: onSuccessTeamAssignment },
  );
 
  const postTeamAssignment = (teamAssignment) => {
    teamAssignmentMutation.mutate(teamAssignment);
  };
 
  const objectToAxiosParamsDeleteRepos = (deleteReposRequest) => ({
    url: `/api/repos`,
    method: "DELETE",
    params: {
      courseId: courseId,
      prefix: deleteReposRequest.prefix,
    },
  });
 
  const deleteReposMutation = useBackendMutation(
    objectToAxiosParamsDeleteRepos,
    {
      onSuccess: onSuccessDeleteRepos,
    },
  );
 
  const postDeleteRepos = (deleteReposRequest) => {
    deleteReposMutation.mutate(deleteReposRequest);
  };
 
  return (
    <Row md={3} className="g-2 mb-2" data-testid={"AssignmentTabComponent"}>
      <Col md={6}>
        <Card className="h-100">
          <Card.Header>Individual Repository Assignment</Card.Header>
          <Card.Body>
            <IndividualAssignmentForm submitAction={postIndividualAssignment} />
          </Card.Body>
        </Card>
      </Col>
      <Col md={6}>
        <Card className="h-100">
          <Card.Header>Team Repository Assignment</Card.Header>
          <Card.Body>
            <TeamRepositoryAssignmentForm submitAction={postTeamAssignment} />
          </Card.Body>
        </Card>
      </Col>
      <Col md={6}>
        <Card className="h-100">
          <Card.Header>
            <div className="d-flex align-items-center gap-2">
              <span>Delete Empty Repositories</span>
              <OverlayTrigger
                placement="right"
                overlay={
                  <Tooltip id="delete-empty-repos-tooltip">
                    Delete all repos in the organization that start with this
                    prefix and have no commits.
                  </Tooltip>
                }
              >
                <span
                  className="text-primary"
                  role="button"
                  tabIndex={0}
                  data-testid="DeleteRepos-help"
                >
                  ?
                </span>
              </OverlayTrigger>
            </div>
          </Card.Header>
          <Card.Body>
            <p className="text-muted small">
              Delete all repos in the organization that have names starting with
              the prefix below and have no commits.
            </p>
            <DeleteReposForm submitAction={postDeleteRepos} />
          </Card.Body>
        </Card>
      </Col>
    </Row>
  );
}