All files / components/Students StudentsTabComponent.jsx

100% Statements 29/29
100% Branches 2/2
100% Functions 11/11
100% Lines 28/28

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                        44x 44x   44x     44x             44x           44x 1x 1x     44x           44x 1x     44x 2x 2x 2x               44x 2x 2x     2x     2x     44x           44x 2x     44x   1x               1x                       2x               3x                        
import { toast } from "react-toastify";
import { useBackend, useBackendMutation } from "main/utils/useBackend";
import React, { useState } from "react";
import { Button, Modal } from "react-bootstrap";
import StudentsCSVUploadForm from "main/components/Students/StudentsCSVUploadForm";
import StudentsForm from "main/components/Students/StudentsForm";
import StudentsTable from "main/components/Students/StudentsTable";
 
// The Students tab of a course's admin page: adding students (individually
// or via CSV roster upload) is scoped to this courseId, not a free-standing
// admin page with a course-picker. See issue #251.
export default function StudentsTabComponent({ courseId, currentUser }) {
  const [showAddModal, setShowAddModal] = useState(false);
  const [showCsvModal, setShowCsvModal] = useState(false);
 
  const queryKey = [`/api/student/course/${courseId}`];
 
  // Stryker disable all
  const { data: students } = useBackend(
    queryKey,
    { method: "GET", url: `/api/student/course/${courseId}` },
    [],
  );
  // Stryker restore all
 
  const objectToAxiosParamsAdd = (student) => ({
    url: "/api/student",
    method: "POST",
    data: { ...student, courseId },
  });
 
  const onAddSuccess = () => {
    toast("Student added successfully.");
    setShowAddModal(false);
  };
 
  const addMutation = useBackendMutation(
    objectToAxiosParamsAdd,
    { onSuccess: onAddSuccess },
    queryKey,
  );
 
  const submitAddForm = (data) => {
    addMutation.mutate(data);
  };
 
  const objectToAxiosParamsCsv = (formData) => {
    const file = new FormData();
    file.append("file", formData.upload[0]);
    return {
      url: "/api/student/upload/csv",
      method: "POST",
      params: { courseId },
      data: file,
    };
  };
 
  const onCsvSuccess = (result) => {
    const skipped = result.skippedEmails.length;
    const skippedMessage = skipped
      ? `, ${skipped} skipped (already on roster)`
      : "";
    toast(
      `Roster uploaded: ${result.created} student(s) added${skippedMessage}.`,
    );
    setShowCsvModal(false);
  };
 
  const csvMutation = useBackendMutation(
    objectToAxiosParamsCsv,
    { onSuccess: onCsvSuccess },
    queryKey,
  );
 
  const submitCsvForm = (formData) => {
    csvMutation.mutate(formData);
  };
 
  return (
    <div data-testid="StudentsTabComponent">
      <Modal show={showAddModal} onHide={() => setShowAddModal(false)}>
        <Modal.Header closeButton>
          <Modal.Title>Add Individual Student</Modal.Title>
        </Modal.Header>
        <Modal.Body>
          <StudentsForm submitAction={submitAddForm} cancelDisabled={true} />
        </Modal.Body>
      </Modal>
      <Modal show={showCsvModal} onHide={() => setShowCsvModal(false)}>
        <Modal.Header closeButton>
          <Modal.Title>Upload CSV Roster</Modal.Title>
        </Modal.Header>
        <Modal.Body>
          <StudentsCSVUploadForm submitAction={submitCsvForm} />
        </Modal.Body>
      </Modal>
      <Button
        variant="primary"
        className="mb-3 me-2"
        data-testid="StudentsTabComponent-add-button"
        onClick={() => setShowAddModal(true)}
      >
        Add Individual Student
      </Button>
      <Button
        variant="secondary"
        className="mb-3"
        data-testid="StudentsTabComponent-csv-button"
        onClick={() => setShowCsvModal(true)}
      >
        Upload CSV Roster
      </Button>
      <StudentsTable
        students={students}
        currentUser={currentUser}
        courseId={courseId}
      />
    </div>
  );
}