Implement found behaviour

This commit is contained in:
jableader 2024-10-14 11:07:18 +11:00
parent e8b71cafac
commit 578174107e

View file

@ -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) {