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

176 lines
4.9 KiB
Vue
Raw Normal View History

2024-01-13 02:00:15 +00:00
<template>
<div>
2024-01-13 08:43:15 +00:00
<div v-if="!id && !recipe">
2024-01-13 02:00:15 +00:00
<input class="recipe-link" type="text" v-model="link" placeholder="Link to Recipe" />
<button @click="parseLink">Parse</button>
</div>
2024-01-13 08:43:15 +00:00
2024-01-13 03:22:10 +00:00
<div v-if="parse_failed">
2024-01-13 02:00:15 +00:00
<p>Recipe not found</p>
</div>
2024-01-13 03:22:10 +00:00
<div v-if="!parse_failed && recipe">
2024-01-13 08:43:15 +00:00
<div class="image-container" v-if="image_styling" :style="image_styling" ></div>
2024-01-13 02:00:15 +00:00
<h1><input class="recipe-name" type="text" :value="recipe.name"></h1>
2024-01-13 08:43:15 +00:00
<h3 class="recipe-link"><a :href="recipe.link">View Recipe</a></h3>
<h2>Ingredients</h2>
2024-01-13 02:00:15 +00:00
<ul>
<li v-for="ingredient in recipe.ingredients" :key="ingredient.line">
2024-01-14 02:12:19 +00:00
<p class="ingredient-line">
<ingredient-line
:ingredient="ingredient"
@update-ingredient="updateIngredient"
@update-product-link="updateProduct"
@delete-item="deleteIngredient" />
</p>
<button @click="deleteIngredient(ingredient)">Delete</button>
2024-01-13 02:00:15 +00:00
</li>
</ul>
2024-01-13 03:22:10 +00:00
<button class="submit-btn" @click="saveRecipe">Save</button>
2024-01-13 02:00:15 +00:00
</div>
</div>
</template>
<style scoped>
input {
border: 0;
border-bottom: 1px solid #ccc;
font-size: large;
}
input.recipe-link {
width: 80%;
}
2024-01-13 08:43:15 +00:00
.image-container {
max-height: 20vh;
min-height: 20vh;
display: flex;
flex-direction: column;
}
2024-01-13 02:00:15 +00:00
input.recipe-name {
width: 100%;
2024-01-13 08:43:15 +00:00
font-weight: bold;
font-size: larger;
}
.recipe-link {
color: #0000EE;
text-decoration: none;
2024-01-13 02:00:15 +00:00
}
ul {
padding-right: 1em;
}
li {
list-style: none;
2024-01-14 02:12:19 +00:00
display: flex;
flex-direction: row;
justify-content: space-between;
border: 1px solid #ccc;
border-radius: 5px;
padding: 5px;
margin: 1vh;
}
.ingredient-line {
flex: 1;
margin: 0;
margin-right: 1em;
2024-01-13 02:00:15 +00:00
}
</style>
<script>
import data from '@/data.js'
import IngredientLine from './IngredientLine.vue'
export default {
props: {
id: { type: Number, optional: true }
},
components: {
IngredientLine
},
data() {
return {
2024-01-13 03:22:10 +00:00
link: this.$route.query.url ?? "",
parse_failed: false,
2024-01-13 02:00:15 +00:00
recipe: null,
2024-01-13 08:43:15 +00:00
chefs: [],
2024-01-13 02:00:15 +00:00
}
},
mounted() {
2024-01-13 03:22:10 +00:00
this.refreshRecipe();
},
2024-01-13 08:43:15 +00:00
computed: {
image_styling() {
if (this.recipe?.image_urls && this.recipe.image_urls[0]) {
const image = this.recipe.image_urls[0];
// linear-gradient(to bottom, rgba(255, 255, 255, 0.8) 0%, rgba(255, 255, 255, 0) 100%), url('https://i2.wp.com/www.downshiftology.com/wp-content/uploads/2019/04/steamed-broccoli-4.jpg') center/cover no-repeat;
return {background: `linear-gradient(to bottom, rgba(255, 255, 255, 0.8) 0%, rgba(255, 255, 255, 0) 100%), url('${image}') center/cover no-repeat` }
}
return null;
}
},
2024-01-13 03:22:10 +00:00
methods: {
parseLink() {
this.$router.push({ path: '/recipes/add', query: { url: this.link }})
this.refreshRecipe();
},
refreshRecipe() {
2024-01-13 07:00:55 +00:00
if (this.id) {
data.getRecipe(this.id).then((response) => {
this.recipe = response;
this.link = response.link;
});
return;
}
else if (this.link) {
data.parseRecipe(this.link).then((response) => {
if (!response)
{
this.parse_failed = true;
return;
}
this.parse_failed = false;
this.recipe = response;
});
}
else {
2024-01-13 03:22:10 +00:00
this.recipe = null;
}
2024-01-13 02:00:15 +00:00
},
updateProduct(ingredient, product_link) {
data.parseProduct(ingredient, product_link).then(product => {
ingredient.product = product
});
},
updateIngredient(ingredient, line) {
data.parseIngredients([line]).then(function(newIngredients) {
const newIngredient = newIngredients[0];
ingredient.line = line;
ingredient.name = newIngredient.name;
2024-01-13 03:22:10 +00:00
ingredient.quantity = newIngredient.quantity;
ingredient.unit = newIngredient.unit;
2024-01-13 02:00:15 +00:00
ingredient.preparation = newIngredient.preparation;
if (newIngredient.product) {
ingredient.product = newIngredient.product;
}
});
},
2024-01-13 06:36:02 +00:00
deleteIngredient(ingredient) {
this.recipe.ingredients = this.recipe.ingredients.filter(i => i != ingredient);
},
2024-01-13 03:22:10 +00:00
saveRecipe() {
data.saveRecipe(this.recipe).then((response) => {
this.$router.push(`/recipes/${response.id}`)
});
}
2024-01-13 02:00:15 +00:00
},
}
</script>