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

302 lines
9.2 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-05-25 02:09:27 +00:00
<date-picker @date-selected="selectDate" :date="meal.suggested_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">
2024-10-15 08:15:35 +00:00
<li v-for="mealRecipe in meal.recipes" :key="mealRecipe.recipe.id">
<div class="saved-recipe">
<p class="recipe-card">
<recipe-card :recipe="mealRecipe.recipe" />
</p>
<p class="servings">
<input type="number" v-model="mealRecipe.servings" min="1" />
<small><em>servings</em></small>
</p>
<input type="checkbox" class="show-ingredient-checkbox" :checked="showIngredient(mealRecipe)" />
<label for="show-ingredients" @click="showIngredient(mealRecipe, !showIngredient(mealRecipe))">
<img class="icon" :src="require('@/assets/show-ingredients.svg')" />
</label>
<button class="icon-button" @click="removeRecipe(mealRecipe)">
<img class="icon" :src="require('@/assets/trash.svg')" />
</button>
</div>
2024-10-15 08:15:35 +00:00
<div v-if="showIngredient(mealRecipe)">
<ul>
<li v-for="ingredient in scaleIngredients(mealRecipe)" :key="ingredient.id" class="saved-ingredient">
<CompactParsedIngredient :ingredient="ingredient" />
</li>
</ul>
</div>
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-27 12:11:57 +00:00
<editable-ingredients-panel :ingredients="meal.extra_ingredients" @on-add="addIngredient" @on-delete="deleteIngredient" @on-update-ingredient="updateIngredient" @on-editing="onEditAdditionalIngredients" />
2024-01-14 01:46:45 +00:00
</div>
<button @click="saveMeal">Save</button>
2024-10-15 08:40:06 +00:00
<p v-if="meal.purchase_date"><em>Purchased {{ ago(meal.purchase_date) }}</em></p>
2024-01-13 23:09:15 +00:00
</div>
2024-01-13 02:00:15 +00:00
</template>
<script>
2024-10-15 08:15:35 +00:00
2025-10-18 01:42:23 +00:00
import { getMeal, saveMeal } from '@/api/meals';
import { getRecipe } from '@/api/recipes';
import { currentUser } from '@/api/auth';
2024-09-21 22:02:21 +00:00
import alert from '@/alert.js';
2024-10-15 08:40:06 +00:00
import { ago } from '@/dateformats.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-10-15 08:15:35 +00:00
import CompactParsedIngredient from '../ingredients/CompactParsedIngredient.vue';
2024-01-13 23:09:15 +00:00
2024-05-19 12:40:32 +00:00
function addPersonIfNotExists(list, person) {
if (!list.find(p => p.id === person.id)) {
list.push(person);
}
}
2024-01-13 02:00:15 +00:00
export default {
props: ['id'],
2024-10-15 08:15:35 +00:00
components: { RecipeSearchBox, DatePicker, RecipeCard, EditableIngredientsPanel, PersonList, CompactParsedIngredient },
2024-01-13 02:00:15 +00:00
data() {
return {
2024-10-15 08:15:35 +00:00
showIngredients: {},
2024-01-13 02:00:15 +00:00
meal: {
2024-05-20 12:16:39 +00:00
id: -1,
2024-05-25 02:09:27 +00:00
suggested_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-05-20 12:16:39 +00:00
if (this.id >= 0) {
2025-10-18 01:42:23 +00:00
this.meal = await getMeal(this.id);
2024-01-13 02:00:15 +00:00
}
2024-05-27 12:11:57 +00:00
else {
2025-10-18 01:42:23 +00:00
const self = await currentUser();
2024-05-27 12:11:57 +00:00
this.meal = {...this.meal, chefs: [self], consumers: [self], cleanup: [self], };
}
2024-01-13 02:00:15 +00:00
},
2024-01-13 23:09:15 +00:00
methods: {
2024-10-15 08:40:06 +00:00
ago,
2024-04-28 02:13:49 +00:00
async selectRecipe(recipe) {
// Refetch to get additional details
2025-10-18 01:42:23 +00:00
recipe = await getRecipe(recipe.id);
2024-04-28 02:13:49 +00:00
if (recipe.created_by) {
2024-05-19 12:40:32 +00:00
addPersonIfNotExists(this.meal.chefs, recipe.created_by);
addPersonIfNotExists(this.meal.consumers, recipe.created_by);
2024-04-28 02:13:49 +00:00
if (this.meal.cleanup.length === 0) {
2024-05-19 12:40:32 +00:00
addPersonIfNotExists(this.meal.cleanup, recipe.created_by);
2024-04-28 02:13:49 +00:00
}
}
this.meal.recipes.push({ recipe, recipe_id: recipe.id, meal_id: this.meal.id, servings: recipe.serves });
2024-01-14 01:46:45 +00:00
},
selectDate(date) {
2024-05-25 02:09:27 +00:00
this.meal.suggested_date = date;
2024-01-14 01:46:45 +00:00
},
removeRecipe(mealRecipe) {
2024-10-15 08:23:00 +00:00
if (confirm(`Are you sure you want to remove ${mealRecipe.servings} servings of ${mealRecipe.recipe.name} from this meal?`)) {
this.meal.recipes = this.meal.recipes.filter(r => r != mealRecipe);
}
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) {
2024-05-19 12:40:32 +00:00
addPersonIfNotExists(this.meal[list], person);
2024-05-04 04:22:37 +00:00
},
2024-01-17 11:31:45 +00:00
async saveMeal() {
2025-10-18 01:42:23 +00:00
const meal = await saveMeal(this.meal);
2024-05-20 12:16:39 +00:00
if (meal?.id >= 0) {
2024-10-14 05:53:01 +00:00
this.meal = meal;
2024-01-17 11:31:45 +00:00
this.$router.push(`/meals/${meal.id}`);
2024-09-21 22:02:21 +00:00
alert.show({ heading: 'Meal saved', message: 'Meal saved successfully', type: 'success' });
2024-01-17 11:31:45 +00:00
return;
}
2024-09-21 22:02:21 +00:00
alert.show({ heading: 'Error saving meal', message: 'An error occurred while saving the meal', type: 'error' });
2024-05-27 12:11:57 +00:00
},
onEditAdditionalIngredients(editing) {
if (editing && this.meal.extra_ingredients.length === 0) {
this.addIngredient();
}
else {
this.meal.extra_ingredients = this.meal.extra_ingredients.filter(i => i.line);
}
2024-10-15 08:15:35 +00:00
},
showIngredient(mealRecipe, value) {
const index = this.meal.recipes.indexOf(mealRecipe);
const key = `${mealRecipe.recipe.id}-${index}`;
if (value === undefined) {
return this.showIngredients[key];
}
return this.showIngredients[key] = value;
},
scaleIngredients(mealRecipe) {
return mealRecipe.recipe.ingredients.map(i => {
return {
...i,
quantity: i.quantity * mealRecipe.servings / mealRecipe.recipe.serves
};
});
},
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
}
.recipe-card {
flex: 1;
}
.servings {
display: inline-block;
margin-right: 1em;
}
.servings input {
width: 3em;
border: none;
border-bottom: 1px solid #000;
text-align: center;
font-size: large;
font-style: italic;
}
2024-10-15 08:15:35 +00:00
.icon-button {
background: none;
border: none;
cursor: pointer;
border-radius: 1em;
padding: 0.5em;
}
2024-10-15 08:15:35 +00:00
.icon-button:hover {
/* Invert the colors of the trash icon */
filter: invert(1);
}
2024-10-15 08:15:35 +00:00
.show-ingredient-checkbox {
display: none;
}
.show-ingredient-checkbox + label {
cursor: pointer;
background-color: #fff;
padding: 0.5em;
border-radius: 1em;
}
.show-ingredient-checkbox + label:hover {
background-color: #eee;
}
.show-ingredient-checkbox:checked + label {
filter: invert(1);
}
2024-01-14 01:46:45 +00:00
</style>