From da61a2daa313f39782379c3e76110e5bb1aa75ab Mon Sep 17 00:00:00 2001 From: jableader Date: Mon, 27 May 2024 21:56:58 +1000 Subject: [PATCH] Mark meal as complete --- src/components/meals/MealPlanPage.vue | 7 ++++++- .../shopping/CurrentShoppingListPage.vue | 2 +- src/data.js | 17 ++++++++++++++--- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/components/meals/MealPlanPage.vue b/src/components/meals/MealPlanPage.vue index e728069..44e53c5 100644 --- a/src/components/meals/MealPlanPage.vue +++ b/src/components/meals/MealPlanPage.vue @@ -9,6 +9,7 @@ @@ -98,7 +99,7 @@ export default { } }, async beforeMount() { - const meals = await data.getMeals(this.from, this.to) + const meals = await data.getUpcomingMeals(this.from, this.to) this.meals = meals; }, methods: { @@ -106,6 +107,10 @@ export default { await data.deleteMeal(this.selectedMeal.id); this.meals = this.meals.filter(m => m.id !== this.selectedMeal.id); this.selectedMeal = null; + }, + async markConsumed() { + await data.markMealConsumed(this.selectedMeal.id); + this.meals = this.meals.filter(m => m.id !== this.selectedMeal.id); } } } diff --git a/src/components/shopping/CurrentShoppingListPage.vue b/src/components/shopping/CurrentShoppingListPage.vue index 41cec6f..8239e47 100644 --- a/src/components/shopping/CurrentShoppingListPage.vue +++ b/src/components/shopping/CurrentShoppingListPage.vue @@ -57,7 +57,7 @@ export default { return { from, to, availableMeals: [], shoppingList: null, includedMeals: [], listByProduct: [], stockTaking: false, productsToShow: []} }, async beforeMount() { - const allMeals = await data.getMeals(this.from, this.to); + const allMeals = await data.getUpcomingMeals(this.from, this.to); this.availableMeals = allMeals.filter(m => !m.purchase_date); this.shoppingList = await data.getCurrentShoppingList(); }, diff --git a/src/data.js b/src/data.js index 81a64a0..6307ced 100644 --- a/src/data.js +++ b/src/data.js @@ -1,7 +1,7 @@ const BASE_URL = window.location.href.replace(/^(http:\/\/[^/:]+).*/, "$1:8000") const datesToFix = { - Meal: { fields: [ "suggested_date", "purchase_date" ] }, + Meal: { fields: [ "suggested_date", "purchase_date", "consumed_date" ] }, ShoppingList: { fields: [ "created_date", "purchased_date" ], dependants: l => ({ ShoppingListResult: l.results, ShoppingListRequest: l.requests }) }, ShoppingListResult: { fields: [ "found_date", "created_date" ], }, ShoppingListRequest: { fields: [ "created_date" ], dependants: r => ({ Meal: r.meal }) }, @@ -43,8 +43,19 @@ const fixDates = (obj, type) => { let user = null; export default { - async getMeals(from, to) { - const response = await fetch(BASE_URL + "/meals?from=" + from.toISOString() + "&to=" + to.toISOString()); + async markMealConsumed(meal_id) { + const response = await fetch(BASE_URL + `/meals/${encodeURIComponent(meal_id)}/consumed`, { + method: "POST", + credentials: "include", + }); + + const meal = await response.json(); + fixDates(meal, "Meal"); + + return meal; + }, + async getUpcomingMeals(from, to) { + const response = await fetch(BASE_URL + "/meals/upcoming?from=" + from.toISOString() + "&to=" + to.toISOString()); const meals = await response.json(); fixDates(meals, "Meal");