All files / components/Classrooms SingleClassroomDropdown.js

100% Statements 20/20
100% Branches 11/11
100% Functions 7/7
100% Lines 19/19

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        1x                   32x   32x         32x   32x 18x 17x         26x 14x     2x         32x 2x 2x 2x 2x 1x       32x                             40x 40x                        
import React, { useState, useEffect } from "react";
import { Form } from "react-bootstrap";
import axios from "axios";
 
const SingleClassroomDropdown = ({
  buildingCode,
  quarter,
  classroom,
  setClassroom,
  controlId,
  onChange = null,
  label = "Classroom",
  showAll = false,
}) => {
  const localSearchClassroom = localStorage.getItem(controlId);
 
  const [classroomState, setClassroomState] = useState(
    // Stryker disable next-line all : not sure how to test/mock local storage
    localSearchClassroom || classroom,
  );
 
  const [classrooms, setClassrooms] = useState([]);
 
  useEffect(() => {
    if (buildingCode) {
      axios
        .get("/api/public/classrooms/roomnumbers", {
          params: { quarter, buildingCode },
        })
        .then((response) => {
          const sorted = [...response.data].sort((a, b) => a.localeCompare(b));
          setClassrooms(sorted);
        })
        .catch((error) => {
          console.error("Error fetching classrooms:", error);
        });
    }
  }, [buildingCode, quarter]);
 
  const handleClassroomOnChange = (event) => {
    localStorage.setItem(controlId, event.target.value);
    setClassroomState(event.target.value);
    setClassroom(event.target.value);
    if (onChange != null) {
      onChange(event);
    }
  };
 
  return (
    <Form.Group controlId={controlId}>
      <Form.Label>{label}</Form.Label>
      <Form.Control
        as="select"
        value={classroomState}
        onChange={handleClassroomOnChange}
      >
        <option value="">Select a classroom</option>
        {showAll && (
          <option data-testid={`${controlId}-option-all`} value="ALL">
            ALL
          </option>
        )}
        {classrooms.map((room) => {
          const key = `${controlId}-option-${room.replace(/ /g, "-")}`;
          return (
            <option key={key} data-testid={key} value={room}>
              {room}
            </option>
          );
        })}
      </Form.Control>
    </Form.Group>
  );
};
 
export default SingleClassroomDropdown;