All files / components/TabComponent TeamsTabComponent.jsx

100% Statements 214/214
100% Branches 31/31
100% Functions 22/22
100% Lines 214/214

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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 2791x 1x 1x 1x               1x 1x 1x 1x   1x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x   76x 76x 76x 76x 76x 76x   76x   76x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x   76x 3x 3x 3x 3x 3x 3x 3x   76x 1x 1x 1x 1x 1x 1x   76x 1x 1x 1x 1x   76x 1x 1x   76x 76x 76x 76x 76x 2x 2x 1x 1x 1x 1x 1x 1x 1x 1x 2x 76x 76x     76x 76x 76x 76x 1x 1x 1x 1x 1x 1x 76x 4x 4x 4x 1x 1x 1x 4x 2x 2x 2x 3x 1x 1x 1x 1x 4x 76x 76x     76x 76x 76x     76x 5x 5x   76x 3x 3x   76x 1x 1x   76x 76x 76x 76x 76x 76x 76x   76x 76x 76x     76x 76x 76x 76x 76x   76x 76x 76x     76x 76x 76x 76x 76x   76x 76x   76x   76x 76x 76x 76x 76x   76x 76x   76x   76x 76x 76x 76x 76x   76x 76x   76x   76x 76x 76x 76x 76x   76x 76x   76x   76x 76x 76x 76x 76x 76x 76x       76x 76x 76x 76x 76x 76x       76x 76x 76x 76x 76x 76x         76x 76x 76x 76x     76x 76x 76x 76x 76x 76x 76x           76x 76x 76x 52x 52x 46x 46x 6x 6x 76x 76x 76x         76x  
import { toast } from "react-toastify";
import { useBackend, useBackendMutation } from "main/utils/useBackend";
import React, { useState } from "react";
import {
  Button,
  Col,
  Form,
  ModalBody,
  ModalHeader,
  Row,
} from "react-bootstrap";
import TeamsCSVUploadForm from "main/components/Teams/TeamsCSVUploadForm";
import TeamsForm from "main/components/Teams/TeamsForm";
import Modal from "react-bootstrap/Modal";
import TeamsTable from "main/components/Teams/TeamsTable";
 
