munch-ease-frontend/src/components/meals/EditMealPage.vue

180 lines
5.1 KiB
Vue
Raw Normal View History

2024-01-13 02:00:15 +00:00
<template>
2024-01-14 01:46:45 +00:00
<div class="container">
<div class="fields">
2024-04-28 02:13:49 +00:00
<date-picker @date-selected="selectDate" :date="meal.meal_date" />
2024-05-04 08:00:50 +00:00
<div class="persons-list">
Cooked by <person-list :people="meal.chefs" @remove-person="(p) => removePerson('chefs', p)" @add-person="(p) => addPerson('chefs', p)" />
for <person-list :people="meal.consumers" @remove-person="(p) => removePerson('consumers', p)" @add-person="(p) => addPerson('consumers', p)" />,
with <person-list :people="meal.cleanup" @remove-person="(p) => removePerson('cleanup', p)" @add-person="(p) => addPerson('cleanup', p)" /> on cleanup.
</div>
2024-01-14 01:46:45 +00:00
</div>
2024-01-14 06:20:21 +00:00
<div class="recipes">
<h2>Recipes</h2>
<ul v-if="meal.recipes && meal.recipes.length">
<li v-for="recipe in meal.recipes" :key="recipe.id" class="saved-recipe">
<p class="recipe-card">
<recipe-card :recipe="recipe" />
</p>
2024-05-11 01:27:26 +00:00
<button @click="removeRecipe(recipe)">
<img class="icon" :src="require('@/assets/trash.svg')" />
</button>
2024-01-14 06:20:21 +00:00
</li>
</ul>
<div v-else>
<p>Add some recipes using the search box</p>
</div>
<div class="fields">
<recipe-search-box @select-recipe="selectRecipe" />
</div>
</div>
<div class="ingredients">
2024-05-04 04:22:37 +00:00
<h2>Sides & Additional Ingredients</h2>
2024-05-12 07:00:26 +00:00
<editable-ingredients-panel :ingredients="meal.extra_ingredients" @on-add="addIngredient" @on-delete="deleteIngredient" @on-update-ingredient="updateIngredient" />
2024-01-14 01:46:45 +00:00
</div>
2024-01-17 11:31:45 +00:00
<button @click="saveMeal">Save</button>
2024-01-13 23:09:15 +00:00
</div>
2024-01-13 02:00:15 +00:00
</template>
<script>
2024-01-13 23:09:15 +00:00
import data from '@/data.js'
2024-05-04 02:49:32 +00:00
import RecipeSearchBox from '@/components/recipes/RecipeSearchBox.vue';
import RecipeCard from '@/components/recipes/RecipeCard.vue';
2024-01-14 01:46:45 +00:00
import DatePicker from './DatePicker.vue';
2024-05-12 07:00:26 +00:00
import EditableIngredientsPanel from '../ingredients/EditableIngredientsPanel.vue';
2024-05-04 04:22:37 +00:00
import PersonList from './PersonList.vue';
2024-01-13 23:09:15 +00:00
2024-01-13 02:00:15 +00:00
export default {
props: ['id'],
2024-05-12 07:00:26 +00:00
components: { RecipeSearchBox, DatePicker, RecipeCard, EditableIngredientsPanel, PersonList },
2024-01-13 02:00:15 +00:00
data() {
return {
meal: {
2024-01-17 11:31:45 +00:00
id: 0,
2024-04-28 02:13:49 +00:00
meal_date: new Date(),
2024-01-14 06:20:21 +00:00
recipes: [],
2024-01-17 11:31:45 +00:00
extra_ingredients: [],
chefs: [],
consumers: [],
2024-04-28 02:13:49 +00:00
cleanup: []
2024-01-13 02:00:15 +00:00
}
2024-01-13 23:09:15 +00:00
};
},
2024-01-17 11:31:45 +00:00
async beforeMount() {
2024-01-13 23:09:15 +00:00
if (this.id) {
2024-01-17 11:31:45 +00:00
this.meal = await data.getMeal(this.id);
2024-01-13 02:00:15 +00:00
}
},
2024-01-13 23:09:15 +00:00
methods: {
2024-04-28 02:13:49 +00:00
async selectRecipe(recipe) {
// Refetch to get additional details
recipe = await data.getRecipe(recipe.id);
if (recipe.created_by) {
this.meal.chefs.push(recipe.created_by);
this.meal.consumers.push(recipe.created_by);
if (this.meal.cleanup.length === 0) {
this.meal.cleanup.push(recipe.created_by);
}
}
2024-01-14 06:20:21 +00:00
this.meal.recipes.push(recipe);
2024-01-14 01:46:45 +00:00
},
selectDate(date) {
2024-04-28 02:13:49 +00:00
this.meal.meal_date = date;
2024-01-14 01:46:45 +00:00
},
removeRecipe(recipe) {
this.meal.recipes = this.meal.recipes.filter(r => r.id !== recipe.id);
2024-01-14 06:20:21 +00:00
},
2024-05-12 07:00:26 +00:00
addIngredient() {
this.meal.extra_ingredients = [{ line: '', product: null }, ...this.meal.extra_ingredients];
2024-01-14 06:20:21 +00:00
},
deleteIngredient(ingredient) {
2024-01-17 11:31:45 +00:00
this.meal.extra_ingredients = this.meal.extra_ingredients.filter(i => i != ingredient);
2024-01-14 06:20:21 +00:00
},
2024-05-12 07:00:26 +00:00
updateIngredient(ingredient, newIngredient) {
this.meal.extra_ingredients = this.meal.extra_ingredients.map(i => i == ingredient ? newIngredient : i);
},
2024-05-04 04:22:37 +00:00
removePerson(list, person) {
this.meal[list] = this.meal[list].filter(p => p.id !== person.id);
},
addPerson(list, person) {
this.meal[list].push(person);
},
2024-01-17 11:31:45 +00:00
async saveMeal() {
const meal = await data.saveMeal(this.meal);
if (meal?.id) {
this.$router.push(`/meals/${meal.id}`);
return;
}
alert('Failed to save meal');
}
2024-01-13 23:09:15 +00:00
}
2024-01-13 02:00:15 +00:00
}
</script>
2024-01-14 01:46:45 +00:00
<style scoped>
2024-05-11 01:27:26 +00:00
img.icon {
width: 2em;
height: 2em;
}
2024-05-04 04:22:37 +00:00
.persons-list {
text-align: left;
2024-05-11 01:27:26 +00:00
padding: 1ex 2em;
2024-05-04 04:22:37 +00:00
}
.persons-list p {
margin: 0;
padding-bottom: 1em;
}
.person-list li {
display: inline-block;
padding-right: 1em;
}
2024-01-14 01:46:45 +00:00
.container {
margin: 1em auto;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
2024-01-17 13:34:42 +00:00
li {
padding-bottom: 0.5vh;
}
2024-01-14 06:20:21 +00:00
.saved-ingredient {
display: flex;
justify-content: space-between;
align-items: center;
}
2024-01-14 01:46:45 +00:00
.saved-recipe {
display: flex;
justify-content: space-between;
align-items: center;
}
2024-01-14 06:20:21 +00:00
.saved-ingredient p {
flex: 1;
margin: 0;
margin-right: 1em;
2024-01-14 01:46:45 +00:00
}
2024-01-14 06:20:21 +00:00
2024-01-14 01:46:45 +00:00
.saved-recipe button:hover {
background: #eee;
}
.saved-recipe .recipe-card {
2024-01-17 13:34:42 +00:00
margin: 0;
2024-01-14 01:46:45 +00:00
}
</style>