recipe_app/frontend/src/pages/RecipePage.tsx

50 lines
1.2 KiB
TypeScript
Raw Normal View History

2025-07-08 10:40:49 -07:00
import RecipeCard from "../components/RecipeCard.tsx"
import { useParams } from "react-router-dom";
import { useState, useEffect } from "react";
2025-07-08 15:42:28 -07:00
import { getRecipeById } from "../services/frontendApi.js";
2025-07-08 10:40:49 -07:00
import { type Recipe } from "../types/Recipe"
function RecipePage() {
2025-07-09 11:02:23 -07:00
const [recipe, setRecipe] = useState<Recipe>({
details: {},
ingredients: [],
steps: {}
});
2025-07-08 10:40:49 -07:00
const [error, setError] = useState<string | null>(null);
const [loading, setLoading] = useState(true);
const { id } = useParams();
useEffect(() => {
const loadRecipe = async () => {
try {
const recipe = await getRecipeById(id);
setRecipe(recipe);
console.log(recipe)
} catch (error) {
console.log(error);
setError("Failed to load recipes...");
} finally {
setLoading(false);
}
};
loadRecipe();
2025-07-09 11:02:23 -07:00
}, [id]);
2025-07-08 10:40:49 -07:00
return (
<div className="recipe">
{error && <div className="error-message">{error}</div>}
{loading ? (
<div className="loading">Loading...</div>
) : (
<div className="recipe-card">
2025-07-09 11:02:23 -07:00
<RecipeCard recipe={recipe} key={recipe.details.id} />
2025-07-08 10:40:49 -07:00
</div>
)}
</div>
);
}
export default RecipePage;