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 MealSelectionList from './MealSelectionList.vue'
import ShoppingListItem from './ShoppingListItem.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 { export default {
name: 'FullShoppingListPage', name: 'FullShoppingListPage',
components: { MealSelectionList, ShoppingListItem }, components: { MealSelectionList, ShoppingListItem },
@ -143,9 +159,7 @@ export default {
return { from, to, availableMeals: [], shoppingList: null, includedMeals: [], productGroupsToPurchase: [], purchasedGroups: [], selected: [], showPurchased: false } return { from, to, availableMeals: [], shoppingList: null, includedMeals: [], productGroupsToPurchase: [], purchasedGroups: [], selected: [], showPurchased: false }
}, },
async beforeMount() { async beforeMount() {
const allMeals = await data.getUpcomingMeals(this.from, this.to); this.loadData();
this.availableMeals = allMeals.filter(m => !m.purchase_date);
this.shoppingList = await data.getCurrentShoppingList();
}, },
watch: { watch: {
shoppingList: { shoppingList: {
@ -154,6 +168,11 @@ export default {
}, },
}, },
methods: { 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() { async updateLists() {
if (!this.shoppingList) if (!this.shoppingList)
return; return;
@ -173,21 +192,14 @@ export default {
this.shoppingList.requests = this.shoppingList.requests.filter(r => r.meal?.id !== meal.id); this.shoppingList.requests = this.shoppingList.requests.filter(r => r.meal?.id !== meal.id);
}, },
async markFound() { async markFound() {
await saveShoppingList('', this.shoppingList, this.selected);
this.selected = [];
this.loadData();
}, },
async markPurchased() { async markPurchased() {
const results = this.selected.map(item => item.requested.map(r => ({ const shoppingList = await saveShoppingList('', this.shoppingList, this.selected);
product_id: r.ingredient.product_id, this.selected = [];
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);
this.$router.push(`/shopping/${shoppingList.id}`); this.$router.push(`/shopping/${shoppingList.id}`);
}, },
toggleSelect(item) { toggleSelect(item) {