All files / pages/Statistics BestItemsPage.jsx

100% Statements 4/4
100% Branches 2/2
100% Functions 2/2
100% Lines 4/4

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                16x   16x                   16x 1x                                                                                    
import React, { useState } from "react";
import { Form } from "react-bootstrap";
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import ItemStatisticsTable from "main/components/Statistics/ItemStatisticsTable";
import { useBackend } from "main/utils/useBackend";
import { PERIOD_OPTIONS } from "main/pages/Statistics/statisticsConstants";
 
export default function BestItemsPage() {
  const [period, setPeriod] = useState("ALL");
 
  const { data: items } = useBackend(
    ["/api/statistics/items/best", period],
    {
      method: "GET",
      url: "/api/statistics/items/best",
      params: { period },
    },
    [],
  );
 
  const onChangePeriod = (e) => {
    setPeriod(e.target.value);
  };
 
  return (
    <BasicLayout>
      <div className="pt-2">
        <h1>Best Rated Items</h1>
        <Form.Group className="mb-3">
          <Form.Label htmlFor="BestItemsPage-period-select">
            Time period
          </Form.Label>
          <Form.Select
            id="BestItemsPage-period-select"
            data-testid="BestItemsPage-period-select"
            value={period}
            onChange={onChangePeriod}
          >
            {PERIOD_OPTIONS.map((opt) => (
              <option
                key={opt.value}
                value={opt.value}
                data-testid={`BestItemsPage-period-option-${opt.value}`}
              >
                {opt.label}
              </option>
            ))}
          </Form.Select>
        </Form.Group>
        {items && items.length === 0 ? (
          <p data-testid="BestItemsPage-empty">
            No reviews available for this period yet.
          </p>
        ) : (
          <ItemStatisticsTable
            items={items}
            testIdPrefix="BestItemsPage-table"
          />
        )}
      </div>
    </BasicLayout>
  );
}