munch-ease-frontend/src/components/shopping/FullShoppingListPage.vue

82 lines
2.2 KiB
Vue
Raw Normal View History

2024-05-18 07:05:28 +00:00
<template>
<h3>Full shopping list</h3>
<h4>Included Meals</h4>
<meal-selection-list @meal-selected="mealSelected" @meal-unselected="mealUnselected" :checked="includedMeals" :meals="availableMeals" />
<ul class="full-shopping-list">
<li v-for="item in listByProduct" :key="item.id">
<shopping-list-item :product="item.product" :sources="item.sources" />
</li>
</ul>
</template>
<style scoped>
.full-shopping-list li {
list-style-type: none;
border: 1px solid #ccc;
border-radius: 0.5em;
margin-bottom: 1em;
}
.full-shopping-list {
padding: 0;
}
</style>
<script>
import data from '@/data.js'
import { requestsToSources, groupByProduct } from './shopping.js'
import MealSelectionList from './MealSelectionList.vue'
import ShoppingListItem from './ShoppingListItem.vue'
export default {
name: 'FullShoppingListPage',
components: { MealSelectionList, ShoppingListItem },
props: {
id: [String, Number]
},
data() {
const from = new Date();
from.setTime(0);
const to = new Date();
to.setDate(to.getDate() + 7);
return { from, to, availableMeals: [], shoppingList: null, includedMeals: [], listByProduct: []}
},
async beforeMount() {
this.availableMeals = await data.getMeals(this.from, this.to);
this.shoppingList = await data.getCurrentShoppingList();
},
watch: {
shoppingList: {
handler: 'updateLists',
deep: true
}
},
methods: {
async updateLists() {
if (!this.shoppingList)
return;
this.includedMeals = this.shoppingList.requests.map(r => r.meal).filter(m => m);
const sources = requestsToSources(this.shoppingList.requests);
this.listByProduct = groupByProduct(sources);
},
async mealSelected(meal) {
const request = await data.requestMeal(meal.id);
this.shoppingList.requests.push(request);
},
async mealUnselected(meal) {
await data.unrequestMeal(meal.id);
this.shoppingList.requests = this.shoppingList.requests.filter(r => r.meal?.id !== meal.id);
}
}
}
</script>