Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions frontend/src/fixtures/menuItemFixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,39 @@ const menuItemFixtures = {
id: 1,
name: "Oatmeal (vgn)",
station: "Grill (Cafe)",
reviews: [{ itemsStars: 4 }, { itemsStars: 5 }, { itemsStars: 3 }],
reviewScore: 4.0,
},
],
fiveMenuItems: [
{
id: 1,
name: "Oatmeal (vgn)",
station: "Grill (Cafe)",
reviews: [{ itemsStars: 4 }, { itemsStars: 5 }],
reviewScore: 4.5,
},
{
id: 2,
name: "Blintz with Strawberry Compote (v)",
station: "Grill (Cafe)",
reviews: [],
reviewScore: null,
},
{
id: 3,
name: "Cage Free Scrambled Eggs (v)",
station: "Grill (Cafe)",
reviews: [{ itemsStars: 3 }, { itemsStars: 4 }],
reviewScore: 3.5,
},
{
id: 4,
name: "Cage Free Scrambled Egg Whites (v)",
station: "Grill (Cafe)",
reviews: [{ itemsStars: 5 }],
reviewScore: 5.0,
},
{
id: 5,
name: "Sliced Potato with Onions (vgn)",
station: "Grill (Cafe)",
reviews: null,
reviewScore: null,
},
],
};
Expand Down
19 changes: 7 additions & 12 deletions frontend/src/main/components/MenuItem/MenuItemTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,12 @@ export default function MenuItemTable({ menuItems, currentUser }) {
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 calculateAverageRating = (reviewScore) => {
if (!reviewScore) {
return "No ratings";
} else {
return reviewScore.toFixed(1);
}
};

const columns = [
Expand All @@ -39,7 +34,7 @@ export default function MenuItemTable({ menuItems, currentUser }) {
},
{
Header: "Average Rating",
accessor: (row) => calculateAverageRating(row.reviews),
accessor: (row) => calculateAverageRating(row.reviewScore),
id: "averageRating",
},
];
Expand Down
Loading
Loading