recipe_app/frontend/src/pages/AddRecipe.tsx

146 lines
5.1 KiB
TypeScript
Raw Normal View History

2025-07-08 11:13:01 -07:00
import React, { useState } from 'react';
2025-07-08 15:42:28 -07:00
import { addRecipe } from "../services/frontendApi.js";
2025-07-08 11:13:01 -07:00
import { useNavigate } from "react-router-dom";
2025-07-09 14:43:45 -07:00
import AddBulkIngredients from "../components/AddBulkIngredients.tsx"
2025-07-10 16:34:53 -07:00
import AddBulkSteps from "../components/AddBulkSteps.tsx"
import StarRating from "../components/StarRating.tsx"
// import { type Step } from "../types/Recipe";
interface Step {
step_number: number;
instruction: string;
}
2025-07-08 10:40:49 -07:00
function AddRecipe() {
2025-07-08 11:13:01 -07:00
const [newRecipeId, setNewRecipeId] = useState<number | null>(null);
const navigate = useNavigate();
const [ingredients, setIngredients] = useState<string[]>([]);
2025-07-10 16:34:53 -07:00
const [steps, setSteps] = useState<Step[]>([]);
2025-07-10 14:28:57 -07:00
const [showBulkForm, setShowBulkForm] = useState(true);
2025-07-11 09:41:19 -07:00
const [recipeName, setRecipeName] = useState("");
const [recipeCuisine, setRecipeCuisine] = useState("");
const [author, setAuthor] = useState("");
const [stars, setStars] = useState(0);
const [prepMinutes, setPrepMinutes] = useState(5);
const [cookMinutes, setCookMinutes] = useState(5);
2025-07-08 11:13:01 -07:00
2025-07-08 10:40:49 -07:00
const addRecipeForm = async (event: React.FormEvent) => {
event.preventDefault();
2025-07-10 16:34:53 -07:00
const stepsHash = Object.fromEntries(
steps.map(step => [step.step_number, step.instruction])
2025-07-10 16:34:53 -07:00
);
2025-07-11 09:41:19 -07:00
if (recipeName && recipeCuisine && Object.keys(stepsHash).length > 0 && ingredients.length > 0) {
2025-07-08 11:13:01 -07:00
const recipeData = {
2025-07-11 09:41:19 -07:00
name: recipeName,
cuisine: recipeCuisine,
author: author,
prep_minutes: prepMinutes,
cook_minutes: cookMinutes,
stars: stars,
2025-07-09 17:06:40 -07:00
ingredients: ingredients,
2025-07-10 16:34:53 -07:00
steps: stepsHash
2025-07-08 11:13:01 -07:00
}
2025-07-09 17:06:40 -07:00
console.log(recipeData)
2025-07-08 11:13:01 -07:00
const data = await addRecipe(recipeData);
setNewRecipeId(data.id);
2025-07-11 09:41:19 -07:00
} else {
alert('missing required data')
2025-07-08 11:13:01 -07:00
}
};
2025-07-09 17:06:40 -07:00
2025-07-08 11:13:01 -07:00
React.useEffect(() => {
2025-07-10 16:34:53 -07:00
if (newRecipeId !== null && newRecipeId !== undefined) {
2025-07-08 11:13:01 -07:00
navigate(`/recipe/${newRecipeId}`);
2025-07-08 10:40:49 -07:00
}
2025-07-08 11:13:01 -07:00
}, [newRecipeId, navigate]);
2025-07-08 10:40:49 -07:00
return (
2025-07-17 09:17:10 -07:00
<div className="add-recipe-card page-outer">
2025-07-08 10:40:49 -07:00
<form onSubmit={addRecipeForm} className="add-recipe-form">
<input
type="text"
placeholder="recipe name"
2025-07-11 17:06:41 -07:00
className="recipe-name mb-4 p-2 border border-gray-300 rounded w-full"
2025-07-11 09:41:19 -07:00
value={recipeName}
onChange={(e) => setRecipeName(e.target.value)}
2025-07-08 10:40:49 -07:00
/>
<input
type="text"
placeholder="cuisine"
2025-07-11 17:06:41 -07:00
className="recipe-cusine mb-4 p-2 border border-gray-300 rounded w-full"
2025-07-11 09:41:19 -07:00
value={recipeCuisine}
onChange={(e) => setRecipeCuisine(e.target.value)}
2025-07-08 10:40:49 -07:00
/>
<input
type="text"
placeholder="author or source"
className="recipe-cusine mb-4 p-2 border border-gray-300 rounded w-full"
value={author}
onChange={(e) => setAuthor(e.target.value)}
/>
<div className="flex items-center justify-between mb-4">
<div>
<label htmlFor="prepTime" className="mr-2 font-bold">Prep Time:</label>
<input
type="number"
placeholder="prep time in minutes"
className="recipe-cusine p-2 border border-gray-300 rounded w-24"
value={prepMinutes}
onChange={(e) => setPrepMinutes(e.target.value)}
/>
<span className="ml-2">minutes</span>
</div>
<div>
<label htmlFor="cookTime" className="mr-2 font-bold">Cook Time:</label>
<input
type="text"
placeholder="cook time in minutes"
className="recipe-cusine p-2 border border-gray-300 rounded w-24"
value={cookMinutes}
onChange={(e) => setCookMinutes(e.target.value)}
/>
<span className="ml-2">minutes</span>
</div>
<div>
<StarRating rating={stars} onRatingChange={(newRating: number) => setStars(newRating)} />
</div>
</div>
<label className="mb-4 flex items-center cursor-pointer">
<div className="relative">
<input
type="checkbox"
checked={showBulkForm}
onChange={(e) => setShowBulkForm(e.target.checked)}
className="sr-only"
/>
</div>
</label>
<div>
<AddBulkIngredients ingredients={ingredients} onChange={setIngredients} />
2025-07-11 17:06:41 -07:00
</div>
{/*<ul className="mb-4">
2025-07-11 17:06:41 -07:00
{ingredients.map((ing, index) => (
<li key={index} className="text-gray-700 flex items-start mb-2">
<span>{ing}</span>
2025-07-11 17:06:41 -07:00
</li>
))}
</ul>*/}
<div>
<AddBulkSteps steps={steps} onChange={setSteps} />
</div>
{/*<ul className="mb-4">
2025-07-10 16:34:53 -07:00
{steps.map((step) => (
<li key={step.step_number} className="text-gray-700 flex items-start mb-2">
<span>{`${step.step_number}. ${step.instruction}`}</span>
2025-07-11 17:06:41 -07:00
</li>
2025-07-10 16:34:53 -07:00
))}
</ul>*/}
<button type="submit" className="ar-button bg-amber-600 text-white py-2 px-4 rounded hover:bg-amber-700">
submit
</button>
</form>
2025-07-08 10:40:49 -07:00
</div>
)
}
export default AddRecipe