All files / components/Sections SectionsTable.js

100% Statements 58/58
100% Branches 11/11
100% Functions 28/28
100% Lines 57/57

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                                            1150x           41x 41x   41x   41x 5x 5x   41x 4x 4x             41x                     41x 41x 41x 1x       41x 19x 1x   1x         41x                 41x 2x         41x             41x 5x 4x   1x   5x     41x 3x     3x 3x         41x     317x         221x           344x               221x         317x           317x         221x         317x         221x       317x         221x       317x         221x       317x         221x       317x         221x               221x                           41x   41x   41x   41x                                                                             3x 3x                    
import SectionsTableBase from "main/components/SectionsTableBase";
import PersonalScheduleDropdown from "../PersonalSchedules/PersonalScheduleDropdown";
 
// import { useNavigate } from "react-router-dom";
import React, { useState, useEffect } from "react";
import { Modal, Button, Form } from "react-bootstrap";
import { toast } from "react-toastify";
import { yyyyqToQyy } from "main/utils/quarterUtilities.js";
import { useBackend, useBackendMutation } from "main/utils/useBackend";
import {
  convertToFraction,
  formatDays,
  formatInstructors,
  formatLocation,
  formatTime,
  isSection,
  formatStatus,
  formatInfoLink,
  renderInfoLink,
} from "main/utils/sectionUtils.js";
 
function getFirstVal(values) {
  return values[0];
}
 
