All files / components/TabComponent EnrollmentTabComponent.jsx

100% Statements 193/193
100% Branches 30/30
100% Functions 17/17
100% Lines 193/193

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 2681x 1x 1x 1x                   1x 1x 1x 1x 1x   1x 65x 65x 65x 65x 65x 65x 65x 65x   65x 65x 65x 65x 65x 65x   65x   65x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x   65x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   65x 2x 2x 2x 2x 2x   65x 65x 65x 65x 65x 1x 1x   1x 65x 65x     65x 65x 65x 65x 65x 2x 1x 1x   1x 1x 1x 1x 1x 2x 65x 65x     65x 3x 3x   65x 2x 2x   65x 1x 1x   65x 65x 65x 65x 65x 65x 65x 65x   65x 65x 65x       65x 65x 65x       65x 65x 65x 65x 65x   65x 65x 65x     65x 65x 65x 65x 65x   65x 65x 65x 65x 65x       65x 65x   65x 65x 65x 65x 65x 65x         65x 65x 65x 65x         65x 65x 65x 65x 65x 65x 65x 65x 65x           65x 65x 65x 65x 65x 65x       65x 65x         65x 65x 65x 65x     65x 65x 65x 65x 65x 65x 65x           65x 65x 65x 65x 112x 112x 112x 61x 112x 51x 51x 1x 51x 50x 50x 1x 50x 1x 1x 48x 48x 65x 65x 65x 65x     65x 65x 65x 65x 65x   65x         65x  
import { toast } from "react-toastify";
import { useBackend, useBackendMutation } from "main/utils/useBackend";
import React, { useState } from "react";
import {
  Button,
  Col,
  Form,
  ModalBody,
  ModalHeader,
  Row,
  OverlayTrigger,
  Tooltip,
} from "react-bootstrap";
import RosterStudentCSVUploadForm from "main/components/RosterStudent/RosterStudentCSVUploadForm";
import RosterStudentForm from "main/components/RosterStudent/RosterStudentForm";
import RosterStudentTable from "main/components/RosterStudent/RosterStudentTable";
import Modal from "react-bootstrap/Modal";
import DroppedStudentsTable from "main/components/RosterStudent/DroppedStudentsTable";
 
