All files / components/BasicCourseSearch CourseOverTimeSearchForm.jsx

100% Statements 23/23
100% Branches 15/15
100% Functions 4/4
100% Lines 23/23

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                                  2x 102x   102x 102x   102x   102x     102x     102x           102x                   102x   102x     102x     102x     102x 102x   102x 3x                 102x 25x 25x 25x             102x                                                                                     25x                                                                          
import { useState } from "react";
import { useForm } from "react-hook-form";
import { Form, Button, Container, Row, Col } from "react-bootstrap";
 
import { quarterRange, yyyyqToQyy } from "main/utils/quarterUtilities";
 
import { useSystemInfo } from "main/utils/systemInfo";
import SingleQuarterDropdown from "../Quarters/SingleQuarterDropdown";
import SingleSubjectDropdown from "../Subjects/SingleSubjectDropdown";
import { useBackend } from "main/utils/useBackend";
 
import {
  getCourseNumber,
  getSuffix,
  courseNumRegex,
} from "main/utils/courseNumberUtilities";
 
const CourseOverTimeSearchForm = ({ fetchJSON }) => {
  const { data: systemInfo } = useSystemInfo();
 
  const startQtr = systemInfo?.startQtrYYYYQ || "20211";
  const endQtr = systemInfo?.endQtrYYYYQ || "20214";
 
  const quarters = quarterRange(startQtr, endQtr);
 
  const localStartQuarter = localStorage.getItem(
    "CourseOverTimeSearch.StartQuarter",
  );
  const localEndQuarter = localStorage.getItem(
    "CourseOverTimeSearch.EndQuarter",
  );
  const localSubject = localStorage.getItem("CourseOverTimeSearch.Subject");
 
  const {
    data: subjects,
    error: _error,
    status: _status,
  } = useBackend(
    ["/api/UCSBSubjects/all"],
    {
      // Stryker disable next-line StringLiteral : GET is the default, so replacing with empty string is an equivalent mutation
      method: "GET",
      url: "/api/UCSBSubjects/all",
    },
    [],
  );
 
  const defaultSubjectArea = "ANTH";
 
  const [startQuarter, setStartQuarter] = useState(
    localStartQuarter || quarters[0].yyyyq,
  );
  const [endQuarter, setEndQuarter] = useState(
    localEndQuarter || quarters[0].yyyyq,
  );
  const [subject, setSubject] = useState(
    localSubject || subjects[0]?.subjectCode || defaultSubjectArea,
  );
  const [courseNumber, setCourseNumber] = useState("");
  const [courseSuf, setCourseSuf] = useState("");
 
  const submitAction = (event) => {
    fetchJSON(event, {
      startQuarter,
      endQuarter,
      subject,
      courseNumber,
      courseSuf,
    });
  };
 
  const handleCourseNumberOnChange = (event) => {
    const rawCourse = event.target.value;
    setCourseSuf(getSuffix(rawCourse));
    setCourseNumber(getCourseNumber(rawCourse));
  };
 
  const {
    register,
    formState: { errors },
    handleSubmit,
  } = useForm();
 
  return (
    <Form
      onSubmit={handleSubmit(submitAction)}
      data-testid="CourseOverTimeSearchForm"
    >
      <Container>
        <Row>
          <Col md="auto">
            <SingleQuarterDropdown
              quarters={quarters}
              quarter={startQuarter}
              setQuarter={setStartQuarter}
              controlId={"CourseOverTimeSearch.StartQuarter"}
              label={"Start Quarter"}
            />
          </Col>
          <Col md="auto">
            <SingleQuarterDropdown
              quarters={quarters}
              quarter={endQuarter}
              setQuarter={setEndQuarter}
              controlId={"CourseOverTimeSearch.EndQuarter"}
              label={"End Quarter"}
            />
          </Col>
          <Col md="auto">
            <SingleSubjectDropdown
              subjects={subjects}
              subject={subject}
              setSubject={setSubject}
              controlId={"CourseOverTimeSearch.Subject"}
              label={"Subject Area"}
            />
          </Col>
          <Col>
            <Form.Group controlId="CourseOverTimeSearchCourseNumber">
              <Form.Label>Course Number</Form.Label>
              <Form.Control
                isInvalid={Boolean(errors.CourseOverTimeSearchCourseNumber)}
                {...register("CourseOverTimeSearchCourseNumber", {
                  pattern: courseNumRegex,
                  onChange: (e) => handleCourseNumberOnChange(e), // Here's the fix!
                })}
              />
              <Form.Text muted>
                For example: &apos;16&apos; or &apos;130A&apos;; omit the
                subject area prefix.
              </Form.Text>
              <Form.Control.Feedback type="invalid">
                {errors.CourseOverTimeSearchCourseNumber &&
                  "Course Number is required. "}
                {errors.CourseOverTimeSearchCourseNumber?.type === "pattern" &&
                  "Course number should be a 1 to 3 digit number, optionally followed by up to two letters."}
              </Form.Control.Feedback>
            </Form.Group>
          </Col>
        </Row>
        <Row className="my-2" data-testid="CourseOverTimeSearchForm.ButtonRow">
          <Col md="auto">
            <Button variant="primary" type="submit">
              Submit
            </Button>
          </Col>
          <Col md="auto">
            <p data-testid="CourseOverTimeSearchForm.FullSearchString">
              Searching for:&nbsp;
              <code data-testid="CourseOverTimeSearchForm.SearchString">{`${subject} ${courseNumber}${courseSuf}`}</code>
              for quarters {`${yyyyqToQyy(startQuarter)}`} through&nbsp;
              {`${yyyyqToQyy(endQuarter)}`}
            </p>
          </Col>
        </Row>
      </Container>
    </Form>
  );
};
 
export default CourseOverTimeSearchForm;