diff --git a/src/components/shopping/CurrentShoppingListPage.vue b/src/components/shopping/CurrentShoppingListPage.vue
index 67dcc53..1a33998 100644
--- a/src/components/shopping/CurrentShoppingListPage.vue
+++ b/src/components/shopping/CurrentShoppingListPage.vue
@@ -21,7 +21,7 @@
-
+
Purchased Meals
@@ -138,7 +138,7 @@ button img {
import alert from '@/alert.js'
import data from '@/data.js'
-import { itemsToGroups, groupsToItems } from './shopping.js'
+import { itemsToGroups, groupsToItems, uniqueMeals } from './shopping.js'
import MealSelectionList from './MealSelectionList.vue'
import ShoppingListItem from './ShoppingListItem.vue'
@@ -188,7 +188,7 @@ export default {
return itemsToGroups(this.shoppingList?.purchased_items ?? []);
},
purchasedMeals() {
- return Object.values(this.shoppingList?.meals_lookup ?? {}).filter(m => m.purchase_date).sort((a, b) => a.suggested_date - b.suggested_date);
+ return uniqueMeals(this.shoppingList?.purchased_items ?? []);
},
availableMeals() {
const meals = { ...this.shoppingList?.meals_lookup ?? {} };
diff --git a/src/components/shopping/PurchasedShoppingListPage.vue b/src/components/shopping/PurchasedShoppingListPage.vue
index 933e075..d74a0ee 100644
--- a/src/components/shopping/PurchasedShoppingListPage.vue
+++ b/src/components/shopping/PurchasedShoppingListPage.vue
@@ -34,7 +34,7 @@
import { ago } from '@/dateformats.js'
import data from '@/data.js'
-import { itemsToGroups } from './shopping.js'
+import { itemsToGroups, uniqueMeals } from './shopping.js'
import MealSelectionList from './MealSelectionList.vue'
import ShoppingListItem from './ShoppingListItem.vue'
@@ -48,16 +48,7 @@ export default {
computed: {
includedMeals() {
if (!this.shoppingList) return [];
-
- const seenMeals = new Set();
- return this.shoppingList.items
- .map(item => item.meal)
- .filter(meal => {
- if (!meal) return false;
- if (seenMeals.has(meal.id)) return false;
- seenMeals.add(meal.id);
- return true;
- });
+ return uniqueMeals(this.shoppingList.items);
},
listByProduct() {
return this.shoppingList ? itemsToGroups(this.shoppingList.items) : [];
diff --git a/src/components/shopping/shopping.js b/src/components/shopping/shopping.js
index 408338e..f55731e 100644
--- a/src/components/shopping/shopping.js
+++ b/src/components/shopping/shopping.js
@@ -2,6 +2,16 @@ export function groupsToItems(groups) {
return groups.map(group => group.shoppingListItems).flat();
}
+export function uniqueMeals(shoppingListItems) {
+ const mealsWithDuplicates = shoppingListItems
+ .map(item => item.meal)
+ .filter(m => m);
+
+ const mealsLookup = mealsWithDuplicates.reduce(((acc, meal) => { acc[meal.id] ??= meal; return acc; }), {});
+
+ return Object.values(mealsLookup);
+}
+
export function itemsToGroups(shoppingListItems) {
const ingredients_by_product_id = {};
const ingredients_by_name = {};