All files / pages/CSV CSVDownloadsPage.jsx

100% Statements 33/33
100% Branches 14/14
100% Functions 6/6
100% Lines 33/33

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                        17x 17x 17x 17x           17x           17x 17x 17x 17x 17x       17x     17x 17x 17x 17x     17x       17x   17x           17x 3x     17x 1x 1x     17x 2x 2x     17x 1x 1x           17x 1x 1x           17x                                                                                                                                                            
import React, { useState } from "react";
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import { Accordion, Form, Button, FormCheck } from "react-bootstrap";
import SingleQuarterDropdown from "../../components/Quarters/SingleQuarterDropdown";
import SingleSubjectDropdown from "../../components/Subjects/SingleSubjectDropdown";
import SingleLevelDropdown from "../../components/Levels/SingleLevelDropdown";
import { useSystemInfo } from "main/utils/systemInfo";
import { quarterRange } from "main/utils/quarterUtilities";
import { useBackend } from "main/utils/useBackend";
import { allTheLevels } from "fixtures/levelsFixtures";
 
export default function CSVDownloadsPage() {
  const { data: systemInfo } = useSystemInfo();
  const startQtr = systemInfo?.startQtrYYYYQ || "20084";
  const endQtr = systemInfo?.endQtrYYYYQ || "20262";
  const quarters = quarterRange(startQtr, endQtr);
 
  const {
    data: subjects,
    error: _error,
    status: _status,
  } = useBackend(
    ["/api/UCSBSubjects/all"],
    { method: "GET", url: "/api/UCSBSubjects/all" },
    [],
  );
 
  const localSearchQuarter = localStorage.getItem("CSVDownloads.Quarter");
  const localSearchSubject = localStorage.getItem("CSVDownloads.Subject");
  const localLevel = localStorage.getItem("CSVDownloads.Level");
  const localOmitSections = localStorage.getItem("CSVDownloads.OmitSections");
  const localWithTimeLocations = localStorage.getItem(
    "CSVDownloads.WithTimeLocations",
  );
 
  const [quarter, setQuarter] = useState(
    localSearchQuarter || quarters[quarters.length - 1].yyyyq,
  );
  const defaultSubject = "ANTH";
  const [subject, setSubject] = useState(localSearchSubject || defaultSubject);
  const [level, setLevel] = useState(localLevel || "U");
  const [omitSections, setOmitSections] = useState(
    localOmitSections === null ? true : localOmitSections === "true",
  );
  const [withTimeLocations, setWithTimeLocations] = useState(
    localWithTimeLocations === null ? true : localWithTimeLocations === "true",
  );
 
  const byQuarterUrl = `/api/courses/csv/quarter?yyyyq=${encodeURIComponent(quarter)}`;
  const byQuarterAndSubjectUrl =
    `/api/courses/csv/byQuarterAndSubjectArea?yyyyq=${encodeURIComponent(quarter)}` +
    `&subjectArea=${encodeURIComponent(subject)}` +
    `&level=${encodeURIComponent(level)}` +
    `&omitSections=${encodeURIComponent(omitSections)}` +
    `&withTimeLocations=${encodeURIComponent(withTimeLocations)}`;
 
  const downloadCsv = (url) => {
    window.location.assign(url);
  };
 
  const handleQuarterSubmit = (e) => {
    e.preventDefault();
    downloadCsv(byQuarterUrl);
  };
 
  const handleQuarterSubjectSubmit = (e) => {
    e.preventDefault();
    downloadCsv(byQuarterAndSubjectUrl);
  };
 
  const handleOmitSectionsChange = (event) => {
    setOmitSections(event.target.checked);
    localStorage.setItem(
      "CSVDownloads.OmitSections",
      event.target.checked.toString(),
    );
  };
 
  const handleWithTimeLocationsChange = (event) => {
    setWithTimeLocations(event.target.checked);
    localStorage.setItem(
      "CSVDownloads.WithTimeLocations",
      event.target.checked.toString(),
    );
  };
 
  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}>
                <SingleQuarterDropdown
                  quarters={quarters}
                  quarter={quarter}
                  setQuarter={setQuarter}
                  controlId="CSVDownloads.Quarter"
                  label="Quarter"
                />
                <Button type="submit" variant="primary" className="mt-3">
                  Download CSV
                </Button>
              </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}>
                <SingleQuarterDropdown
                  quarters={quarters}
                  quarter={quarter}
                  setQuarter={setQuarter}
                  controlId="CSVDownloads.Quarter"
                  label="Quarter"
                />
                <SingleSubjectDropdown
                  subjects={subjects}
                  subject={subject}
                  setSubject={setSubject}
                  controlId="CSVDownloads.Subject"
                  label="Subject Area"
                />
                <SingleLevelDropdown
                  levels={allTheLevels}
                  level={level}
                  setLevel={setLevel}
                  controlId={"CSVDownloads.Level"}
                  label="Course Level"
                />
                <FormCheck
                  data-testid={`CSVDownloads.OmitSections-checkbox`}
                  label="Omit Sections"
                  onChange={handleOmitSectionsChange}
                  checked={omitSections}
                ></FormCheck>
                <FormCheck
                  data-testid={`CSVDownloads.WithTimeLocations-checkbox`}
                  label="Only include courses with assigned Times and Locations"
                  onChange={handleWithTimeLocationsChange}
                  checked={withTimeLocations}
                ></FormCheck>
                <Button type="submit" variant="primary" className="mt-3">
                  Download CSV
                </Button>
              </Form>
            </Accordion.Body>
          </Accordion.Item>
        </Accordion>
      </div>
    </BasicLayout>
  );
}