All files / components/BasicCourseSearch CourseOverTimeDescriptionSearchForm.jsx

100% Statements 23/23
100% Branches 12/12
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            2x 89x           89x 89x   89x   89x       89x     89x       89x   89x       89x   89x 89x   89x 4x 4x     89x 59x     89x 4x 4x           89x   89x                                                                                                            
import { useState } from "react";
import { Form, Button, Container, Row, Col, FormCheck } from "react-bootstrap";
import { quarterRange } from "main/utils/quarterUtilities";
import { useSystemInfo } from "main/utils/systemInfo";
import SingleQuarterDropdown from "../Quarters/SingleQuarterDropdown";
 
const CourseOverTimeDescriptionSearchForm = ({ fetchJSON }) => {
  const { data: systemInfo } = useSystemInfo();
 
  // Don't confuse the startQtr and endQtr which are the system defaults
  // for the first and last values in the dropdown lists, with the actual
  // *currently selectted* start and end quarters for the search!
 
  const firstQtr = systemInfo.startQtrYYYYQ || "20211";
  const lastQtr = systemInfo.endQtrYYYYQ || "20214";
 
  const quarters = quarterRange(firstQtr, lastQtr);
 
  const localStartQuarter = localStorage.getItem(
    "CourseOverTimeDescriptionSearch.StartQuarter",
  );
 
  const localEndQuarter = localStorage.getItem(
    "CourseOverTimeDescriptionSearch.EndQuarter",
  );
  const localSearchTerms = localStorage.getItem(
    "CourseOverTimeDescriptionSearch.SearchTerms",
  );
  const localStorageCheckbox =
    localStorage.getItem("CourseOverTimeDescriptionSearch.Checkbox") === "true";
 
  const [startQuarter, setStartQuarter] = useState(
    localStartQuarter || firstQtr,
  );
 
  const [endQuarter, setEndQuarter] = useState(localEndQuarter || lastQtr);
 
  const [searchTerms, setSearchTerms] = useState(localSearchTerms || "");
  const [checkbox, setCheckbox] = useState(localStorageCheckbox || false);
 
  const handleSubmit = (event) => {
    event.preventDefault();
    fetchJSON(event, { startQuarter, endQuarter, searchTerms, checkbox });
  };
 
  const handleSearchTermsOnChange = (event) => {
    setSearchTerms(event.target.value);
  };
 
  const handleCheckboxOnChange = (event) => {
    setCheckbox(event.target.checked);
    localStorage.setItem(
      "CourseOverTimeDescriptionSearch.Checkbox",
      event.target.checked.toString(),
    );
  };
 
  const testid = "CourseOverTimeDescriptionSearchForm";
 
  return (
    <Form onSubmit={handleSubmit}>
      <Container>
        <Row>
          <Col md="auto">
            <SingleQuarterDropdown
              quarters={quarters}
              quarter={startQuarter}
              setQuarter={setStartQuarter}
              controlId={"CourseOverTimeDescriptionSearch.StartQuarter"}
              label={"Start Quarter"}
            />
          </Col>
          <Col md="auto">
            <SingleQuarterDropdown
              quarters={quarters}
              quarter={endQuarter}
              setQuarter={setEndQuarter}
              controlId={"CourseOverTimeDescriptionSearch.EndQuarter"}
              label={"End Quarter"}
            />
          </Col>
        </Row>
        <Form.Group controlId="CourseOverTimeDescriptionSearch.SearchTerms">
          <Form.Label>Search terms</Form.Label>
          <Form.Control
            onChange={handleSearchTermsOnChange}
            defaultValue={searchTerms}
          />
        </Form.Group>
        <Form.Group controlId="CourseOverTimeDescriptionSearch.Checkbox">
          <FormCheck
            data-testid={`${testid}-checkbox`}
            label="Lectures Only"
            onChange={handleCheckboxOnChange}
            checked={checkbox}
          ></FormCheck>
        </Form.Group>
        <Row
          data-testid={`${testid}-bottom-row`}
          style={{ paddingTop: 10, paddingBottom: 10 }}
        >
          <Col md="auto">
            <Button variant="primary" type="submit">
              Submit
            </Button>
          </Col>
        </Row>
      </Container>
    </Form>
  );
};
 
export default CourseOverTimeDescriptionSearchForm;