move navigation behavior for editor to methods
This commit is contained in:
parent
0eec7cf58e
commit
5c8ddf96f2
2 changed files with 26 additions and 10 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
import { useParams, useNavigate } from "react-router-dom"
|
import { useParams, useNavigate } from "react-router-dom"
|
||||||
//import { useEffect, useState } from "react"
|
//import { useEffect, useState } from "react"
|
||||||
//import type { Recipe } from "../types/recipe"
|
import type { Recipe } from "../types/recipe"
|
||||||
import RecipeEditor from "./RecipeEditor"
|
import RecipeEditor from "./RecipeEditor"
|
||||||
import { recipes } from "../mock_data/recipes"
|
import { recipes } from "../mock_data/recipes"
|
||||||
|
|
||||||
|
|
@ -48,13 +48,29 @@ export default function RecipeEditView() {
|
||||||
}
|
}
|
||||||
if (!recipe) return <div>Loading...</div>
|
if (!recipe) return <div>Loading...</div>
|
||||||
*/
|
*/
|
||||||
const handleSave = () => {
|
const handleSave = (updatedRecipe : Recipe) => {
|
||||||
console.log("Saving")
|
console.log("Saving", updatedRecipe.title)
|
||||||
navigate("/") // back to list
|
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 <div>Oops, there's no recipe in RecipeEditView</div>
|
if (!recipe) return <div>Oops, there's no recipe in RecipeEditView</div>
|
||||||
console.log("opening recipe in edit mode", recipe.title, id)
|
console.log("opening recipe in edit mode", recipe.title, id)
|
||||||
|
|
||||||
return <RecipeEditor recipe={recipe} onSave={handleSave} />
|
|
||||||
|
return <RecipeEditor recipe={recipe} onSave={handleSave} onCancel={handleCancel}/>
|
||||||
}
|
}
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
import { useState } from "react"
|
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 { Recipe } from "../types/recipe"
|
||||||
import type { Ingredient } from "../types/ingredient"
|
import type { Ingredient } from "../types/ingredient"
|
||||||
import {IngredientListEditor} from "./IngredientListEditor"
|
import {IngredientListEditor} from "./IngredientListEditor"
|
||||||
|
|
@ -7,13 +6,14 @@ import {IngredientListEditor} from "./IngredientListEditor"
|
||||||
type RecipeEditorProps = {
|
type RecipeEditorProps = {
|
||||||
recipe: Recipe
|
recipe: Recipe
|
||||||
onSave: (recipe: Recipe) => void
|
onSave: (recipe: Recipe) => void
|
||||||
|
onCancel: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Editor component for managing a recipe, including title,
|
* Editor component for managing a recipe, including title,
|
||||||
* ingredients (with amount, unit, name), instructions, and image URL.
|
* 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>(recipe)
|
const [draft, setDraft] = useState<Recipe>(recipe)
|
||||||
|
|
||||||
const updateIngredients = (ingredients: Ingredient[]) => {
|
const updateIngredients = (ingredients: Ingredient[]) => {
|
||||||
|
|
@ -58,12 +58,12 @@ export default function RecipeEditor({ recipe, onSave }: RecipeEditorProps) {
|
||||||
>
|
>
|
||||||
Save
|
Save
|
||||||
</button>
|
</button>
|
||||||
<Link
|
<button
|
||||||
to={`/recipe/${recipe.id}`}
|
|
||||||
className="default-button"
|
className="default-button"
|
||||||
|
onClick={() => onCancel()}
|
||||||
>
|
>
|
||||||
Cancel
|
Cancel
|
||||||
</Link>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue