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

164 lines
4.5 KiB
Vue
Raw Normal View History

2024-01-13 02:00:15 +00:00
<template>
2025-10-18 02:08:22 +00:00
<div>
2024-01-13 08:43:15 +00:00
<div v-if="!id && !recipe">
2025-10-18 02:08:22 +00:00
<input class="recipe-link" type="text" v-model="link" placeholder="Link to Recipe" /> <br />
<button @click="parseLink">Parse</button>
<button @click="createFromScratch">Create from Scratch</button>
2024-01-13 02:00:15 +00:00
</div>
2024-01-13 08:43:15 +00:00
2024-01-13 03:22:10 +00:00
<div v-if="parse_failed">
2025-10-18 02:08:22 +00:00
<p>Recipe not found</p>
2024-01-13 02:00:15 +00:00
</div>
2024-01-13 03:22:10 +00:00
<div v-if="!parse_failed && recipe">
2025-10-18 02:08:22 +00:00
<div class="image-container" v-if="image_styling" :style="image_styling"></div>
<h1><input class="recipe-name" type="text" v-model="recipe.name" /></h1>
<label for="recipe-serves">Number of serves: </label>
<input type="number" v-model="recipe.serves" />
<h3 class="recipe-link"><a :href="recipe.link">View Recipe</a></h3>
<h2>Ingredients</h2>
<editable-ingredients-panel
:ingredients="recipe.ingredients"
:edit-only="true"
@on-add="addIngredient"
@on-delete="deleteIngredient"
@on-update-ingredient="updateIngredient"
/>
2024-05-04 02:00:05 +00:00
2025-10-18 02:08:22 +00:00
<div>
<button v-if="recipe.id" class="delete-btn" @click="deleteRecipe">Delete</button>
<button class="submit-btn" @click="saveRecipe">{{ recipe.id ? 'Save' : 'Create' }}</button>
</div>
2024-01-13 02:00:15 +00:00
</div>
2025-10-18 02:08:22 +00:00
</div>
2024-01-13 02:00:15 +00:00
</template>
<style scoped>
input {
2025-10-18 02:08:22 +00:00
border: 0;
border-bottom: 1px solid #ccc;
font-size: large;
2024-01-13 02:00:15 +00:00
}
input.recipe-link {
2025-10-18 02:08:22 +00:00
width: 80%;
2024-01-13 02:00:15 +00:00
}
2024-01-13 08:43:15 +00:00
.image-container {
2025-10-18 02:08:22 +00:00
max-height: 20vh;
min-height: 20vh;
display: flex;
flex-direction: column;
2024-01-13 08:43:15 +00:00
}
2024-01-13 02:00:15 +00:00
input.recipe-name {
2025-10-18 02:08:22 +00:00
width: 100%;
font-weight: bold;
font-size: larger;
2024-01-13 08:43:15 +00:00
}
.recipe-link {
2025-10-18 02:08:22 +00:00
color: #0000ee;
text-decoration: none;
2024-01-13 02:00:15 +00:00
}
</style>
<script>
2024-09-22 02:58:25 +00:00
import alert from '@/alert.js'
2025-10-18 01:36:55 +00:00
import { getRecipe, parseRecipe, saveRecipe, deleteRecipe as deleteRecipeApi } from '@/api/recipes'
2024-05-12 07:00:26 +00:00
import EditableIngredientsPanel from '@/components/ingredients/EditableIngredientsPanel.vue'
2024-01-13 02:00:15 +00:00
export default {
2025-10-18 02:08:22 +00:00
props: {
id: { type: Number, optional: true },
},
components: { EditableIngredientsPanel },
data() {
return {
link: this.$route.query.url ?? '',
parse_failed: false,
recipe: null,
chefs: [],
}
},
mounted() {
this.refreshRecipe()
},
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;
2024-01-13 02:00:15 +00:00
return {
2025-10-18 02:08:22 +00:00
background: `linear-gradient(to bottom, rgba(255, 255, 255, 0.8) 0%, rgba(255, 255, 255, 0) 100%), url('${image}') center/cover no-repeat`,
2024-01-13 02:00:15 +00:00
}
2025-10-18 02:08:22 +00:00
}
return null
2024-01-13 02:00:15 +00:00
},
2025-10-18 02:08:22 +00:00
},
methods: {
parseLink() {
this.$router.push({ path: '/recipes/add', query: { url: this.link } })
this.refreshRecipe()
2024-01-13 03:22:10 +00:00
},
2025-10-18 02:08:22 +00:00
async refreshRecipe() {
if (this.id >= 0) {
this.recipe = await getRecipe(this.id)
this.link = this.recipe.link
return
} else if (this.link) {
this.recipe = await parseRecipe(this.link)
this.parse_failed = !this.recipe
} else {
this.recipe = null
}
},
async updateIngredient(ingredient, newIngredient) {
this.recipe.ingredients = this.recipe.ingredients.map((i) =>
i == ingredient ? newIngredient : i
)
2024-01-13 08:43:15 +00:00
},
2025-10-18 02:08:22 +00:00
deleteIngredient(ingredient) {
this.recipe.ingredients = this.recipe.ingredients.filter((i) => i != ingredient)
},
async saveRecipe() {
const recipe = await saveRecipe(this.recipe)
if (recipe?.id >= 0) {
alert.show({
heading: 'Recipe saved',
message: 'Your recipe has been saved',
type: 'success',
})
this.$router.push(`/recipes/${recipe.id}`)
return
}
2024-01-17 09:17:22 +00:00
2025-10-18 02:08:22 +00:00
alert.show({
heading: 'Error saving recipe',
message: 'There was an error saving your recipe',
type: 'error',
})
},
async createFromScratch() {
this.recipe = {
id: -1,
name: 'My new recipe',
created_by_id: -1,
link: '',
ingredients: [],
image_urls: [],
}
},
addIngredient() {
this.recipe.ingredients = [{ line: '', product: null }, ...this.recipe.ingredients]
},
async deleteRecipe() {
if (confirm('Are you sure you want to delete this recipe?')) {
await deleteRecipeApi(this.recipe.id)
this.$router.push('/recipes')
}
2024-01-13 02:00:15 +00:00
},
2025-10-18 02:08:22 +00:00
},
2024-01-13 02:00:15 +00:00
}
2025-10-18 02:08:22 +00:00
</script>