From 5c8ddf96f2b023cdf49d2d25c5e0e8d0eb0c0766 Mon Sep 17 00:00:00 2001 From: Anika Raemer Date: Sun, 7 Sep 2025 10:33:15 +0200 Subject: [PATCH] move navigation behavior for editor to methods --- frontend/src/components/RecipeEditView.tsx | 26 +++++++++++++++++----- frontend/src/components/RecipeEditor.tsx | 10 ++++----- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/frontend/src/components/RecipeEditView.tsx b/frontend/src/components/RecipeEditView.tsx index 10a0918..d6bc00e 100644 --- a/frontend/src/components/RecipeEditView.tsx +++ b/frontend/src/components/RecipeEditView.tsx @@ -1,6 +1,6 @@ import { useParams, useNavigate } from "react-router-dom" //import { useEffect, useState } from "react" -//import type { Recipe } from "../types/recipe" +import type { Recipe } from "../types/recipe" import RecipeEditor from "./RecipeEditor" import { recipes } from "../mock_data/recipes" @@ -48,13 +48,29 @@ export default function RecipeEditView() { } if (!recipe) return
Loading...
*/ - const handleSave = () => { - console.log("Saving") - navigate("/") // back to list + const handleSave = (updatedRecipe : Recipe) => { + console.log("Saving", updatedRecipe.title) + navigateBack(); } + const handleCancel = () => { + console.log("Cancelling edit mode for", recipe ? recipe.id : "no recipe at all") + navigateBack(); + } + + const navigateBack = () => { + if(recipe && recipe.id){ + console.log("navigating to recipe with id", recipe.id) + navigate("/recipe/" + recipe.id) // go back to detail view + } else { + console.log("navigating back to list as no recipe was selected") + navigate("/") // no recipe -> go back to list + } + } + // error handling -> if there is no recipe, we cannot open edit view if (!recipe) return
Oops, there's no recipe in RecipeEditView
console.log("opening recipe in edit mode", recipe.title, id) - return + + return } \ No newline at end of file diff --git a/frontend/src/components/RecipeEditor.tsx b/frontend/src/components/RecipeEditor.tsx index 884ab90..20b3f0c 100644 --- a/frontend/src/components/RecipeEditor.tsx +++ b/frontend/src/components/RecipeEditor.tsx @@ -1,5 +1,4 @@ import { useState } from "react" -import { Link } from "react-router-dom" // @todo replace cancel link with button and onCancel handler import type { Recipe } from "../types/recipe" import type { Ingredient } from "../types/ingredient" import {IngredientListEditor} from "./IngredientListEditor" @@ -7,13 +6,14 @@ import {IngredientListEditor} from "./IngredientListEditor" type RecipeEditorProps = { recipe: Recipe onSave: (recipe: Recipe) => void + onCancel: () => void } /** * Editor component for managing a recipe, including title, * ingredients (with amount, unit, name), instructions, and image URL. */ -export default function RecipeEditor({ recipe, onSave }: RecipeEditorProps) { +export default function RecipeEditor({ recipe, onSave, onCancel }: RecipeEditorProps) { const [draft, setDraft] = useState(recipe) const updateIngredients = (ingredients: Ingredient[]) => { @@ -58,12 +58,12 @@ export default function RecipeEditor({ recipe, onSave }: RecipeEditorProps) { > Save - onCancel()} > Cancel - + )