All files / components/MenuItem MenuItemTable.jsx

100% Statements 17/17
100% Branches 4/4
100% Functions 5/5
100% Lines 17/17

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          13x 13x 13x 1x 1x     13x 2x 2x     13x 16x 5x   11x       13x                     16x         13x 4x     4x          
import OurTable, { ButtonColumn } from "main/components/OurTable";
import { hasRole } from "main/utils/currentUser";
import { useNavigate } from "react-router";
 
export default function MenuItemTable({ menuItems, currentUser }) {
  const testid = "MenuItemTable";
  const navigate = useNavigate();
  const reviewCallback = async (_cell) => {
    const itemId = _cell.row.original.id;
    navigate(`/reviews/post/${itemId}`);
  };
 
  const viewCallback = async (_cell) => {
    const itemId = _cell.row.original.id;
    navigate(`/reviews/${itemId}`);
  };
 
  const calculateAverageRating = (reviewScore) => {
    if (!reviewScore) {
      return "No ratings";
    } else {
      return reviewScore.toFixed(1);
    }
  };
 
  const columns = [
    {
      Header: "Item Name",
      accessor: "name",
    },
    {
      Header: "Station",
      accessor: "station",
    },
    {
      Header: "Average Rating",
      accessor: (row) => calculateAverageRating(row.reviewScore),
      id: "averageRating",
    },
  ];
 
  if (hasRole(currentUser, "ROLE_USER")) {
    columns.push(
      ButtonColumn("Review Item", "warning", reviewCallback, testid),
    );
    columns.push(ButtonColumn("All Reviews", "warning", viewCallback, testid));
  }
 
  return <OurTable columns={columns} data={menuItems} testid={testid} />;
}