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

82 lines
2.6 KiB
Vue
Raw Normal View History

2024-05-11 06:03:29 +00:00
<template>
<div>
2024-05-18 03:49:43 +00:00
<h1>My Shopping List</h1>
2024-05-18 04:46:27 +00:00
<editable-ingredients-panel @on-add="addIngredient" @on-delete="deleteIngredient" @on-update-ingredient="updateIngredient" @on-editing="onEditing" :ingredients="ingredients" />
2024-05-11 06:03:29 +00:00
</div>
<!--
# Functions
* Add a random item to next shop
* Add meals to next shop
* Show cards for the next 7 meals
* Slider / drawer for more meals
* Check meal to add
* Update shopping list automatically as meals change
* Visual indicator for meals that are already in the list
* Visual indicator for purchased meals
* Aggregate items from meals and random items into a single list of products
* For each product, need to have visibility of
* Name
* Link
* Image
* Quantity
* Source meal / person
* Date added / for
* Need to be able to mark list as done
* Keeps history - track purchased meals
* Clears list
* History view of purchased lists (seperate page ofc)
-->
</template>
<style scoped>
</style>
<script>
2024-05-12 06:32:28 +00:00
import data from '@/data.js'
import EditableIngredientsPanel from '@/components/ingredients/EditableIngredientsPanel.vue'
2024-05-11 06:03:29 +00:00
export default {
2024-05-18 03:49:43 +00:00
name: 'MyShoppingpage',
2024-05-18 04:46:27 +00:00
components: { EditableIngredientsPanel },
2024-05-11 06:03:29 +00:00
data() {
2024-05-18 04:46:27 +00:00
return { ingredients: [], person: null }
2024-05-12 03:32:18 +00:00
},
2024-05-13 03:45:46 +00:00
async beforeMount() {
const person = await data.currentUser();
if (!person)
return this.$router.push({ name: 'login' });
this.person = person;
2024-05-18 04:46:27 +00:00
await this.updateShoppingList();
2024-05-13 03:45:46 +00:00
},
2024-05-12 03:32:18 +00:00
methods: {
2024-05-18 04:46:27 +00:00
async updateShoppingList(save = false) {
const requests = save ?
await data.saveMyShoppingList(this.ingredients) :
await data.getMyShoppingList();
2024-05-13 03:45:46 +00:00
2024-05-18 04:46:27 +00:00
this.ingredients = requests.map(r => r.ingredient);
2024-05-12 06:32:28 +00:00
},
2024-05-13 03:45:46 +00:00
addIngredient() {
2024-05-18 04:46:27 +00:00
this.ingredients = [{ id: 0 }, ...this.ingredients];
2024-05-12 06:32:28 +00:00
},
2024-05-13 03:45:46 +00:00
deleteIngredient(ingredient) {
2024-05-18 04:46:27 +00:00
this.ingredients = this.ingredients.filter(i => i !== ingredient);
2024-05-12 06:32:28 +00:00
},
2024-05-13 03:45:46 +00:00
updateIngredient(oldIngredient, newIngredient) {
2024-05-18 04:46:27 +00:00
this.ingredients = this.ingredients.map(source => source === oldIngredient ? newIngredient : source);
},
async onEditing(isStartingEdit) {
await this.updateShoppingList(!isStartingEdit);
2024-05-12 06:32:28 +00:00
2024-05-18 04:46:27 +00:00
if (isStartingEdit && this.ingredients.length === 0) {
this.addIngredient();
}
2024-05-11 06:03:29 +00:00
}
}
}
</script>