fixed shopping list multipliers and recipe names

This commit is contained in:
vabene1111
2020-09-21 11:56:39 +02:00
parent df79c8f889
commit f5117abcfb
2 changed files with 34 additions and 29 deletions

View File

@ -195,10 +195,11 @@ class MealPlanSerializer(serializers.ModelSerializer):
class ShoppingListRecipeSerializer(serializers.ModelSerializer): class ShoppingListRecipeSerializer(serializers.ModelSerializer):
recipe_name = serializers.ReadOnlyField(source='recipe.name')
class Meta: class Meta:
model = ShoppingListRecipe model = ShoppingListRecipe
fields = ('id', 'recipe', 'multiplier') fields = ('id', 'recipe', 'recipe_name', 'multiplier')
read_only_fields = ('id',) read_only_fields = ('id',)

View File

@ -44,8 +44,8 @@
<div class="col col-md-6"> <div class="col col-md-6">
<ul class="list-group" style="margin-top: 8px"> <ul class="list-group" style="margin-top: 8px">
<li class="list-group-item" v-for="x in shopping_list.recipes">[[x.recipe.name]] <input <li class="list-group-item" v-for="x in shopping_list.recipes">[[x.recipe_name]]
type="number" v-model="x.multiplier" @change="updateDisplay()"> <input type="number" v-model="x.multiplier">
</li> </li>
</ul> </ul>
</div> </div>
@ -104,7 +104,7 @@
<div class="row" style="margin-top: 8px"> <div class="row" style="margin-top: 8px">
<div class="col col-12"> <div class="col col-12">
<table class="table table-sm table-striped"> <table class="table table-sm table-striped">
<tr v-for="x in shopping_list.entries_display"> <tr v-for="x in display_entries">
<td>[[x.amount]]</td> <td>[[x.amount]]</td>
<td>[[x.unit.name]]</td> <td>[[x.unit.name]]</td>
<td>[[x.food.name]]</td> <td>[[x.food.name]]</td>
@ -141,7 +141,7 @@
recipe_query: '', recipe_query: '',
recipes: [], recipes: [],
shopping_list: undefined, shopping_list: undefined,
multiplier_cache: {}, //multiplier_cache: {},
new_entry: { new_entry: {
unit: undefined, unit: undefined,
amount: undefined, amount: undefined,
@ -159,6 +159,31 @@
} }
} }
}, },
computed: {
multiplier_cache() {
let cache = {}
this.shopping_list.recipes.forEach((r) => {
cache[r.id] = !(Number.isNaN(r.multiplier)) ? parseFloat(r.multiplier) : 1
})
return cache
},
display_entries() {
let entries = []
//TODO merge multiple ingredients of same unit
this.shopping_list.entries.forEach(element => {
let item = {}
Object.assign(item, element);
if (item.list_recipe !== null) {
item.amount = item.amount * this.multiplier_cache[item.list_recipe]
}
entries.push(item)
});
return entries
},
},
/* /*
watch: { watch: {
recipe: { recipe: {
@ -188,10 +213,10 @@
"created_by": 1 "created_by": 1
} }
this.updateDisplay()
} }
}, },
methods: { methods: {
/* /*
warnPageLeave: function (event) { warnPageLeave: function (event) {
@ -214,7 +239,6 @@
this.$http.get("{% url 'api:shoppinglist-detail' shopping_list_id %}").then((response) => { this.$http.get("{% url 'api:shoppinglist-detail' shopping_list_id %}").then((response) => {
this.shopping_list = response.body this.shopping_list = response.body
this.updateDisplay()
}).catch((err) => { }).catch((err) => {
console.log(err) console.log(err)
this.makeToast('{% trans 'Error' %}', '{% trans 'There was an error loading a resource!' %}' + err.bodyText, 'danger') this.makeToast('{% trans 'Error' %}', '{% trans 'There was an error loading a resource!' %}' + err.bodyText, 'danger')
@ -270,7 +294,7 @@
} }
this.$refs.new_entry_amount.focus(); this.$refs.new_entry_amount.focus();
this.updateDisplay()
}, },
getRecipes: function () { getRecipes: function () {
let url = "{% url 'api:recipe-list' %}?limit=5" let url = "{% url 'api:recipe-list' %}?limit=5"
@ -292,6 +316,7 @@
"created": true, "created": true,
"id": Math.random() * 1000, "id": Math.random() * 1000,
"recipe": recipe.id, "recipe": recipe.id,
"recipe_name": recipe.name,
"multiplier": 1 "multiplier": 1
} }
this.shopping_list.recipes.push(slr) this.shopping_list.recipes.push(slr)
@ -310,28 +335,7 @@
} }
} }
} }
this.updateDisplay()
},
updateMultiplierCache: function () {
this.multiplier_cache = {}
for (let r of this.shopping_list.recipes) {
this.multiplier_cache[r.id] = r.multiplier
}
},
updateDisplay: function () {
this.updateMultiplierCache()
//TODO merge multiple ingredients of same unit
this.shopping_list.entries_display = []
this.shopping_list.entries.forEach(function (element) {
let item = {}
Object.assign(item, element);
if (item.list_recipe !== null) {
item.amount = item.amount * app.multiplier_cache[item.list_recipe]
}
app.shopping_list.entries_display.push(item)
});
}, },
searchKeywords: function (query) { searchKeywords: function (query) {
this.keywords_loading = true this.keywords_loading = true