2025-07-08 10:40:49 -07:00
|
|
|
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-11 17:06:41 -07:00
|
|
|
import { type Recipe, type Ingredient } from "../types/Recipe"
|
2025-07-24 12:11:32 -07:00
|
|
|
import StarRating from "../components/StarRating.tsx"
|
|
|
|
import { setDBStars } from "../services/frontendApi.js";
|
2025-07-08 10:40:49 -07:00
|
|
|
|
|
|
|
function RecipePage() {
|
2025-07-09 11:02:23 -07:00
|
|
|
const [recipe, setRecipe] = useState<Recipe>({
|
|
|
|
details: {},
|
|
|
|
ingredients: [],
|
2025-07-24 12:11:32 -07:00
|
|
|
steps: []
|
2025-07-09 11:02:23 -07:00
|
|
|
});
|
2025-07-08 10:40:49 -07:00
|
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
const { id } = useParams();
|
2025-07-24 12:11:32 -07:00
|
|
|
const isWebSource = recipe && recipe.details && recipe.details.author
|
|
|
|
? /http|com/.test(recipe.details.author) //etc
|
|
|
|
: false;
|
|
|
|
const [stars, setStars] = useState<number>(0);
|
|
|
|
const [initialStars, setInitialStars] = useState<number | null>(null);
|
2025-07-08 10:40:49 -07:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const loadRecipe = async () => {
|
|
|
|
try {
|
|
|
|
const recipe = await getRecipeById(id);
|
|
|
|
setRecipe(recipe);
|
2025-07-24 12:11:32 -07:00
|
|
|
setStars(recipe.details?.stars ?? 0)
|
|
|
|
setInitialStars(recipe.details?.stars ?? 0);
|
2025-07-08 10:40:49 -07:00
|
|
|
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-24 12:11:32 -07:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (initialStars === null || initialStars === stars) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateStarsInDB = async () => {
|
|
|
|
await setDBStars(id, stars);
|
|
|
|
};
|
|
|
|
|
|
|
|
updateStarsInDB();
|
|
|
|
}, [stars]);
|
2025-07-08 10:40:49 -07:00
|
|
|
return (
|
2025-07-17 09:17:10 -07:00
|
|
|
<div className="recipe page-outer">
|
2025-07-08 10:40:49 -07:00
|
|
|
|
|
|
|
{error && <div className="error-message">{error}</div>}
|
|
|
|
|
|
|
|
{loading ? (
|
|
|
|
<div className="loading">Loading...</div>
|
|
|
|
) : (
|
2025-07-11 17:06:41 -07:00
|
|
|
|
2025-07-17 09:17:10 -07:00
|
|
|
<div className="recipe-card">
|
2025-07-11 17:06:41 -07:00
|
|
|
<div className="border-b-2 border-amber-300 pb-4 mb-6">
|
2025-07-24 12:11:32 -07:00
|
|
|
<h3 className="text-2xl lg:text-3xl font-bold text-amber-900 mb-2">{recipe.details.name}</h3>
|
2025-07-11 17:06:41 -07:00
|
|
|
<p className="text-amber-700 italic text-lg">{recipe.details.cuisine}</p>
|
2025-07-29 09:34:33 -07:00
|
|
|
<p>prep: {recipe.details.prep_minutes} min | cook: {recipe.details.cook_minutes} min</p>
|
2025-07-11 17:06:41 -07:00
|
|
|
</div>
|
|
|
|
|
2025-07-24 12:11:32 -07:00
|
|
|
<div className="grid lg:grid-cols-2 gap-6 mb-6">
|
2025-07-11 17:06:41 -07:00
|
|
|
<div className="bg-white rounded-lg p-4 shadow-sm border border-amber-100">
|
|
|
|
<h4 className="text-xl font-semibold text-amber-800 mb-3 flex items-center">
|
|
|
|
Ingredients:
|
|
|
|
</h4>
|
|
|
|
<ul className="space-y-2">
|
|
|
|
{recipe.ingredients.map((ingredient: Ingredient, index) => (
|
|
|
|
<li key={index} className="text-gray-700 flex items-start">
|
|
|
|
<span className="w-1.5 h-1.5 bg-amber-400 rounded-full mt-2 mr-3 flex-shrink-0"></span>
|
2025-08-05 15:09:52 -07:00
|
|
|
<span className="font-medium text-left">{ingredient.raw}</span>
|
2025-07-11 17:06:41 -07:00
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="bg-white rounded-lg p-4 shadow-sm border border-amber-100">
|
|
|
|
<h4 className="text-xl font-semibold text-amber-800 mb-3 flex items-center">
|
|
|
|
Instructions:
|
|
|
|
</h4>
|
|
|
|
<ol className="space-y-3">
|
|
|
|
{recipe.steps && Object.keys(recipe.steps || {}).map((stepNumber) => (
|
|
|
|
<li key={stepNumber} className="text-gray-700 flex items-start">
|
|
|
|
<span className="bg-amber-500 text-white rounded-full w-6 h-6 flex items-center justify-center text-sm font-bold mr-3 mt-0.5 flex-shrink-0">
|
2025-07-24 12:11:32 -07:00
|
|
|
{recipe.steps[parseInt(stepNumber)].step_number}
|
2025-07-11 17:06:41 -07:00
|
|
|
</span>
|
2025-08-05 15:09:52 -07:00
|
|
|
<span className="leading-relaxed text-left">{recipe.steps[parseInt(stepNumber)].instruction}</span>
|
2025-07-11 17:06:41 -07:00
|
|
|
</li>
|
|
|
|
))}
|
|
|
|
</ol>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="border-t-2 border-amber-300 pt-4">
|
|
|
|
<div className="flex justify-between items-center text-sm text-amber-600">
|
2025-07-24 12:11:32 -07:00
|
|
|
{isWebSource ? (
|
|
|
|
<span>Source: {recipe.details.author}</span>
|
|
|
|
) : (
|
|
|
|
<span>From the kitchen of {recipe.details.author}</span>
|
|
|
|
)}
|
|
|
|
<span>
|
|
|
|
<StarRating rating={stars} onRatingChange={(newRating: number) => setStars(newRating)} />
|
|
|
|
</span>
|
2025-07-11 17:06:41 -07:00
|
|
|
</div>
|
|
|
|
</div>
|
2025-07-08 10:40:49 -07:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default RecipePage;
|