export default function TeamsTabComponent({
  courseId,
  testIdPrefix,
  currentUser,
}) {
  const [postTeamModal, setPostTeamModal] = useState(false);
  const [csvModal, setCsvModal] = useState(false);
  const [errorPostTeamModal, setErrorPostTeamModal] = useState(false);
  const [errorPostCSVTeamModal, setErrorPostCSVTeamModal] = useState(false);
  const [partialSuccessPostCSVTeamModal, setPartialSuccessPostCSVTeamModal] =
    useState(false);
  const [successPostCSVTeamModal, setSuccessPostCSVTeamModal] = useState(false);
 
  const { data: teams } = useBackend(
    [`/api/teams/all?courseId=${courseId}`],
    // Stryker disable next-line StringLiteral : GET and empty string are equivalent
    { method: "GET", url: `/api/teams/all?courseId=${courseId}` },
    [],
    true,
  );
  const [searchTeams, setSearchTeams] = useState("");
 
  const objectToAxiosParamsCSV = (formData) => {
    const file = new FormData();
    file.append("file", formData.upload[0]);
    return {
      url: `/api/teams/upload/csv`,
      data: file,
      params: {
        courseId: courseId,
      },
      method: "POST",
    };
  };
 
  const objectToAxiosParamsPost = (team) => ({
    url: `/api/teams/post`,
    method: "POST",
    params: {
      courseId: courseId,
      name: team.name,
    },
  });
 
  const objectToAxiosParamsPushTeamsToGithub = () => ({
    url: `/api/jobs/launch/pushTeamsToGithub`,
    method: "POST",
    params: {
      courseId: courseId,
    },
  });
 
  const onSuccessTeams = (modalFn) => {
    toast("Team successfully added.");
    setSearchTeams("");
    modalFn(false);
  };
 
  const onSuccessPushTeamsToGithub = () => {
    toast("Push teams to Github job successfully started.");
  };
 
  const teamPostMutation = useBackendMutation(
    objectToAxiosParamsPost,
    {
      onSuccess: () => onSuccessTeams(setPostTeamModal),
      onError: (error) => {
        setPostTeamModal(false);
        if (error.response.status === 409) {
          setErrorPostTeamModal({
            message: `Team name already exists. Please choose a different name.`,
          });
        } else {
          setErrorPostTeamModal({
            message: `${JSON.stringify(error.response.status)} error occurred while attempting to create team.`,
          });
        }
      },
    },
    [`/api/teams/all?courseId=${courseId}`],
  );
 
  const teamCsvMutation = useBackendMutation(
    objectToAxiosParamsCSV,
    {
      onSuccess: (data) => {
        setCsvModal(false);
        setSearchTeams("");
        setSuccessPostCSVTeamModal({
          message: `New members: ${data.created}, Existing members: ${data.existing}`,
        });
      },
      onError: (error) => {
        setCsvModal(false);
        setSearchTeams("");
        if (error.response.status === 400) {
          setErrorPostCSVTeamModal({
            message: `Upload failed (Error 400). Please ensure your CSV follows one of the formats documented in the 'Help' section.`,
          });
        } else if (error.response.status === 409) {
          setPartialSuccessPostCSVTeamModal({
            message: `CSV Import Complete with Rejected Members (Error 409). Rejected Students: ${JSON.stringify(error.response.data.rejected, null, 2)}, Existing Students: ${JSON.stringify(error.response.data.existing, null, 2)}, New Students: ${JSON.stringify(error.response.data.created, null, 2)}`,
          });
        } else {
          setErrorPostCSVTeamModal({
            message: `${JSON.stringify(error.response.status, null, 2)} error occurred while processing the CSV file.`,
          });
        }
      },
    },
    [`/api/teams/all?courseId=${courseId}`],
  );
 
  const pushTeamsToGithubMutation = useBackendMutation(
    objectToAxiosParamsPushTeamsToGithub,
    { onSuccess: onSuccessPushTeamsToGithub },
  );
 
  const handleCsvSubmit = (formData) => {
    teamCsvMutation.mutate(formData);
  };
 
  const handlePostSubmit = (team) => {
    teamPostMutation.mutate(team);
  };
 
  const handlePushTeamsToGithub = () => {
    pushTeamsToGithubMutation.mutate();
  };
 
  return (
    <div data-testid={`${testIdPrefix}-teams-tab-component`}>
      <Modal
        show={csvModal}
        onHide={() => setCsvModal(false)}
        centered={true}
        data-testid={`${testIdPrefix}-csv-modal`}
      >
        <ModalHeader closeButton>Upload Teams by CSV</ModalHeader>
        <ModalBody>
          <TeamsCSVUploadForm submitAction={handleCsvSubmit} />
        </ModalBody>
      </Modal>
      <Modal
        show={postTeamModal}
        onHide={() => setPostTeamModal(false)}
        centered={true}
        data-testid={`${testIdPrefix}-post-modal`}
      >
        <ModalHeader closeButton>Add Individual Team</ModalHeader>
        <ModalBody>
          <TeamsForm submitAction={handlePostSubmit} cancelDisabled={true} />
        </ModalBody>
      </Modal>
      <Modal
        show={errorPostTeamModal}
        onHide={() => setErrorPostTeamModal(false)}
        centered={true}
        data-testid={`${testIdPrefix}-error-post-team-modal`}
      >
        <ModalHeader closeButton>
          <h4 className="text-danger"> Error Creating Team </h4>
        </ModalHeader>
        <ModalBody>{errorPostTeamModal.message}</ModalBody>
      </Modal>
      <Modal
        show={errorPostCSVTeamModal}
        onHide={() => setErrorPostCSVTeamModal(false)}
        centered={true}
        data-testid={`${testIdPrefix}-error-post-csv-team-modal`}
      >
        <ModalHeader closeButton>
          <h4 className="text-danger"> CSV Import Unsuccessful </h4>
        </ModalHeader>
        <ModalBody>{errorPostCSVTeamModal.message}</ModalBody>
      </Modal>
      <Modal
        show={successPostCSVTeamModal}
        onHide={() => setSuccessPostCSVTeamModal(false)}
        centered={true}
        data-testid={`${testIdPrefix}-success-post-csv-team-modal`}
      >
        <ModalHeader closeButton>
          <h4 className="text-success"> CSV Import Successful </h4>
        </ModalHeader>
        <ModalBody>{successPostCSVTeamModal.message}</ModalBody>
      </Modal>
      <Modal
        show={partialSuccessPostCSVTeamModal}
        onHide={() => setPartialSuccessPostCSVTeamModal(false)}
        centered={true}
        data-testid={`${testIdPrefix}-partial-success-post-csv-team-modal`}
      >
        <ModalHeader closeButton>
          <h4 className="text-danger"> CSV Import Completed with Errors </h4>
        </ModalHeader>
        <ModalBody>{partialSuccessPostCSVTeamModal.message}</ModalBody>
      </Modal>
      <Row sm={3} className="p-2">
        <Col>
          <Button
            onClick={() => setCsvModal(true)}
            data-testid={`${testIdPrefix}-csv-button`}
            className="w-100"
          >
            Upload Teams by CSV
          </Button>
        </Col>
        <Col>
          <Button
            onClick={() => setPostTeamModal(true)}
            data-testid={`${testIdPrefix}-post-button`}
            className="w-100"
          >
            Add Individual Team
          </Button>
        </Col>
        <Col>
          <Button
            onClick={handlePushTeamsToGithub}
            className="w-100"
            data-testid={`${testIdPrefix}-push-teams-button`}
          >
            Push Teams to Github
          </Button>
        </Col>
      </Row>
      <Row className="mb-1 py-2">
        <Form>
          <Form.Group as={Row} controlId="searchFilter">
            <Form.Label column sm={2}>
              Search Teams:
            </Form.Label>
            <Col sm={10}>
              <Form.Control
                type="text"
                placeholder="Search by Team Name."
                value={searchTeams}
                onChange={(e) => setSearchTeams(e.target.value)}
                data-testid={`${testIdPrefix}-search`}
              />
            </Col>
          </Form.Group>
        </Form>
      </Row>
      <Row>
        <TeamsTable
          teams={(teams || []).filter((team) => {
            const searchTermLower = searchTeams.toLowerCase();
            if (team.name.toLowerCase().includes(searchTermLower)) {
              return true;
            }
            return false;
          })}
          currentUser={currentUser}
          courseId={courseId}
          testIdPrefix={`${testIdPrefix}-teams-table`}
        />
      </Row>
    </div>
  );
}