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

147 lines
3.8 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">
<date-picker @date-selected="selectDate" :date="meal.date" />
</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>
<button @click="removeRecipe(recipe)">Remove</button>
</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-01-17 06:03:11 +00:00
<h2>Additional Ingredients</h2>
2024-01-17 11:31:45 +00:00
<ul v-if="meal.extra_ingredients && meal.extra_ingredients.length">
<li v-for="ingredient in meal.extra_ingredients" :key="ingredient" class="saved-ingredient">
2024-01-14 06:20:21 +00:00
<p>
2024-01-17 06:03:11 +00:00
<compact-parsed-ingredient :ingredient="ingredient" />
2024-01-14 06:20:21 +00:00
</p>
<button @click="deleteIngredient(ingredient)">Delete</button>
</li>
</ul>
<div v-else>
<p>Add some ingredients using the search box</p>
</div>
<ingredient-add-box @add-ingredient="addIngredient" />
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'
import RecipeSearchBox from './RecipeSearchBox.vue';
2024-01-14 01:46:45 +00:00
import RecipeCard from './RecipeCard.vue';
import DatePicker from './DatePicker.vue';
2024-01-14 02:12:19 +00:00
import IngredientAddBox from './IngredientAddBox.vue';
2024-01-17 06:03:11 +00:00
import CompactParsedIngredient from './CompactParsedIngredient.vue';
2024-01-13 23:09:15 +00:00
2024-01-13 02:00:15 +00:00
export default {
props: ['id'],
2024-01-17 06:03:11 +00:00
components: { RecipeSearchBox, DatePicker, RecipeCard, IngredientAddBox, CompactParsedIngredient },
2024-01-13 02:00:15 +00:00
data() {
return {
meal: {
2024-01-17 11:31:45 +00:00
id: 0,
2024-01-13 02:00:15 +00:00
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-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: {
selectRecipe(recipe) {
2024-01-14 06:20:21 +00:00
this.meal.recipes.push(recipe);
2024-01-14 01:46:45 +00:00
},
selectDate(date) {
this.meal.date = date;
},
removeRecipe(recipe) {
this.meal.recipes = this.meal.recipes.filter(r => r.id !== recipe.id);
2024-01-14 06:20:21 +00:00
},
addIngredient(ingredient) {
2024-01-17 11:31:45 +00:00
this.meal.extra_ingredients.push(ingredient);
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-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>
.container {
margin: 1em auto;
max-width: 1200px;
}
.fields {
width: 80%;
margin: 0 auto;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
2024-01-14 06:20:21 +00:00
.saved-ingredient {
display: flex;
justify-content: space-between;
align-items: center;
margin: 1vh 1em;
}
2024-01-14 01:46:45 +00:00
.saved-recipe {
display: flex;
justify-content: space-between;
align-items: center;
margin: 1vh 1em;
}
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 {
flex: 1;
margin: 0 1em;
}
</style>