centralize all routes
This commit is contained in:
parent
5726bdb154
commit
a10f3e37c2
5 changed files with 53 additions and 24 deletions
|
|
@ -2,6 +2,7 @@ import { BrowserRouter as Router, Routes, Route } from "react-router-dom"
|
||||||
import RecipeDetailPage from "./components/recipes/RecipeDetailPage"
|
import RecipeDetailPage from "./components/recipes/RecipeDetailPage"
|
||||||
import RecipeEditPage from "./components/recipes/RecipeEditPage"
|
import RecipeEditPage from "./components/recipes/RecipeEditPage"
|
||||||
import RecipeListPage from "./components/recipes/RecipeListPage"
|
import RecipeListPage from "./components/recipes/RecipeListPage"
|
||||||
|
import { getRecipeAddUrlDefinition, getRecipeDetailsUrlDefinition, getRecipeEditUrlDefinition, getRootUrlDefinition } from "./routes"
|
||||||
|
|
||||||
import "./App.css"
|
import "./App.css"
|
||||||
|
|
||||||
|
|
@ -14,13 +15,16 @@ function App() {
|
||||||
<Router>
|
<Router>
|
||||||
<Routes>
|
<Routes>
|
||||||
{/* Home page: list of recipes */}
|
{/* Home page: list of recipes */}
|
||||||
<Route path="/" element={<RecipeListPage />} />
|
<Route path= {getRootUrlDefinition()} element={<RecipeListPage />} />
|
||||||
|
|
||||||
{/* Detail page: shows one recipe */}
|
{/* Detail page: shows one recipe */}
|
||||||
<Route path="/recipe/:id" element={<RecipeDetailPage />} />
|
<Route path={getRecipeDetailsUrlDefinition()} element={<RecipeDetailPage />} />
|
||||||
|
|
||||||
{/* Edit page: form to edit a recipe */}
|
{/* Edit page: form to edit a recipe */}
|
||||||
<Route path="/recipe/:id/edit" element={<RecipeEditPage />} />
|
<Route path={getRecipeEditUrlDefinition()} element={<RecipeEditPage />} />
|
||||||
|
|
||||||
|
{/* Add page: form to add a recipe */}
|
||||||
|
<Route path={getRecipeAddUrlDefinition()} element={<RecipeEditPage />} />
|
||||||
</Routes>
|
</Routes>
|
||||||
</Router>
|
</Router>
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ import { useParams, Link } from "react-router-dom"
|
||||||
import type { Recipe } from "../../types/recipe"
|
import type { Recipe } from "../../types/recipe"
|
||||||
import { useEffect, useState } from "react"
|
import { useEffect, useState } from "react"
|
||||||
import { fetchRecipe } from "../../api/recipePoint"
|
import { fetchRecipe } from "../../api/recipePoint"
|
||||||
|
import { getRecipeEditUrl, getRecipeListUrl } from "../../routes"
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -123,13 +124,13 @@ export default function RecipeDetailPage() {
|
||||||
{/* Action buttons */}
|
{/* Action buttons */}
|
||||||
<div className="button-group">
|
<div className="button-group">
|
||||||
<Link
|
<Link
|
||||||
to={`/recipe/${recipe.id}/edit`}
|
to={ getRecipeEditUrl(recipe.id) }
|
||||||
className="primary-button"
|
className="primary-button"
|
||||||
>
|
>
|
||||||
Bearbeiten
|
Bearbeiten
|
||||||
</Link>
|
</Link>
|
||||||
<Link
|
<Link
|
||||||
to="/"
|
to={getRecipeListUrl()}
|
||||||
className="default-button"
|
className="default-button"
|
||||||
>
|
>
|
||||||
Zurueck
|
Zurueck
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ 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 { fetchRecipe, createRecipe, updateRecipe } from "../../api/recipePoint"
|
import { fetchRecipe, createRecipe, updateRecipe } from "../../api/recipePoint"
|
||||||
|
import { getRecipeDetailUrl, getRecipeListUrl, getRootUrl } from "../../routes"
|
||||||
|
|
||||||
export default function RecipeEditPage() {
|
export default function RecipeEditPage() {
|
||||||
// Extract recipe ID from route params
|
// Extract recipe ID from route params
|
||||||
|
|
@ -62,10 +63,10 @@ export default function RecipeEditPage() {
|
||||||
const navigateBack = () => {
|
const navigateBack = () => {
|
||||||
if(recipe && recipe.id){
|
if(recipe && recipe.id){
|
||||||
console.log("navigating to recipe with id", recipe.id)
|
console.log("navigating to recipe with id", recipe.id)
|
||||||
navigate("/recipe/" + recipe.id) // go back to detail view
|
navigate(getRecipeDetailUrl(recipe.id)) // go back to detail view
|
||||||
} else {
|
} else {
|
||||||
console.log("navigating back to list as no recipe was selected")
|
console.log("navigating back to list as no recipe was selected")
|
||||||
navigate("/") // no recipe -> go back to list
|
navigate(getRecipeListUrl()) // no recipe -> go back to list
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// error handling -> if there is no recipe, we cannot open edit view
|
// error handling -> if there is no recipe, we cannot open edit view
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@ import { useEffect, useState } from "react"
|
||||||
import RecipeListItem from "./RecipeListItem"
|
import RecipeListItem from "./RecipeListItem"
|
||||||
import type { Recipe } from "../../types/recipe"
|
import type { Recipe } from "../../types/recipe"
|
||||||
import { fetchRecipeList } from "../../api/recipePoint"
|
import { fetchRecipeList } from "../../api/recipePoint"
|
||||||
|
import { useNavigate } from "react-router-dom"
|
||||||
|
import { getRecipeAddUrl, getRecipeAddUrlDefinition, getRecipeDetailUrl } from "../../routes"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Displays a list of recipes in a sidebar layout.
|
* Displays a list of recipes in a sidebar layout.
|
||||||
|
|
@ -9,22 +11,26 @@ import { fetchRecipeList } from "../../api/recipePoint"
|
||||||
*/
|
*/
|
||||||
export default function RecipeListPage() {
|
export default function RecipeListPage() {
|
||||||
|
|
||||||
const [recipeList, setRecipeList] = useState<Recipe[]|null>(null)
|
const navigate = useNavigate()
|
||||||
// load recipes once on render
|
const [recipeList, setRecipeList] = useState<Recipe[]|null>(null)
|
||||||
useEffect(() => {
|
// load recipes once on render
|
||||||
const loadRecipeList = async () => {
|
useEffect(() => {
|
||||||
try {
|
const loadRecipeList = async () => {
|
||||||
// Fetch recipe list
|
try {
|
||||||
console.log("loading recipe list")
|
// Fetch recipe list
|
||||||
const data = await fetchRecipeList()
|
console.log("loading recipe list")
|
||||||
setRecipeList(data)
|
const data = await fetchRecipeList()
|
||||||
} catch (err) {
|
setRecipeList(data)
|
||||||
console.error(err)
|
} catch (err) {
|
||||||
}
|
console.error(err)
|
||||||
}
|
}
|
||||||
loadRecipeList()
|
}
|
||||||
}, [])
|
loadRecipeList()
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
const handleAdd = () => {
|
||||||
|
navigate(getRecipeAddUrl())
|
||||||
|
}
|
||||||
if(!recipeList) { return <div>Unable to load recipes!</div>}
|
if(!recipeList) { return <div>Unable to load recipes!</div>}
|
||||||
return (
|
return (
|
||||||
/*Contaier spanning entire screen used to center content horizontally */
|
/*Contaier spanning entire screen used to center content horizontally */
|
||||||
|
|
@ -39,7 +45,9 @@ export default function RecipeListPage() {
|
||||||
<input className="input-field"
|
<input className="input-field"
|
||||||
placeholder="Search"
|
placeholder="Search"
|
||||||
></input>
|
></input>
|
||||||
<button className="primary-button">
|
<button className="primary-button"
|
||||||
|
onClick={handleAdd}
|
||||||
|
>
|
||||||
Add recipe
|
Add recipe
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -50,7 +58,7 @@ export default function RecipeListPage() {
|
||||||
<RecipeListItem
|
<RecipeListItem
|
||||||
key={recipe.id}
|
key={recipe.id}
|
||||||
title = {recipe.title}
|
title = {recipe.title}
|
||||||
targetPath={'recipe/'+recipe.id}
|
targetPath={getRecipeDetailUrl(recipe.id)}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
15
frontend/src/routes.ts
Normal file
15
frontend/src/routes.ts
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
/**
|
||||||
|
* Routes for all pages
|
||||||
|
*/
|
||||||
|
// Route definitions using :id as placeholder for the id
|
||||||
|
export function getRootUrlDefinition() : string { return getRootUrl()}
|
||||||
|
export function getRecipeDetailsUrlDefinition() : string {return getRecipeDetailUrl(":id")}
|
||||||
|
export function getRecipeEditUrlDefinition() : string {return getRecipeEditUrl(":id")}
|
||||||
|
export function getRecipeAddUrlDefinition() : string {return getRecipeAddUrl()}
|
||||||
|
|
||||||
|
// URLs including id
|
||||||
|
export function getRootUrl () : string { return "/"}
|
||||||
|
export function getRecipeListUrl() : string {return getRootUrl()}
|
||||||
|
export function getRecipeDetailUrl(id: string) : string {return "/recipe/" + id}
|
||||||
|
export function getRecipeEditUrl(id: string) : string {return "/recipe/" + id + "/edit"}
|
||||||
|
export function getRecipeAddUrl() : string {return "/new-recipe"}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue