All files / pages/CSV CSVDownloadsPage.jsx

100% Statements 36/36
100% Branches 29/29
100% Functions 8/8
100% Lines 35/35

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                                        78x 78x 78x 78x           78x           78x 78x 78x   78x       78x       78x     78x 78x 78x 78x       78x 58x 19x       78x   78x           78x 8x     78x 3x 3x     78x 6x 6x     78x                                                                                                                   7x       7x                                                                   1x 1x                             1x 1x                                                            
import React, { useEffect, useState } from "react";
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import {
  Accordion,
  Form,
  Button,
  Container,
  Row,
  Col,
  FormCheck,
} from "react-bootstrap";
import { useSystemInfo } from "main/utils/systemInfo";
import { quarterRange } from "main/utils/quarterUtilities";
import { useBackend } from "main/utils/useBackend";
import SingleQuarterDropdown from "main/components/Quarters/SingleQuarterDropdown";
import SingleSubjectDropdown from "main/components/Subjects/SingleSubjectDropdown";
import SingleLevelDropdown from "main/components/Levels/SingleLevelDropdown";
import { allTheLevels } from "fixtures/levelsFixtures";
 
export default function CSVDownloadsPage() {
  const { data: systemInfo } = useSystemInfo();
  const startQtr = systemInfo?.startQtrYYYYQ || "20221";
  const endQtr = systemInfo?.endQtrYYYYQ || "20222";
  const quarters = quarterRange(startQtr, endQtr);
 
  const {
    data: subjects,
    error: _error,
    status: _status,
  } = useBackend(
    ["/api/UCSBSubjects/all"],
    { method: "GET", url: "/api/UCSBSubjects/all" },
    [],
  );
 
  const localQuarter = localStorage.getItem("CSVDownloads.Quarter");
  const localSubjectArea = localStorage.getItem("CSVDownloads.SubjectArea");
  const localLevel = localStorage.getItem("CSVDownloads.Level");
  const localOmitSections =
    localStorage.getItem("CSVDownloads.OmitSections") === "false"
      ? false
      : true;
  const localWithTimeLocations =
    localStorage.getItem("CSVDownloads.WithTimeLocations") === "false"
      ? false
      : true;
 
  const [quarter, setQuarter] = useState(
    localQuarter || quarters[quarters.length - 1].yyyyq,
  );
  const [subjectArea, setSubjectArea] = useState(localSubjectArea || "");
  const [level, setLevel] = useState(localLevel || "U");
  const [omitSections, setOmitSections] = useState(localOmitSections);
  const [withTimeLocations, setWithTimeLocations] = useState(
    localWithTimeLocations,
  );
 
  useEffect(() => {
    if (subjects.length > 0 && !subjectArea) {
      setSubjectArea(subjects[0].subjectCode);
    }
  }, [subjects, subjectArea]);
 
  const byQuarterUrl = `/api/courses/csv/quarter?yyyyq=${encodeURIComponent(quarter)}`;
  const byQuarterAndSubjectUrl =
    `/api/courses/csv/byQuarterAndSubjectArea?yyyyq=${encodeURIComponent(quarter)}` +
    `&subjectArea=${encodeURIComponent(subjectArea)}` +
    `&level=${encodeURIComponent(level)}` +
    `&omitSections=${encodeURIComponent(omitSections ? "true" : "false")}` +
    `&withTimeLocations=${encodeURIComponent(withTimeLocations ? "true" : "false")}`;
 
  const downloadCsv = (url) => {
    window.location.assign(url);
  };
 
  const handleQuarterSubmit = (e) => {
    e.preventDefault();
    downloadCsv(byQuarterUrl);
  };
 
  const handleQuarterSubjectSubmit = (e) => {
    e.preventDefault();
    if (subjectArea) downloadCsv(byQuarterAndSubjectUrl);
  };
 
  return (
    <BasicLayout>
      <div className="container mt-3">
        <h1>CSV Downloads</h1>
 
        <Accordion defaultActiveKey="by-quarter" className="mt-3">
          {/* Download by Quarter */}
          <Accordion.Item eventKey="by-quarter">
            <Accordion.Header>
              Download all UCSB classes by Quarter
            </Accordion.Header>
            <Accordion.Body>
              <Form onSubmit={handleQuarterSubmit}>
                <Container>
                  <Row className="mb-3">
                    <Col md="auto">
                      <SingleQuarterDropdown
                        quarters={quarters}
                        quarter={quarter}
                        setQuarter={setQuarter}
                        controlId={"CSVDownloads.Quarter"}
                        label={"Quarter"}
                      />
                    </Col>
                  </Row>
                  <Row>
                    <Col md="auto">
                      <Button
                        type="submit"
                        variant="primary"
                        disabled={!quarter}
                      >
                        Download CSV
                      </Button>
                    </Col>
                  </Row>
                </Container>
              </Form>
            </Accordion.Body>
          </Accordion.Item>
 
          {/* Download by Quarter + Subject Area */}
          <Accordion.Item eventKey="by-quarter-and-subject-area">
            <Accordion.Header>
              Download all UCSB classes by Quarter and Subject Area
            </Accordion.Header>
            <Accordion.Body>
              <Form onSubmit={handleQuarterSubjectSubmit}>
                <Container>
                  <Row className="mb-3">
                    <Col md="auto">
                      <SingleQuarterDropdown
                        quarters={quarters}
                        quarter={quarter}
                        setQuarter={setQuarter}
                        controlId={"CSVDownloads.QuarterBySubjectArea"}
                        label={"Quarter"}
                        onChange={(e) => {
                          localStorage.setItem(
                            "CSVDownloads.Quarter",
                            e.target.value,
                          );
                          localStorage.setItem(
                            "CSVDownloads.QuarterBySubjectArea",
                            e.target.value,
                          );
                        }}
                      />
                    </Col>
                    <Col md="auto">
                      {subjects.length > 0 && subjectArea && (
                        <SingleSubjectDropdown
                          subjects={subjects}
                          subject={subjectArea}
                          setSubject={setSubjectArea}
                          controlId={"CSVDownloads.SubjectArea"}
                          label={"Subject Area"}
                        />
                      )}
                    </Col>
                    <Col md="auto">
                      <SingleLevelDropdown
                        levels={allTheLevels}
                        setLevel={setLevel}
                        controlId={"CSVDownloads.Level"}
                        label={"Level"}
                      />
                    </Col>
                  </Row>
                  <Row className="mb-3">
                    <Col md="auto">
                      <Form.Group controlId="CSVDownloads.OmitSections">
                        <FormCheck
                          data-testid="CSVDownloads.OmitSections-checkbox"
                          label="Omit sections"
                          onChange={(e) => {
                            setOmitSections(e.target.checked);
                            localStorage.setItem(
                              "CSVDownloads.OmitSections",
                              e.target.checked.toString(),
                            );
                          }}
                          checked={omitSections}
                        />
                      </Form.Group>
                    </Col>
                    <Col md="auto">
                      <Form.Group controlId="CSVDownloads.WithTimeLocations">
                        <FormCheck
                          data-testid="CSVDownloads.WithTimeLocations-checkbox"
                          label="With time/locations"
                          onChange={(e) => {
                            setWithTimeLocations(e.target.checked);
                            localStorage.setItem(
                              "CSVDownloads.WithTimeLocations",
                              e.target.checked.toString(),
                            );
                          }}
                          checked={withTimeLocations}
                        />
                      </Form.Group>
                    </Col>
                  </Row>
                  <Row>
                    <Col md="auto">
                      <Button
                        type="submit"
                        variant="primary"
                        disabled={!quarter || !subjectArea}
                      >
                        Download CSV
                      </Button>
                    </Col>
                  </Row>
                </Container>
              </Form>
            </Accordion.Body>
          </Accordion.Item>
        </Accordion>
      </div>
    </BasicLayout>
  );
}