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

115 lines
4 KiB
Vue
Raw Normal View History

2024-05-18 07:05:28 +00:00
<template>
<h3>Full shopping list</h3>
2024-05-19 12:19:51 +00:00
2024-05-28 11:09:18 +00:00
<input type="checkbox" id="show-found" @change="onCheckboxChange" :checked="stockTaking" />
2024-05-19 12:19:51 +00:00
<label for="show-found">Show Stocktaking</label>
2024-05-18 07:05:28 +00:00
<h4>Included Meals</h4>
<meal-selection-list @meal-selected="mealSelected" @meal-unselected="mealUnselected" :checked="includedMeals" :meals="availableMeals" />
<ul class="full-shopping-list">
2024-05-19 12:19:51 +00:00
<li v-for="item in productsToShow" :key="item.id">
2024-05-19 10:21:49 +00:00
<shopping-list-item :product="item.product" :sources="item.sources" :totals="item.totals" :fully-found="item.fullyFound" :found-date="item.foundDate" />
2024-05-19 12:19:51 +00:00
<span v-if="stockTaking">
<button v-if="item.fullyFound" @click="markNotFound(item)">Mark Not Found</button>
<button v-else @click="markFound(item)">Mark Found</button>
</span>
2024-05-18 07:05:28 +00:00
</li>
</ul>
2024-05-20 12:16:39 +00:00
<button @click="markPurchased">Purchased, archive this list!</button>
2024-05-18 07:05:28 +00:00
</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'
2024-05-19 10:21:49 +00:00
import { toProductsModel, groupingToIngredients } from './shopping.js'
2024-05-18 07:05:28 +00:00
import MealSelectionList from './MealSelectionList.vue'
import ShoppingListItem from './ShoppingListItem.vue'
export default {
name: 'FullShoppingListPage',
components: { MealSelectionList, ShoppingListItem },
2024-05-28 11:09:18 +00:00
props: {
stockTaking: {
type: Boolean,
default: false
},
},
2024-05-18 07:05:28 +00:00
data() {
const from = new Date();
from.setTime(0);
const to = new Date();
to.setDate(to.getDate() + 7);
2024-05-28 11:09:18 +00:00
return { from, to, availableMeals: [], shoppingList: null, includedMeals: [], listByProduct: [], productsToShow: []}
2024-05-18 07:05:28 +00:00
},
async beforeMount() {
2024-05-27 11:56:58 +00:00
const allMeals = await data.getUpcomingMeals(this.from, this.to);
2024-05-20 12:16:39 +00:00
this.availableMeals = allMeals.filter(m => !m.purchase_date);
2024-05-18 07:05:28 +00:00
this.shoppingList = await data.getCurrentShoppingList();
},
watch: {
shoppingList: {
handler: 'updateLists',
deep: true
2024-05-19 12:19:51 +00:00
},
stockTaking: 'updateLists'
2024-05-18 07:05:28 +00:00
},
methods: {
async updateLists() {
if (!this.shoppingList)
return;
this.includedMeals = this.shoppingList.requests.map(r => r.meal).filter(m => m);
2024-05-19 10:21:49 +00:00
this.listByProduct = toProductsModel(this.shoppingList);
2024-05-19 12:19:51 +00:00
this.productsToShow = this.stockTaking ? this.listByProduct : this.listByProduct.filter(p => !p.fullyFound);
2024-05-18 07:05:28 +00:00
},
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);
},
async markFound(productGrouping) {
const result = await data.markFound(groupingToIngredients(productGrouping));
// Update shoppingList.result from result.created and result.removed
this.shoppingList.results = this.shoppingList.results.filter(r => !result.removed.some(removed => removed.id === r.id));
this.shoppingList.results.push(...result.created);
},
async markNotFound(item) {
const deletedRequests = await data.markNotFound(item.product.id);
this.shoppingList.results = this.shoppingList.results.filter(r => !deletedRequests.some(deleted => deleted.id === r.id));
2024-05-20 12:16:39 +00:00
},
async markPurchased() {
const result = await data.markCurrentShoppingListPurchased();
this.$router.push({ name: 'PurchasedShoppingListPage', params: { id: result.id } });
2024-05-28 11:09:18 +00:00
},
onCheckboxChange(event) {
this.$router.push({ query: { stockTaking: event.target.checked ? 'true' : undefined } });
2024-05-18 07:05:28 +00:00
}
}
}
</script>