fixes duplicate recipes in meal-plan #221

This commit is contained in:
hay-kot 2021-03-31 21:03:40 -08:00
commit 2206034b7e

View file

@ -101,6 +101,7 @@ export default {
endDate: null, endDate: null,
menu1: false, menu1: false,
menu2: false, menu2: false,
usedRecipes: [1],
}; };
}, },
@ -151,6 +152,10 @@ export default {
endComputedDateFormatted() { endComputedDateFormatted() {
return this.formatDate(this.endDate); return this.formatDate(this.endDate);
}, },
filteredRecipes() {
const recipes = this.items.filter(x => !this.usedRecipes.includes(x));
return recipes.length > 0 ? recipes : this.items;
},
}, },
methods: { methods: {
@ -170,15 +175,21 @@ export default {
this.items = await api.recipes.allByKeys(keys); this.items = await api.recipes.allByKeys(keys);
} }
}, },
get_random(list) { getRandom(list) {
const object = list[Math.floor(Math.random() * list.length)]; let recipe = 1;
return object; while (this.usedRecipes.includes(recipe)) {
recipe = list[Math.floor(Math.random() * list.length)];
}
return recipe;
}, },
random() { random() {
this.usedRecipes = [1];
this.meals.forEach((element, index) => { 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]["slug"] = recipe.slug;
this.meals[index]["name"] = recipe.name; this.meals[index]["name"] = recipe.name;
this.usedRecipes.push(recipe);
console.log(this.usedRecipes, recipe);
}); });
}, },
processTime(index) { processTime(index) {