export default function SectionsTable({ sections }) {
  // Stryker restore all
  // Stryker disable BooleanLiteral
  const [showModal, setShowModal] = useState(false);
  const [fetchSchedules, setFetchSchedules] = useState(false);
  // Stryker disable next-line all : initalState of selectedEnrollCode does not matter and doesnt need to be tested
  const [selectedEnrollCode, setSelectedEnrollCode] = useState("");
 
  const handleShow = () => {
    setShowModal(true);
    setFetchSchedules(true);
  };
  const handleClose = () => {
    setShowModal(false);
    setFetchSchedules(false);
  };
 
  const {
    data: schedules,
    error: _error,
    status: _status,
  } = useBackend(
    // Stryker disable next-line all : don't test internal caching of React Query
    ["/api/personalschedules/all"],
    // Stryker disable next-line all : don't test internal caching of React Query
    { method: "GET", url: "/api/personalschedules/all" },
    [],
    // Stryker disable next-line all : testing this would be hard as it has a default value of true in utils/useBackend.js
    { enabled: fetchSchedules }, // only calls useBackend when the modal is opened so that we dont get constant backend calls failure when not signed in
  );
 
  // Stryker disable all : not sure how to test/mock local storage
  const localSchedule = localStorage.getItem("ModalForm-psId");
  const [schedule, setSchedule] = useState(localSchedule || "");
  if (schedule) {
    localStorage.setItem("ModalForm-psId", schedule);
  }
  // Stryker restore all
 
  useEffect(() => {
    if (schedules && schedules.length > 0 && !localSchedule) {
      setSchedule(schedules[0].id);
      // Stryker disable all : not sure how to test/mock local storage
      localStorage.setItem("ModalForm-psId", schedules[0].id);
      // Stryker restore all
    }
  }, [schedules, localSchedule]);
 
  const objectToAxiosParams = (course) => ({
    url: "/api/courses/post",
    method: "POST",
    params: {
      enrollCd: course.enrollCd,
      psId: course.psId.psId,
    },
  });
 
  const onSuccess = (course) => {
    toast(
      `New course Created - id: ${course[0].id} enrollCd: ${course[0].enrollCd}`,
    );
  };
 
  const mutation = useBackendMutation(
    objectToAxiosParams,
    { onSuccess },
    // Stryker disable next-line all : hard to set up test for caching
    // ["/api/courses/user/all"],
  );
 
  const addCallback = (section) => {
    if (section.section) {
      setSelectedEnrollCode(section.section.enrollCode);
    } else {
      setSelectedEnrollCode(section.cells[9].value);
    }
    handleShow();
  };
 
  const saveCallback = () => {
    const psId = {
      psId: localStorage["ModalForm-psId"],
    };
    const dataFinal = { psId, enrollCd: selectedEnrollCode };
    mutation.mutate(dataFinal);
  };
 
  // Stryker restore all
  // Stryker disable BooleanLiteral
  const columns = [
    {
      Header: "Quarter",
      accessor: (row) => yyyyqToQyy(row.courseInfo.quarter),
      disableGroupBy: true,
      id: "quarter",
 
      aggregate: getFirstVal,
      Aggregated: ({ cell: { value } }) => `${value}`,
    },
    {
      Header: "Course ID",
      accessor: "courseInfo.courseId",
 
      Cell: ({ cell: { value } }) => value.substring(0, value.length - 2),
    },
    {
      Header: "Title",
      accessor: "courseInfo.title",
      disableGroupBy: true,
 
      aggregate: getFirstVal,
      Aggregated: ({ cell: { value } }) => `${value}`,
    },
    {
      // Stryker disable next-line StringLiteral: this column is hidden, very hard to test
      Header: "Is Section?",
      accessor: (row) => isSection(row.section.section),
      // Stryker disable next-line StringLiteral: this column is hidden, very hard to test
      id: "isSection",
    },
    {
      Header: "Status",
      accessor: (row) => formatStatus(row.section),
      disableGroupBy: true,
      id: "status",
 
      aggregate: getFirstVal,
      Aggregated: ({ cell: { value } }) => `${value}`,
    },
    {
      Header: "Enrolled",
      accessor: (row) =>
        convertToFraction(row.section.enrolledTotal, row.section.maxEnroll),
      disableGroupBy: true,
      id: "enrolled",
 
      aggregate: getFirstVal,
      Aggregated: ({ cell: { value } }) => `${value}`,
    },
    {
      Header: "Location",
      accessor: (row) => formatLocation(row.section.timeLocations),
      disableGroupBy: true,
      id: "location",
 
      aggregate: getFirstVal,
      Aggregated: ({ cell: { value } }) => `${value}`,
    },
    {
      Header: "Days",
      accessor: (row) => formatDays(row.section.timeLocations),
      disableGroupBy: true,
      id: "days",
 
      aggregate: getFirstVal,
      Aggregated: ({ cell: { value } }) => `${value}`,
    },
    {
      Header: "Time",
      accessor: (row) => formatTime(row.section.timeLocations),
      disableGroupBy: true,
      id: "time",
 
      aggregate: getFirstVal,
      Aggregated: ({ cell: { value } }) => `${value}`,
    },
    {
      Header: "Instructor",
      accessor: (row) => formatInstructors(row.section.instructors),
      disableGroupBy: true,
      id: "instructor",
 
      aggregate: getFirstVal,
      Aggregated: ({ cell: { value } }) => `${value}`,
    },
    {
      Header: "Enroll Code",
      accessor: "section.enrollCode",
      disableGroupBy: true,
 
      aggregate: getFirstVal,
      Aggregated: ({ cell: { value } }) => `${value}`,
    },
    {
      Header: "Info",
      accessor: formatInfoLink,
      Cell: renderInfoLink,
      disableGroupBy: true,
      id: "info",
 
      aggregate: getFirstVal,
      Aggregated: renderInfoLink,
    },
  ];
 
  const testid = "SectionsTable";
 
  const buttonColumns = [...columns];
 
  const columnsToDisplay = buttonColumns;
 
  return (
    <>
      <SectionsTableBase
        data={sections}
        columns={columnsToDisplay}
        testid={testid}
        addCallback={addCallback}
      />
 
      {/* Modal JSX */}
      <Modal show={showModal} onHide={handleClose}>
        <Modal.Header closeButton>
          <Modal.Title>Add Section to Schedule</Modal.Title>
        </Modal.Header>
        <Modal.Body>
          {/* Modal content here, such as a form to select a schedule and add a section */}
          <Form>
            <Form.Group className="mb-3" data-testid="ModalForm-enrollCd">
              <Form.Label>Enroll Code</Form.Label>
              <Form.Control type="text" value={selectedEnrollCode} readOnly />
            </Form.Group>
            <Form.Group className="mb-3" data-testid="ModalForm-psId">
              <PersonalScheduleDropdown
                schedules={schedules}
                schedule={schedule}
                setSchedule={setSchedule}
                controlId={"ModalForm-psId"}
              />
            </Form.Group>
          </Form>
        </Modal.Body>
        <Modal.Footer>
          <Button variant="secondary" onClick={handleClose}>
            Close
          </Button>
          <Button
            variant="primary"
            onClick={() => {
              // Implement the logic to add the section to a schedule here
              saveCallback();
              handleClose();
            }}
          >
            Save Changes
          </Button>
        </Modal.Footer>
      </Modal>
    </>
  );
}