diff --git a/frontend/src/App.css b/frontend/src/App.css index 4efbe93..240b32e 100644 --- a/frontend/src/App.css +++ b/frontend/src/App.css @@ -59,7 +59,7 @@ /* input field */ .input-field { - @apply border p-2 w-full mb-2 rounded placeholder-gray-400; + @apply border p-2 w-full rounded placeholder-gray-400; } .text-area { @@ -72,7 +72,7 @@ } /* lists */ - .default-list-item { + .default-list { @apply list-disc pl-6 mb-6 } diff --git a/frontend/src/components/RecipeDetailView.tsx b/frontend/src/components/RecipeDetailView.tsx index 5b9e7c9..e92e24f 100644 --- a/frontend/src/components/RecipeDetailView.tsx +++ b/frontend/src/components/RecipeDetailView.tsx @@ -1,5 +1,8 @@ import { useParams, Link } from "react-router-dom" import { recipes } from "../mock_data/recipes" +import type { Recipe } from "../types/recipe" +import { useState } from "react" + /** * Displays the full detail of a single recipe, @@ -10,28 +13,66 @@ export default function RecipeDetailView() { const { id } = useParams<{ id: string }>() const recipe = recipes.find((r) => r.id === id) + if (!recipe) { return

Recipe not found.

} - return ( + // Working copy for re-calculating ingredients + const [recipeWorkingCopy, updateRecipeWorkingCopy] = useState(recipe) + + const recalculateIngredients = (newAmount: number) => { + // Always calculate factor from the *original recipe*, not the working copy + const factor = newAmount / recipe.servings.amount + + // Create a new ingredient list with updated amounts + const updatedIngredients = recipe.ingredients.map((ing) => ({ + ...ing, + amount: ing.amount * factor, + })) + + // Update working copy with new servings + recalculated ingredients + updateRecipeWorkingCopy({ + ...recipeWorkingCopy, + servings: { + ...recipeWorkingCopy.servings, + amount: newAmount, + }, + ingredients: updatedIngredients, + }) + } + // @todo add a feature to recalculate ingredients based on servings + return (
-

{recipe.title}

+

{recipeWorkingCopy.title}

{/* Recipe image */} - {recipe.imageUrl && ( + {recipeWorkingCopy.imageUrl && ( {recipe.title} )} + {/* Servings */} +
+

For {recipeWorkingCopy.servings.amount} {recipeWorkingCopy.servings.unit}

+ { + recalculateIngredients(Number(e.target.value)) + } + } + /> +
{/* Ingredients */}

Zutaten

-

For {recipe.servings.amount} {recipe.servings.unit}

-