2024-05-18 07:05:28 +00:00
|
|
|
<template>
|
|
|
|
|
<h3>Full shopping list</h3>
|
2024-05-19 12:19:51 +00:00
|
|
|
|
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" />
|
2024-10-16 07:49:17 +00:00
|
|
|
|
2024-05-18 07:05:28 +00:00
|
|
|
<ul class="full-shopping-list">
|
2025-07-28 10:51:17 +00:00
|
|
|
<li v-for="group in outstandingItemGroups" :key="group.id" class="selectable" :class="{ 'selected': isSelected(group) }" @click="toggleSelect(group)">
|
|
|
|
|
<shopping-list-item :shopping-list-item-group="group" />
|
2024-05-18 07:05:28 +00:00
|
|
|
</li>
|
|
|
|
|
</ul>
|
2024-05-20 12:16:39 +00:00
|
|
|
|
2025-07-28 10:51:17 +00:00
|
|
|
<div v-if="outstandingItemGroups.length === 0">
|
2024-10-15 08:39:22 +00:00
|
|
|
<p>
|
|
|
|
|
No items to purchase
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
|
2024-10-13 08:19:36 +00:00
|
|
|
<div class="purchased-slider">
|
2025-07-28 10:51:17 +00:00
|
|
|
<span v-if="purchasedItemGroups.length === 0"></span>
|
2024-10-13 08:19:36 +00:00
|
|
|
<button v-else-if="showPurchased" @click="showPurchased=false" >⏶ Hide Purchased ⏶</button>
|
|
|
|
|
<button v-else @click="showPurchased=true">⏷ Show Purchased ⏷</button>
|
|
|
|
|
|
|
|
|
|
<div v-if="showPurchased">
|
2024-10-16 07:49:17 +00:00
|
|
|
<h4>Purchased Meals</h4>
|
|
|
|
|
<meal-selection-list @meal-selected="mealSelected" @meal-unselected="mealUnselected" :checked="includedMeals" :meals="purchasedMeals" />
|
|
|
|
|
|
2024-10-13 08:19:36 +00:00
|
|
|
<h4>
|
2024-10-16 07:49:17 +00:00
|
|
|
Purchased Items
|
2024-10-13 08:19:36 +00:00
|
|
|
</h4>
|
|
|
|
|
<ul class="full-shopping-list">
|
2025-07-28 10:51:17 +00:00
|
|
|
<li v-for="item in purchasedItemGroups" :key="item.id">
|
|
|
|
|
<shopping-list-item :shopping-list-item-group="item" />
|
2024-10-13 08:19:36 +00:00
|
|
|
</li>
|
|
|
|
|
</ul>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
2024-10-15 08:39:22 +00:00
|
|
|
<div class="spacer" v-if="selected.length">
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
2024-10-13 08:19:36 +00:00
|
|
|
<!-- Display 'Stocked', 'Purchased' and 'Cancel' buttons in a vertical stack fixed to the bottom of the screen when any elements are selected -->
|
|
|
|
|
<div class="footer-buttons" v-if="selected.length">
|
|
|
|
|
<p v-if="selected.length === 1">
|
2025-07-28 11:50:29 +00:00
|
|
|
Mark '{{ selected[0].product?.name ?? selected[0].name }}' as
|
2024-10-13 08:19:36 +00:00
|
|
|
</p>
|
|
|
|
|
<p v-else>
|
|
|
|
|
Mark {{ selected.length }} items as
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<div class="button-group">
|
|
|
|
|
<button @click="markFound">
|
|
|
|
|
<img src="@/assets/house-check.svg" /><br />
|
|
|
|
|
Found
|
|
|
|
|
</button>
|
|
|
|
|
|
|
|
|
|
<button @click="markPurchased">
|
|
|
|
|
<img src="@/assets/shopping-cart.svg" /><br />
|
|
|
|
|
Purchased
|
|
|
|
|
</button>
|
|
|
|
|
|
|
|
|
|
<button @click="selected = []">
|
|
|
|
|
<img src="@/assets/close.svg" /><br />
|
|
|
|
|
Cancel
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2024-05-18 07:05:28 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
|
2024-10-13 08:19:36 +00:00
|
|
|
.full-shopping-list {
|
|
|
|
|
padding: 0;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-18 07:05:28 +00:00
|
|
|
.full-shopping-list li {
|
|
|
|
|
list-style-type: none;
|
2024-10-13 08:19:36 +00:00
|
|
|
|
|
|
|
|
outline: 1px solid #ccc;
|
|
|
|
|
border-radius: 0.5em;
|
|
|
|
|
margin-bottom: 1em;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.full-shopping-list li.selectable {
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
outline: 1px solid #ccc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.full-shopping-list li.selected {
|
|
|
|
|
background-color: #f0f0f0;
|
|
|
|
|
outline-width: 3px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.footer-buttons {
|
|
|
|
|
position: fixed;
|
|
|
|
|
bottom: 0;
|
|
|
|
|
left: 0;
|
|
|
|
|
right: 0;
|
|
|
|
|
padding: 1ex;
|
|
|
|
|
background-color: #f0f0f0;
|
|
|
|
|
border-top: 1px solid #ccc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.footer-buttons p {
|
|
|
|
|
margin: 0;
|
|
|
|
|
text-align: left;
|
|
|
|
|
font-style: italic;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.button-group {
|
|
|
|
|
/* Display as vertical fixed to the bottom of the screen */
|
|
|
|
|
display: flex;
|
|
|
|
|
}
|
2024-05-18 07:05:28 +00:00
|
|
|
|
2024-10-13 08:19:36 +00:00
|
|
|
.button-group button {
|
|
|
|
|
flex: 1;
|
|
|
|
|
padding: 1em;
|
2024-05-18 07:05:28 +00:00
|
|
|
border: 1px solid #ccc;
|
|
|
|
|
border-radius: 0.5em;
|
2024-10-13 08:19:36 +00:00
|
|
|
background-color: #f0f0f0;
|
|
|
|
|
cursor: pointer;
|
2024-05-18 07:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-13 08:19:36 +00:00
|
|
|
button img {
|
|
|
|
|
width: 2em;
|
|
|
|
|
height: 2em;
|
2024-05-18 07:05:28 +00:00
|
|
|
}
|
|
|
|
|
|
2024-10-15 08:39:22 +00:00
|
|
|
.spacer {
|
|
|
|
|
height: 12em;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-18 07:05:28 +00:00
|
|
|
</style>
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
|
2025-07-27 01:58:57 +00:00
|
|
|
import alert from '@/alert.js'
|
|
|
|
|
|
2024-05-18 07:05:28 +00:00
|
|
|
import data from '@/data.js'
|
2025-07-28 10:51:17 +00:00
|
|
|
import { itemsToGroups, groupsToItems } from './shopping.js'
|
2024-05-18 07:05:28 +00:00
|
|
|
|
|
|
|
|
import MealSelectionList from './MealSelectionList.vue'
|
|
|
|
|
import ShoppingListItem from './ShoppingListItem.vue'
|
|
|
|
|
|
2025-07-28 10:51:17 +00:00
|
|
|
async function saveShoppingList(outstandingItemGroups) {
|
|
|
|
|
const items = groupsToItems(outstandingItemGroups);
|
|
|
|
|
if (items.length === 0) {
|
|
|
|
|
alert.show({ type: 'error', message: 'No items selected.' });
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-10-14 00:07:18 +00:00
|
|
|
|
2025-07-28 11:50:29 +00:00
|
|
|
return await data.purchaseShoppingList(items);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const groupsMatch = (a, b) => {
|
|
|
|
|
if (a.product != b.product) return false;
|
|
|
|
|
if (a.name && a.name === b.name) return true;
|
|
|
|
|
return a.product.id === b.product.id;
|
2024-10-14 00:07:18 +00:00
|
|
|
}
|
|
|
|
|
|
2024-05-18 07:05:28 +00:00
|
|
|
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);
|
|
|
|
|
|
2025-07-28 11:50:29 +00:00
|
|
|
return { from, to, shoppingList: null, selected: [], showPurchased: false }
|
2024-05-18 07:05:28 +00:00
|
|
|
},
|
|
|
|
|
async beforeMount() {
|
2024-10-14 00:07:18 +00:00
|
|
|
this.loadData();
|
2024-05-18 07:05:28 +00:00
|
|
|
},
|
2025-07-28 10:51:17 +00:00
|
|
|
computed: {
|
|
|
|
|
outstandingItemGroups() {
|
|
|
|
|
return itemsToGroups(this.shoppingList?.outstanding_items ?? []);
|
|
|
|
|
},
|
|
|
|
|
purchasedItemGroups() {
|
|
|
|
|
return itemsToGroups(this.shoppingList?.purchased_items ?? []);
|
|
|
|
|
},
|
|
|
|
|
purchasedMeals() {
|
|
|
|
|
return Object.values(this.shoppingList?.meals_lookup ?? {}).filter(m => m.purchase_date).sort((a, b) => a.suggested_date - b.suggested_date);
|
2024-05-19 12:19:51 +00:00
|
|
|
},
|
2025-07-28 10:51:17 +00:00
|
|
|
availableMeals() {
|
|
|
|
|
const meals = { ...this.shoppingList?.meals_lookup ?? {} };
|
|
|
|
|
this.upcomingMeals?.forEach(m => {
|
|
|
|
|
if (!meals[m.id]) {
|
|
|
|
|
meals[m.id] = m;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return Object.values(meals).filter(m => !m.purchase_date).sort((a, b) => a.suggested_date - b.suggested_date);
|
2025-07-28 11:50:29 +00:00
|
|
|
},
|
|
|
|
|
includedMeals() {
|
|
|
|
|
return this.shoppingList?.requested_meals.map(m => m.meal) ?? [];
|
2025-07-28 10:51:17 +00:00
|
|
|
}
|
2024-05-18 07:05:28 +00:00
|
|
|
},
|
|
|
|
|
methods: {
|
2024-10-14 00:07:18 +00:00
|
|
|
async loadData() {
|
2025-07-28 10:51:17 +00:00
|
|
|
this.upcomingMeals = await data.getUpcomingMeals(this.from, this.to);
|
2024-10-14 00:07:18 +00:00
|
|
|
this.shoppingList = await data.getCurrentShoppingList();
|
2024-05-18 07:05:28 +00:00
|
|
|
},
|
|
|
|
|
async mealSelected(meal) {
|
2025-07-28 10:51:17 +00:00
|
|
|
await data.requestMeal(meal.id);
|
|
|
|
|
await this.loadData();
|
2024-05-18 07:05:28 +00:00
|
|
|
},
|
|
|
|
|
async mealUnselected(meal) {
|
|
|
|
|
await data.unrequestMeal(meal.id);
|
2025-07-28 10:51:17 +00:00
|
|
|
await this.loadData();
|
2024-05-19 03:43:59 +00:00
|
|
|
},
|
2024-10-13 08:19:36 +00:00
|
|
|
async markFound() {
|
2025-07-27 01:58:57 +00:00
|
|
|
await saveShoppingList(this.selected);
|
2024-10-14 00:07:18 +00:00
|
|
|
|
|
|
|
|
this.selected = [];
|
2025-07-28 10:51:17 +00:00
|
|
|
await this.loadData();
|
2024-05-20 12:16:39 +00:00
|
|
|
},
|
|
|
|
|
async markPurchased() {
|
2025-07-27 01:58:57 +00:00
|
|
|
const shoppingList = await saveShoppingList(this.selected);
|
|
|
|
|
|
|
|
|
|
if (!shoppingList || !shoppingList.id) {
|
|
|
|
|
alert.show({ type: 'error', message: 'Failed to purchase.' });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-14 00:07:18 +00:00
|
|
|
this.selected = [];
|
2024-10-13 08:19:36 +00:00
|
|
|
this.$router.push(`/shopping/${shoppingList.id}`);
|
|
|
|
|
},
|
|
|
|
|
toggleSelect(item) {
|
2025-07-28 11:50:29 +00:00
|
|
|
const index = this.selected.findIndex(i => groupsMatch(i, item));
|
2024-10-13 08:19:36 +00:00
|
|
|
if (index === -1)
|
|
|
|
|
this.selected.push(item);
|
|
|
|
|
else
|
|
|
|
|
this.selected.splice(index, 1);
|
2024-05-28 11:09:18 +00:00
|
|
|
},
|
2024-10-13 08:19:36 +00:00
|
|
|
isSelected(item) {
|
2025-07-28 11:50:29 +00:00
|
|
|
return this.selected.some(i => groupsMatch(i, item));
|
2024-05-18 07:05:28 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
</script>
|