From da958faf33b1b2ab8c08fd2dc777215696de66a8 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Mon, 21 Sep 2020 22:05:53 +0200 Subject: [PATCH] basic shopping list ui cleanup --- cookbook/templates/shopping_list.html | 131 ++++++++++++++++++++------ 1 file changed, 101 insertions(+), 30 deletions(-) diff --git a/cookbook/templates/shopping_list.html b/cookbook/templates/shopping_list.html index 4ad0e887d..20ba7e6ae 100644 --- a/cookbook/templates/shopping_list.html +++ b/cookbook/templates/shopping_list.html @@ -22,32 +22,78 @@

{% trans 'Shopping List' %}

-
+ +
{% trans 'Edit' %}
@@ -136,12 +192,12 @@ delimiters: ['[[', ']]'], el: '#id_base_container', data: { - shopping_list_id: {{ shopping_list_id }}, + shopping_list_id: {% if shopping_list_id %}{{ shopping_list_id }}{% else %}null{% endif %}, + loading: true, edit_mode: true, recipe_query: '', recipes: [], shopping_list: undefined, - //multiplier_cache: {}, new_entry: { unit: undefined, amount: undefined, @@ -199,8 +255,6 @@ }, */ mounted: function () { - - if (this.shopping_list_id) { this.loadShoppingList() } else { @@ -212,9 +266,8 @@ "shared": [], "created_by": 1 } - + this.loading = false } - }, methods: { @@ -239,12 +292,14 @@ this.$http.get("{% url 'api:shoppinglist-detail' shopping_list_id %}").then((response) => { this.shopping_list = response.body + this.loading = false }).catch((err) => { console.log(err) this.makeToast('{% trans 'Error' %}', '{% trans 'There was an error loading a resource!' %}' + err.bodyText, 'danger') }) }, updateShoppingList: function () { + this.loading = true let recipe_promises = [] for (let i in this.shopping_list.recipes) { @@ -268,14 +323,30 @@ Promise.allSettled(recipe_promises).then(() => { console.log("proceeding to update shopping list", this.shopping_list) - this.$http.put("{% url 'api:shoppinglist-detail' shopping_list_id %}", this.shopping_list, {}).then((response) => { - console.log(response) - this.makeToast('{% trans 'Updated' %}', '{% trans 'Changes saved successfully!' %}', 'success') + if (this.shopping_list_id === null) { + this.$http.post("{% url 'api:shoppinglist-list' %}", this.shopping_list, {}).then((response) => { + console.log(response) + this.makeToast('{% trans 'Updated' %}', '{% trans 'Object created successfully!' %}', 'success') + this.loading = false + this.shopping_list = response.body + }).catch((err) => { + console.log(err) + this.makeToast('{% trans 'Error' %}', '{% trans 'There was an error creating a resource!' %}' + err.bodyText, 'danger') + this.loading = false + }) + } else { + this.$http.put("{% url 'api:shoppinglist-detail' shopping_list_id %}", this.shopping_list, {}).then((response) => { + console.log(response) + this.makeToast('{% trans 'Updated' %}', '{% trans 'Changes saved successfully!' %}', 'success') + this.loading = false + }).catch((err) => { + console.log(err) + this.makeToast('{% trans 'Error' %}', '{% trans 'There was an error updating a resource!' %}' + err.bodyText, 'danger') + this.loading = false + }) + } + - }).catch((err) => { - console.log(err) - this.makeToast('{% trans 'Error' %}', '{% trans 'There was an error updating a resource!' %}' + err.bodyText, 'danger') - }) }) }, addEntry: function () {