All files / pages/Statistics CommonsMealAveragesPage.jsx

100% Statements 10/10
100% Branches 6/6
100% Functions 5/5
100% Lines 10/10

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                17x           17x 17x   17x 1x     17x   17x                         1x     32x                                   64x                                  
import React, { useState } from "react";
import { Form } from "react-bootstrap";
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import CommonsMealAveragesSection from "main/components/Statistics/CommonsMealAveragesSection";
import { useBackend } from "main/utils/useBackend";
import { PERIOD_OPTIONS } from "main/pages/Statistics/statisticsConstants";
 
export default function CommonsMealAveragesPage() {
  const { data: commons } = useBackend(
    ["/api/dining/all"],
    { method: "GET", url: "/api/dining/all" },
    [],
  );
 
  const [selectedCode, setSelectedCode] = useState("");
  const [period, setPeriod] = useState("ALL");
 
  const onChangePeriod = (e) => {
    setPeriod(e.target.value);
  };
 
  const effectiveCode = selectedCode || commons[0]?.code;
 
  return (
    <BasicLayout>
      <div className="pt-2">
        <h1>Meal Averages by Dining Commons</h1>
        {commons.length > 0 && (
          <Form.Group className="mb-3">
            <Form.Label htmlFor="CommonsMealAveragesPage-commons-select">
              Dining Commons
            </Form.Label>
            <Form.Select
              id="CommonsMealAveragesPage-commons-select"
              data-testid="CommonsMealAveragesPage-commons-select"
              value={effectiveCode}
              onChange={(e) => setSelectedCode(e.target.value)}
            >
              {commons.map((c) => (
                <option key={c.code} value={c.code}>
                  {c.name}
                </option>
              ))}
            </Form.Select>
          </Form.Group>
        )}
        <Form.Group className="mb-3">
          <Form.Label htmlFor="CommonsMealAveragesPage-period-select">
            Time period
          </Form.Label>
          <Form.Select
            id="CommonsMealAveragesPage-period-select"
            data-testid="CommonsMealAveragesPage-period-select"
            value={period}
            onChange={onChangePeriod}
          >
            {PERIOD_OPTIONS.map((opt) => (
              <option
                key={opt.value}
                value={opt.value}
                data-testid={`CommonsMealAveragesPage-period-option-${opt.value}`}
              >
                {opt.label}
              </option>
            ))}
          </Form.Select>
        </Form.Group>
        {effectiveCode && (
          <CommonsMealAveragesSection code={effectiveCode} period={period} />
        )}
      </div>
    </BasicLayout>
  );
}