2025-08-06 16:18:06 -07:00
|
|
|
|
import { useParams, useNavigate, Link } from "react-router-dom";
|
2025-07-08 10:40:49 -07:00
|
|
|
|
import { useState, useEffect } from "react";
|
2025-08-15 20:52:38 +00:00
|
|
|
|
import {
|
|
|
|
|
|
getRecipeById,
|
|
|
|
|
|
deleteRecipe,
|
|
|
|
|
|
setDBStars,
|
|
|
|
|
|
} from "../services/frontendApi.js";
|
|
|
|
|
|
import { type Recipe } from "../types/Recipe";
|
|
|
|
|
|
import Modal from "../components/Modal.tsx";
|
|
|
|
|
|
import DemoModal from "../components/DemoModal.tsx";
|
|
|
|
|
|
import StarRating from "../components/StarRating.tsx";
|
|
|
|
|
|
import TimeDisplay from "../components/TimeDisplay.tsx";
|
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-08-15 20:52:38 +00: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);
|
2025-08-06 16:18:06 -07:00
|
|
|
|
const [stars, setStars] = useState<number>(0);
|
|
|
|
|
|
const [initialStars, setInitialStars] = useState<number | null>(null);
|
|
|
|
|
|
const [showConfirmModal, setShowConfirmModal] = useState(false);
|
|
|
|
|
|
const [showDemoModal, setShowDemoModal] = useState(false);
|
2025-07-08 10:40:49 -07:00
|
|
|
|
const { id } = useParams();
|
2025-08-15 20:52:38 +00:00
|
|
|
|
const isWebSource =
|
|
|
|
|
|
recipe && recipe.details && recipe.details.author
|
|
|
|
|
|
? /http|com/.test(recipe.details.author) //etc
|
|
|
|
|
|
: false;
|
2025-08-06 16:18:06 -07:00
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
|
|
2025-08-15 20:52:38 +00:00
|
|
|
|
const openModal = () => {
|
|
|
|
|
|
setShowConfirmModal(true);
|
|
|
|
|
|
};
|
|
|
|
|
|
const closeModal = () => {
|
|
|
|
|
|
setShowConfirmModal(false);
|
|
|
|
|
|
};
|
2025-08-06 16:18:06 -07:00
|
|
|
|
|
|
|
|
|
|
const confirmDelete = () => {
|
2025-08-15 20:52:38 +00:00
|
|
|
|
if (process.env.NODE_ENV === "demo") {
|
2025-08-06 16:18:06 -07:00
|
|
|
|
closeModal();
|
|
|
|
|
|
setShowDemoModal(true);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
handleDelete(recipe.details.id);
|
|
|
|
|
|
closeModal();
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2025-07-08 10:40:49 -07:00
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
const loadRecipe = async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const recipe = await getRecipeById(id);
|
2025-08-06 16:18:06 -07:00
|
|
|
|
if (!recipe.details) {
|
2025-08-15 20:52:38 +00:00
|
|
|
|
setError("Sorry, this recipe no longer exists");
|
2025-08-06 16:18:06 -07:00
|
|
|
|
} else {
|
|
|
|
|
|
setRecipe(recipe);
|
2025-08-15 20:52:38 +00:00
|
|
|
|
setStars(recipe.details?.stars ?? 0);
|
2025-08-06 16:18:06 -07:00
|
|
|
|
setInitialStars(recipe.details?.stars ?? 0);
|
2025-08-15 20:52:38 +00:00
|
|
|
|
if (process.env.NODE_ENV === "dev") {
|
|
|
|
|
|
console.log(recipe);
|
2025-08-06 16:18:06 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-08 10:40:49 -07:00
|
|
|
|
} 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-08-06 16:18:06 -07:00
|
|
|
|
|
|
|
|
|
|
const handleDelete = async (id: number | void) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
await deleteRecipe(id);
|
2025-08-15 20:52:38 +00:00
|
|
|
|
navigate("/");
|
2025-08-06 16:18:06 -07:00
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error("Error deleting recipe:", error);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
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
|
|
|
|
{loading ? (
|
|
|
|
|
|
<div className="loading">Loading...</div>
|
2025-08-06 16:18:06 -07:00
|
|
|
|
) : error ? (
|
|
|
|
|
|
<div>
|
|
|
|
|
|
<div className="error-message text-lg">{error}</div>
|
|
|
|
|
|
<div className="m-2">
|
2025-08-15 20:52:38 +00:00
|
|
|
|
<Link
|
|
|
|
|
|
to="/"
|
2025-08-20 10:06:23 -07:00
|
|
|
|
className="ar-button bg-[var(--color-buttonBg)] text-[var(--color-textLight)] py-2 px-4 rounded hover:bg-[var(--color-buttonBgHover)]"
|
2025-08-15 20:52:38 +00:00
|
|
|
|
>
|
2025-08-06 16:18:06 -07:00
|
|
|
|
Return to Cookbook
|
|
|
|
|
|
</Link>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2025-07-08 10:40:49 -07:00
|
|
|
|
) : (
|
2025-08-20 10:06:23 -07:00
|
|
|
|
<div className="border-b-2 border-[var(--color-primaryBorder)] pb-4">
|
2025-08-08 11:55:59 -07:00
|
|
|
|
<div className="recipe-card">
|
|
|
|
|
|
<div className="flex relative justify-between">
|
2025-08-15 20:52:38 +00:00
|
|
|
|
<button
|
2025-08-20 10:06:23 -07:00
|
|
|
|
onClick={() => { }}
|
2025-08-15 20:52:38 +00:00
|
|
|
|
className="invisible ar-button py-1 px-1 rounded self-start"
|
|
|
|
|
|
>
|
|
|
|
|
|
🗑️
|
|
|
|
|
|
</button>
|
2025-08-20 10:06:23 -07:00
|
|
|
|
<h3 className="text-center max-w-lg px-4 text-2xl lg:text-3xl font-bold text-[var(--color-textDark)]">
|
2025-08-15 20:52:38 +00:00
|
|
|
|
{recipe.details.name}
|
|
|
|
|
|
</h3>
|
|
|
|
|
|
<button
|
|
|
|
|
|
onClick={openModal}
|
2025-08-20 10:06:23 -07:00
|
|
|
|
className="ar-button bg-[var(--color-buttonBg)] text-[var(--color-textLight)] py-1 px-1 rounded hover:bg-[var(--color-buttonBgHover)] self-start"
|
2025-08-15 20:52:38 +00:00
|
|
|
|
>
|
|
|
|
|
|
🗑️
|
|
|
|
|
|
</button>
|
2025-08-08 11:55:59 -07:00
|
|
|
|
</div>
|
|
|
|
|
|
<div className="mt-1">
|
2025-08-20 10:06:23 -07:00
|
|
|
|
<p className="text-[var(--color-textDark)] italic text-lg">
|
2025-08-15 20:52:38 +00:00
|
|
|
|
{recipe.details.cuisine}
|
|
|
|
|
|
</p>
|
|
|
|
|
|
<p>
|
|
|
|
|
|
prep: <TimeDisplay minutes={recipe.details.prep_minutes ?? 0} />{" "}
|
|
|
|
|
|
| cook:{" "}
|
|
|
|
|
|
<TimeDisplay minutes={recipe.details.cook_minutes ?? 0} />
|
|
|
|
|
|
</p>
|
2025-08-08 11:55:59 -07:00
|
|
|
|
</div>
|
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-08-20 10:06:23 -07:00
|
|
|
|
<div className="bg-[var(--color-backdrop)] rounded-lg p-4 shadow-sm border border-[var(--color-primaryBorder)]">
|
|
|
|
|
|
<h4 className="text-xl font-semibold text-[var(--color-textDark)] mb-3 flex items-center">
|
2025-07-11 17:06:41 -07:00
|
|
|
|
Ingredients:
|
|
|
|
|
|
</h4>
|
|
|
|
|
|
<ul className="space-y-2">
|
2025-08-15 20:52:38 +00:00
|
|
|
|
{recipe.ingredients.map((ingredient: string, index) => (
|
2025-08-20 10:06:23 -07:00
|
|
|
|
<li key={index} className="text-[var(--color-secondaryTextDark)] flex items-start">
|
|
|
|
|
|
<span className="w-1.5 h-1.5 bg-[var(--color-buttonBg)] rounded-full mt-2 mr-3 flex-shrink-0"></span>
|
2025-08-15 20:52:38 +00:00
|
|
|
|
<span className="font-medium text-left">{ingredient}</span>
|
2025-07-11 17:06:41 -07:00
|
|
|
|
</li>
|
|
|
|
|
|
))}
|
|
|
|
|
|
</ul>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2025-08-20 10:06:23 -07:00
|
|
|
|
<div className="bg-[var(--color-backdrop)] rounded-lg p-4 shadow-sm border border-[var(--color-primaryBorder)]">
|
|
|
|
|
|
<h4 className="text-xl font-semibold text-[var(--color-textDark)] mb-3 flex items-center">
|
2025-07-11 17:06:41 -07:00
|
|
|
|
Instructions:
|
|
|
|
|
|
</h4>
|
|
|
|
|
|
<ol className="space-y-3">
|
2025-08-15 20:52:38 +00:00
|
|
|
|
{recipe.steps &&
|
|
|
|
|
|
Object.keys(recipe.steps || {}).map((stepNumber) => (
|
|
|
|
|
|
<li
|
|
|
|
|
|
key={stepNumber}
|
2025-08-20 10:06:23 -07:00
|
|
|
|
className="text-[var(--color-secondaryTextDark)] flex items-start"
|
2025-08-15 20:52:38 +00:00
|
|
|
|
>
|
2025-08-20 10:06:23 -07:00
|
|
|
|
<span className="bg-[var(--color-buttonBg)] text-[var(--color-textLight)] rounded-full w-6 h-6 flex items-center justify-center text-sm font-bold mr-3 mt-0.5 flex-shrink-0">
|
2025-08-15 20:52:38 +00:00
|
|
|
|
{recipe.steps[parseInt(stepNumber)].step_number}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
<span className="leading-relaxed text-left">
|
|
|
|
|
|
{recipe.steps[parseInt(stepNumber)].instruction}
|
|
|
|
|
|
</span>
|
|
|
|
|
|
</li>
|
|
|
|
|
|
))}
|
2025-07-11 17:06:41 -07:00
|
|
|
|
</ol>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2025-08-20 10:06:23 -07:00
|
|
|
|
<div className="border-t-2 border-[var(--color-primaryBorder)] pt-4">
|
|
|
|
|
|
<div className="flex justify-between items-center text-sm text-[var(--color-textDark)]">
|
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>
|
2025-08-15 20:52:38 +00:00
|
|
|
|
<StarRating
|
|
|
|
|
|
rating={stars}
|
|
|
|
|
|
onRatingChange={(newRating: number) => setStars(newRating)}
|
|
|
|
|
|
/>
|
2025-07-24 12:11:32 -07:00
|
|
|
|
</span>
|
2025-07-11 17:06:41 -07:00
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2025-07-08 10:40:49 -07:00
|
|
|
|
</div>
|
2025-08-15 20:52:38 +00:00
|
|
|
|
)}
|
2025-08-06 16:18:06 -07:00
|
|
|
|
<Modal
|
|
|
|
|
|
isOpen={showConfirmModal}
|
|
|
|
|
|
onClose={closeModal}
|
|
|
|
|
|
message="Are you sure you want to delete this recipe?"
|
|
|
|
|
|
confirmAction={confirmDelete}
|
|
|
|
|
|
cancelAction={closeModal}
|
|
|
|
|
|
/>
|
|
|
|
|
|
{showDemoModal && (
|
|
|
|
|
|
<DemoModal
|
|
|
|
|
|
isOpen={showDemoModal}
|
|
|
|
|
|
onClose={() => setShowDemoModal(false)}
|
|
|
|
|
|
closeModal={() => setShowDemoModal(false)}
|
|
|
|
|
|
/>
|
2025-07-08 10:40:49 -07:00
|
|
|
|
)}
|
2025-08-15 20:52:38 +00:00
|
|
|
|
</div>
|
2025-07-08 10:40:49 -07:00
|
|
|
|
);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default RecipePage;
|