From 2206034b7e7ec961569bc4500edd6f5b4798928a Mon Sep 17 00:00:00 2001 From: hay-kot Date: Wed, 31 Mar 2021 21:03:40 -0800 Subject: [PATCH] fixes duplicate recipes in meal-plan #221 --- .../src/components/MealPlan/MealPlanNew.vue | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/frontend/src/components/MealPlan/MealPlanNew.vue b/frontend/src/components/MealPlan/MealPlanNew.vue index 51d4326b3..89c0d73c7 100644 --- a/frontend/src/components/MealPlan/MealPlanNew.vue +++ b/frontend/src/components/MealPlan/MealPlanNew.vue @@ -101,6 +101,7 @@ export default { endDate: null, menu1: false, menu2: false, + usedRecipes: [1], }; }, @@ -151,6 +152,10 @@ export default { endComputedDateFormatted() { return this.formatDate(this.endDate); }, + filteredRecipes() { + const recipes = this.items.filter(x => !this.usedRecipes.includes(x)); + return recipes.length > 0 ? recipes : this.items; + }, }, methods: { @@ -170,15 +175,21 @@ export default { this.items = await api.recipes.allByKeys(keys); } }, - get_random(list) { - const object = list[Math.floor(Math.random() * list.length)]; - return object; + getRandom(list) { + let recipe = 1; + while (this.usedRecipes.includes(recipe)) { + recipe = list[Math.floor(Math.random() * list.length)]; + } + return recipe; }, random() { + this.usedRecipes = [1]; this.meals.forEach((element, index) => { - let recipe = this.get_random(this.items); + let recipe = this.getRandom(this.filteredRecipes); this.meals[index]["slug"] = recipe.slug; this.meals[index]["name"] = recipe.name; + this.usedRecipes.push(recipe); + console.log(this.usedRecipes, recipe); }); }, processTime(index) {