renamed models, added mapper for recipes
This commit is contained in:
parent
7a6f5b5bcd
commit
8027fce80d
21 changed files with 164 additions and 61 deletions
|
|
@ -1,14 +1,16 @@
|
|||
import { useParams, useNavigate } from "react-router-dom"
|
||||
import { useEffect, useState } from "react"
|
||||
import type { Recipe } from "../../types/recipe"
|
||||
import type { RecipeModel } from "../../models/RecipeModel"
|
||||
import RecipeEditor from "./RecipeEditor"
|
||||
import { fetchRecipe, createRecipe, updateRecipe } from "../../api/points/RecipePoint"
|
||||
import { getRecipeDetailUrl, getRecipeListUrl } from "../../routes"
|
||||
import { mapRecipeDtoToModel, mapRecipeModelToDto } from "../../mappers/recipeMapper"
|
||||
import type { RecipeDto } from "../../api/dtos/RecipeDto"
|
||||
|
||||
export default function RecipeEditPage() {
|
||||
// Extract recipe ID from route params
|
||||
const { id } = useParams<{ id: string }>()
|
||||
const [recipe, setRecipe] = useState<Recipe | null>(null)
|
||||
const [recipe, setRecipe] = useState<RecipeModel | null>(null)
|
||||
const navigate = useNavigate()
|
||||
|
||||
useEffect(() => {
|
||||
|
|
@ -16,8 +18,8 @@ export default function RecipeEditPage() {
|
|||
if (id) {
|
||||
try {
|
||||
// Fetch recipe data when editing an existing one
|
||||
const data = await fetchRecipe(id)
|
||||
setRecipe(data)
|
||||
const data : RecipeDto = await fetchRecipe(id);
|
||||
setRecipe(mapRecipeDtoToModel(data));
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
}
|
||||
|
|
@ -40,12 +42,13 @@ export default function RecipeEditPage() {
|
|||
loadRecipe()
|
||||
}, [id])
|
||||
|
||||
const handleSave = async (updated: Recipe) => {
|
||||
const handleSave = async (updated: RecipeModel) => {
|
||||
try {
|
||||
const dto = mapRecipeModelToDto(updated);
|
||||
if (updated.id) {
|
||||
await updateRecipe(updated)
|
||||
await updateRecipe(dto)
|
||||
} else {
|
||||
await createRecipe(updated)
|
||||
await createRecipe(dto)
|
||||
}
|
||||
navigateBack();
|
||||
} catch (err) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue