All files / components/BasicCourseSearch CourseByInstructorSearchForm.js

100% Statements 18/18
100% Branches 6/6
100% Functions 3/3
100% Lines 18/18

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            2x   138x                       138x 138x   138x   138x   138x   138x 138x 138x   138x 3x 3x     138x 40x 40x     138x   138x                                                                              
import { useState } from "react";
import { Form, Button, Container, Row, Col } from "react-bootstrap";
import { quarterRange } from "main/utils/quarterUtilities";
import { useSystemInfo } from "main/utils/systemInfo";
import SingleQuarterDropdown from "../Quarters/SingleQuarterDropdown";
 
const CourseByInstructorSearchForm = ({ fetchJSON }) => {
 
  const { data: systemInfo } = useSystemInfo();
 
  // Note that we need to distinguish between the first and last
  // quarters of the quarter range shown in the dropdowns, vs. 
  // the selected start and end quarters for the search.
  // The first and last quarters of the range are determined by
  // quarterRangeFirst and quarterRangeLast, which are set in
  // systemInfo.json.  The selected start and end quarters are
  // initialized from localStorage; both default to the first
  // quarter in the range
 
 
  const quarterRangeFirst = systemInfo.startQtrYYYYQ || "20211";
  const quarterRangeLast = systemInfo.endQtrYYYYQ || "20214";
 
  const quarters = quarterRange(quarterRangeFirst, quarterRangeLast);
 
  const localStorageInstructor = localStorage.getItem("CourseByInstructorSearch.Instructor");
 
  const initialInstructor = localStorageInstructor || "";
  
  const [startQuarter, setStartQuarter] = useState(quarters[0].yyyyq);
  const [endQuarter, setEndQuarter] = useState(quarters[0].yyyyq);
  const [instructor, setInstructor] = useState(initialInstructor);
 
  const handleSubmit = (event) => {
    event.preventDefault();
    fetchJSON(event, { startQuarter, endQuarter, instructor });
  };
 
  const handleInstructorOnChange = (event) => {
    setInstructor(event.target.value);
    localStorage.setItem("CourseByInstructorSearch.Instructor", event.target.value);
  };
 
  const testid="CourseByInstructorSearchForm";
 
  return (
    <Form onSubmit={handleSubmit}>
      <Container>
        <Row>
          <Col md="auto">
            <SingleQuarterDropdown
              quarters={quarters}
              quarter={startQuarter}
              setQuarter={setStartQuarter}
              controlId={"CourseByInstructorSearch.StartQuarter"}
              label={"Start Quarter"}
            />
          </Col>
          <Col md="auto">
            <SingleQuarterDropdown
              quarters={quarters}
              quarter={endQuarter}
              setQuarter={setEndQuarter}
              controlId={"CourseByInstructorSearch.EndQuarter"}
              label={"End Quarter"}
            />
          </Col>
          <Form.Group controlId="CourseByInstructorSearch.Instructor">
            <Form.Label>Instructor (Try searching 'Conrad' or 'CONRAD P T')</Form.Label>
            <Form.Control onChange={handleInstructorOnChange} defaultValue={instructor} />
          </Form.Group>
        </Row>
        <Row data-testid={`${testid}-data-row`} style={{ paddingTop: 10, paddingBottom: 10 }}>
          <Col md="auto">
            <Button variant="primary" type="submit">
              Submit
            </Button>
          </Col>
        </Row>
      </Container>
    </Form>
  );
};
 
export default CourseByInstructorSearchForm;