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 | 35x 35x 35x 35x 1x 1x 35x 3x 3x 35x 44x 17x 27x 68x 49x 27x 49x 25x 35x 44x 35x 4x 4x 33x 33x 50x 60x 4x | import OurTable, { ButtonColumn } from "main/components/OurTable";
import { hasRole } from "main/utils/currentUser";
import { useNavigate } from "react-router";
import React, { useState } from "react";
export default function MenuItemTable({ menuItems, currentUser }) {
const testid = "MenuItemTable";
const navigate = useNavigate();
const [searchTerm, setSearchTerm] = useState("");
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 = (reviews) => {
if (!reviews || !Array.isArray(reviews) || reviews.length === 0)
return "No reviews";
const validRatings = reviews
.filter(
(r) => r && typeof r === "object" && typeof r.itemsStars === "number",
)
.map((r) => r.itemsStars);
if (validRatings.length === 0) return "No ratings";
const avg = validRatings.reduce((a, b) => a + b, 0) / validRatings.length;
return avg.toFixed(1);
};
const columns = [
{
Header: "Item Name",
accessor: "name",
},
{
Header: "Station",
accessor: "station",
},
{
Header: "Average Rating",
accessor: (row) => calculateAverageRating(row.reviews),
id: "averageRating",
},
];
if (hasRole(currentUser, "ROLE_USER")) {
columns.push(
ButtonColumn("Review Item", "warning", reviewCallback, testid),
);
columns.push(ButtonColumn("All Reviews", "warning", viewCallback, testid));
}
// split input (delimiters: space, comma) into non-empty keywords
// Stryker disable all : equivalent mutation problem (but this is more optimal than its equivalent mutant without the +)
const keywords = searchTerm.toLowerCase().split(/[\s,]+/);
// Stryker restore all
const searchedItems = menuItems.filter((item) => {
return keywords.every(
(kw) =>
item.name.toLowerCase().includes(kw) ||
item.station.toLowerCase().includes(kw),
);
});
return (
<>
<input
type="text"
placeholder="Search by keywords (separated by space or comma)"
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
className="form-control mb-3"
/>
<OurTable columns={columns} data={searchedItems} testid={testid} />
</>
);
}
|