From 578174107e0381a3c2faa1084ac6a9fddb46303f Mon Sep 17 00:00:00 2001 From: jableader Date: Mon, 14 Oct 2024 11:07:18 +1100 Subject: [PATCH] Implement found behaviour --- .../shopping/CurrentShoppingListPage.vue | 44 ++++++++++++------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/src/components/shopping/CurrentShoppingListPage.vue b/src/components/shopping/CurrentShoppingListPage.vue index 105154c..66541a5 100644 --- a/src/components/shopping/CurrentShoppingListPage.vue +++ b/src/components/shopping/CurrentShoppingListPage.vue @@ -124,6 +124,22 @@ import { toProductsModel, getCompletedRequests, sourcesToRequests } from './shop import MealSelectionList from './MealSelectionList.vue' import ShoppingListItem from './ShoppingListItem.vue' +async function saveShoppingList(storeName, currentShoppingList, productGroupsPurchased) { + const results = productGroupsPurchased.map(item => item.requested.map(r => ({ + product_id: r.ingredient.product_id, + product: r.ingredient.product, + quantity: r.ingredient.quantity, + unit: r.ingredient.unit, + list_id: -1, + }))).flat(); + + const requestedSources = productGroupsPurchased.map(item => item.requested).flat(); + const completedRequests = getCompletedRequests(currentShoppingList, results); + const servicedRequests = sourcesToRequests(requestedSources); + + return await data.purchaseItems(storeName, servicedRequests, results, completedRequests); +} + export default { name: 'FullShoppingListPage', components: { MealSelectionList, ShoppingListItem }, @@ -143,9 +159,7 @@ export default { return { from, to, availableMeals: [], shoppingList: null, includedMeals: [], productGroupsToPurchase: [], purchasedGroups: [], selected: [], showPurchased: false } }, async beforeMount() { - const allMeals = await data.getUpcomingMeals(this.from, this.to); - this.availableMeals = allMeals.filter(m => !m.purchase_date); - this.shoppingList = await data.getCurrentShoppingList(); + this.loadData(); }, watch: { shoppingList: { @@ -154,6 +168,11 @@ export default { }, }, methods: { + async loadData() { + const allMeals = await data.getUpcomingMeals(this.from, this.to); + this.availableMeals = allMeals.filter(m => !m.purchase_date); + this.shoppingList = await data.getCurrentShoppingList(); + }, async updateLists() { if (!this.shoppingList) return; @@ -173,21 +192,14 @@ export default { this.shoppingList.requests = this.shoppingList.requests.filter(r => r.meal?.id !== meal.id); }, async markFound() { + await saveShoppingList('', this.shoppingList, this.selected); + + this.selected = []; + this.loadData(); }, async markPurchased() { - const results = this.selected.map(item => item.requested.map(r => ({ - product_id: r.ingredient.product_id, - product: r.ingredient.product, - quantity: r.ingredient.quantity, - unit: r.ingredient.unit, - list_id: -1, - }))).flat(); - - const requestedSources = this.selected.map(item => item.requested).flat(); - const completedRequests = getCompletedRequests(this.shoppingList, results); - const servicedRequests = sourcesToRequests(requestedSources); - - const shoppingList = await data.purchaseItems('', servicedRequests, results, completedRequests); + const shoppingList = await saveShoppingList('', this.shoppingList, this.selected); + this.selected = []; this.$router.push(`/shopping/${shoppingList.id}`); }, toggleSelect(item) {