export default function EnrollmentTabComponent({
  courseId,
  testIdPrefix,
  currentUser,
}) {
  const [postModal, setPostModal] = useState(false);
  const [csvModal, setCsvModal] = useState(false);
  const [csvErrorModal, setCsvErrorModal] = useState(false);
  const [csvErrorModalData, setCsvErrorModalData] = useState(null);
 
  const { data: rosterStudents } = useBackend(
    [`/api/rosterstudents/course/${courseId}`],
    // Stryker disable next-line StringLiteral : GET and empty string are equivalent
    { method: "GET", url: `/api/rosterstudents/course/${courseId}` },
    [],
    true,
  );
  const [searchTerm, setSearchTerm] = useState("");
 
  const objectToAxiosParamsCSV = (formData) => {
    const file = new FormData();
    file.append("file", formData.upload[0]);
    return {
      url: `/api/rosterstudents/upload/csv`,
      data: file,
      params: {
        courseId: courseId,
      },
      method: "POST",
    };
  };
 
  const objectToAxiosParamsPost = (student) => ({
    url: `/api/rosterstudents/post`,
    method: "POST",
    params: {
      courseId: courseId,
      firstName: student.firstName,
      lastName: student.lastName,
      studentId: student.studentId,
      email: student.email,
    },
  });
 
  const onSuccessRoster = (modalFn) => {
    toast("Roster successfully updated.");
    // Clear the search filter to show the updated roster
    setSearchTerm("");
    modalFn(false);
  };
 
  const rosterPostMutation = useBackendMutation(
    objectToAxiosParamsPost,
    {
      onSuccess: () => onSuccessRoster(setPostModal),
      onError: (error) => {
        toast.error(
          `Error adding student: ${JSON.stringify(error.response.data, null, 2)}`,
        );
      },
    },
    [`/api/rosterstudents/course/${courseId}`],
  );
 
  const rosterCsvMutation = useBackendMutation(
    objectToAxiosParamsCSV,
    {
      onSuccess: () => onSuccessRoster(setCsvModal),
      onError: (error) => {
        if (error.response.status !== 409) {
          toast.error(
            `Error uploading CSV: ${JSON.stringify(error.response.data, null, 2)}`,
          );
        } else {
          setCsvErrorModal(true);
          setCsvErrorModalData(error.response.data.rejected);
          setCsvModal(false);
        }
      },
    },
    [`/api/rosterstudents/course/${courseId}`],
  );
 
  const handleCsvSubmit = (formData) => {
    rosterCsvMutation.mutate(formData);
  };
 
  const handlePostSubmit = (student) => {
    rosterPostMutation.mutate(student);
  };
 
  const downloadCsv = () => {
    window.open(`/api/csv/rosterstudents?courseId=${courseId}`, "_blank");
  };
 
  return (
    <div data-testid={`${testIdPrefix}-EnrollmentTabComponent`}>
      <Modal
        show={csvErrorModal}
        onHide={() => setCsvErrorModal(false)}
        centered={true}
        data-testid={`${testIdPrefix}-csv-error-modal`}
        size="lg"
      >
        <ModalHeader closeButton>Upload CSV Roster</ModalHeader>
        <ModalBody>
          <p>
            The following students couldn&apos;t be uploaded to the roster as
            their emails and student IDs match two separate students:
          </p>
          <RosterStudentTable
            students={csvErrorModalData}
            testIdPrefix={`${testIdPrefix}-RosterStudentTable-csv-error`}
          />
        </ModalBody>
      </Modal>
      <Modal
        show={csvModal}
        onHide={() => setCsvModal(false)}
        centered={true}
        data-testid={`${testIdPrefix}-csv-modal`}
      >
        <ModalHeader closeButton>Upload CSV Roster</ModalHeader>
        <ModalBody>
          <RosterStudentCSVUploadForm submitAction={handleCsvSubmit} />
        </ModalBody>
      </Modal>
      <Modal
        show={postModal}
        onHide={() => setPostModal(false)}
        centered={true}
        data-testid={`${testIdPrefix}-post-modal`}
      >
        <ModalHeader closeButton>Add Individual Student</ModalHeader>
        <ModalBody>
          <RosterStudentForm
            submitAction={handlePostSubmit}
            cancelDisabled={true}
          />
        </ModalBody>
      </Modal>
      <Row sm={3} className="p-2">
        <Col>
          {}
          <div className="d-flex align-items-center gap-2 w-100">
            <Button
              onClick={() => setCsvModal(true)}
              data-testid={`${testIdPrefix}-csv-button`}
              className="flex-grow-1"
            >
              Upload CSV Roster
            </Button>
 
            {}
            <OverlayTrigger
              placement="right"
              overlay={
                <Tooltip id={`${testIdPrefix}-csv-help-tooltip`}>
                  CSV Upload format Help
                </Tooltip>
              }
            >
              <a
                href="/help/csv"
                target="_blank"
                rel="noopener noreferrer"
                aria-label="CSV Upload format Help"
                style={{
                  textDecoration: "none",
                }}
              >
                ℹ️
              </a>
            </OverlayTrigger>
          </div>
        </Col>
        <Col>
          <Button
            onClick={() => setPostModal(true)}
            data-testid={`${testIdPrefix}-post-button`}
            className="w-100"
          >
            Add Individual Student
          </Button>
        </Col>
        <Col>
          <Button onClick={downloadCsv} className="w-100">
            Download Student CSV
          </Button>
        </Col>
      </Row>
      <Row className="mb-1">
        <Form>
          <Form.Group as={Row} controlId="searchFilter">
            <Form.Label column sm={2}>
              Search Students:
            </Form.Label>
            <Col sm={10}>
              <Form.Control
                type="text"
                placeholder="Search by name, email, student ID, or Github Login"
                value={searchTerm}
                onChange={(e) => setSearchTerm(e.target.value)}
                data-testid={`${testIdPrefix}-search`}
              />
            </Col>
          </Form.Group>
        </Form>
      </Row>
      <Row>
        <RosterStudentTable
          students={rosterStudents
            .filter((student) => {
              const searchTermLower = searchTerm.toLowerCase();
              const fullName = `${student.firstName} ${student.lastName}`;
              if (student.studentId.toLowerCase().includes(searchTermLower)) {
                return true;
              } else if (
                student.email.toLowerCase().includes(searchTermLower)
              ) {
                return true;
              } else if (
                student.githubLogin?.toLowerCase().includes(searchTermLower)
              ) {
                return true;
              } else if (fullName.toLowerCase().includes(searchTermLower)) {
                return true;
              }
              return false;
            })
            .filter((student) => student.rosterStatus !== "DROPPED")}
          currentUser={currentUser}
          courseId={courseId}
          testIdPrefix={`${testIdPrefix}-RosterStudentTable`}
        />
      </Row>
      <Row>
        <h2>Dropped Students</h2>
        <DroppedStudentsTable
          students={rosterStudents.filter(
            (student) => student.rosterStatus === "DROPPED",
          )}
          courseId={courseId}
        />
      </Row>
    </div>
  );
}