updating food working

This commit is contained in:
vabene1111
2021-01-26 17:56:43 +01:00
parent 96c4823664
commit 26ec1724a5
2 changed files with 49 additions and 16 deletions

View File

@ -173,7 +173,7 @@ class SupermarketSerializer(UniqueFieldsMixin, serializers.ModelSerializer):
class FoodSerializer(UniqueFieldsMixin, WritableNestedModelSerializer): class FoodSerializer(UniqueFieldsMixin, WritableNestedModelSerializer):
supermarket_category = SupermarketCategorySerializer(read_only=True) supermarket_category = SupermarketCategorySerializer(allow_null=True)
def create(self, validated_data): def create(self, validated_data):
# since multi select tags dont have id's # since multi select tags dont have id's

View File

@ -110,12 +110,15 @@
</div> </div>
<table class="table table-sm table-striped" style="margin-top: 1vh"> <table class="table table-sm table-striped" style="margin-top: 1vh">
<template v-for="c in display_categories">
<tbody> <tbody>
<template v-for="c in display_entries">
<tr> <tr>
<td colspan="4">[[c.name]]</td> <td colspan="4">[[c.name]]</td>
</tr> </tr>
</tbody>
<tbody is="draggable" :list="c.entries" tag="tbody" group="people" @sort="sortEntries"
@change="dragChanged(c, $event)">
<tr v-for="(element, index) in c.entries" :key="element.id"> <tr v-for="(element, index) in c.entries" :key="element.id">
<td class="handle"><i class="fas fa-sort"></i></td> <td class="handle"><i class="fas fa-sort"></i></td>
<td>[[element.amount]]</td> <td>[[element.amount]]</td>
@ -127,9 +130,8 @@
<i class="fa fa-trash"></i></button> <i class="fa fa-trash"></i></button>
</td> </td>
</tr> </tr>
</template>
</tbody> </tbody>
</template>
</table> </table>
@ -264,7 +266,7 @@
<div class="row" style="margin-top: 8px"> <div class="row" style="margin-top: 8px">
<div class="col col-md-12"> <div class="col col-md-12">
<table class="table"> <table class="table">
<template v-for="c in display_entries"> <template v-for="c in display_categories">
<template v-if="c.entries.filter(item => item.checked === false).length > 0"> <template v-if="c.entries.filter(item => item.checked === false).length > 0">
<tr> <tr>
<td colspan="4">[[c.name]]</td> <td colspan="4">[[c.name]]</td>
@ -284,7 +286,7 @@
<tr> <tr>
<td colspan="4"></td> <td colspan="4"></td>
</tr> </tr>
<template v-for="c in display_entries"> <template v-for="c in display_categories">
<tr v-for="x in c.entries" class="text-muted"> <tr v-for="x in c.entries" class="text-muted">
<template v-if="x.checked"> <template v-if="x.checked">
@ -339,7 +341,6 @@
</b-modal> </b-modal>
</template> </template>
@ -397,10 +398,11 @@
}) })
return cache return cache
}, },
display_entries() { display_categories() {
let categories = { let categories = {
no_category: { no_category: {
name: gettext('Uncategorized'), name: gettext('Uncategorized'),
id: -1,
entries: [] entries: []
} }
} }
@ -409,11 +411,22 @@
if (e.food.supermarket_category !== null) { if (e.food.supermarket_category !== null) {
categories[e.food.supermarket_category.id] = { categories[e.food.supermarket_category.id] = {
name: e.food.supermarket_category.name, name: e.food.supermarket_category.name,
id: e.food.supermarket_category.id,
entries: [] entries: []
}; };
} }
}) })
if (this.shopping_list.supermarket !== null) {
this.shopping_list.supermarket.category_to_supermarket.forEach(el => {
categories[el.category.id] = {
name: el.category.name,
id: el.category.id,
entries: []
};
})
}
this.shopping_list.entries.forEach(element => { this.shopping_list.entries.forEach(element => {
let item = {} let item = {}
Object.assign(item, element); Object.assign(item, element);
@ -440,7 +453,7 @@
}, },
export_text() { export_text() {
let text = '' let text = ''
for (let [i, c] of Object.entries(this.display_entries)) { for (let [i, c] of Object.entries(this.display_categories)) {
for (let e of c.entries.filter(item => item.checked === false)) { for (let e of c.entries.filter(item => item.checked === false)) {
text += `${this.export_text_prefix}${e.amount} ${e.unit.name} ${e.food.name} \n` text += `${this.export_text_prefix}${e.amount} ${e.unit.name} ${e.food.name} \n`
} }
@ -620,11 +633,31 @@
}) })
}, },
sortEntries: function () { sortEntries: function (a, b) {
this.display_entries.forEach((item, index) => { //TODO implement me (might be difficult because of computed drag changed stuff)
},
dragChanged: function (category, evt) {
if (evt.added !== undefined) {
console.log('element was added to new list', category, evt)
this.shopping_list.entries.forEach(entry => {
if (entry.id === evt.added.element.id) {
if (category.id === -1) {
entry.food.supermarket_category = null
} else {
entry.food.supermarket_category = {
name: category.name,
id: category.id
}
}
console.log('UPDATING FOOD OBJECT', entry.food)
this.$http.put(("{% url 'api:food-detail' 123456 %}").replace('123456', entry.food.id), entry.food).then((response) => {
}).catch((err) => {
this.makeToast(gettext('Error'), gettext('There was an error updating a resource!') + err.bodyText, 'danger')
}) })
console.log("IMPLEMENT ME", this.display_entries) }
})
}
}, },
entryChecked: function (entry) { entryChecked: function (entry) {
this.shopping_list.entries.forEach((item) => { this.shopping_list.entries.forEach((item) => {