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 14:28:57 -07:00
|
|
|
import AddIngredientsForm from "../components/AddIngredientsForm.tsx"
|
2025-07-10 16:34:53 -07:00
|
|
|
import AddStepsForm from "../components/AddStepsForm.tsx"
|
|
|
|
import AddBulkSteps from "../components/AddBulkSteps.tsx"
|
|
|
|
import { type Ingredient, type Step } from "../types/Recipe";
|
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();
|
2025-07-10 16:34:53 -07:00
|
|
|
const [ingredients, setIngredients] = useState<Ingredient[]>([]);
|
|
|
|
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("");
|
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.idx, step.instructions])
|
|
|
|
);
|
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,
|
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 (
|
|
|
|
<div className="add-recipe-outer">
|
|
|
|
<form onSubmit={addRecipeForm} className="add-recipe-form">
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
placeholder="name"
|
2025-07-11 09:41:19 -07:00
|
|
|
className="recipe-name"
|
|
|
|
value={recipeName}
|
|
|
|
onChange={(e) => setRecipeName(e.target.value)}
|
2025-07-08 10:40:49 -07:00
|
|
|
/>
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
placeholder="cuisine"
|
2025-07-11 09:41:19 -07:00
|
|
|
className="recipe-cusine"
|
|
|
|
value={recipeCuisine}
|
|
|
|
onChange={(e) => setRecipeCuisine(e.target.value)}
|
2025-07-08 10:40:49 -07:00
|
|
|
/>
|
|
|
|
<button type="submit" className="ar-button">
|
|
|
|
submit
|
|
|
|
</button>
|
|
|
|
</form>
|
2025-07-10 14:28:57 -07:00
|
|
|
<label>
|
|
|
|
<input
|
|
|
|
type="checkbox"
|
|
|
|
checked={showBulkForm}
|
|
|
|
onChange={(e) => setShowBulkForm(e.target.checked)}
|
|
|
|
/>
|
|
|
|
Bulk Entry
|
|
|
|
</label>
|
2025-07-09 14:43:45 -07:00
|
|
|
<div>
|
2025-07-10 14:28:57 -07:00
|
|
|
{showBulkForm ?
|
|
|
|
<AddBulkIngredients ingredients={ingredients} onChange={setIngredients} /> :
|
|
|
|
<AddIngredientsForm ingredients={ingredients} onSubmit={setIngredients} />
|
|
|
|
}
|
2025-07-09 14:43:45 -07:00
|
|
|
</div>
|
2025-07-10 14:28:57 -07:00
|
|
|
<div>
|
|
|
|
<ul>
|
|
|
|
{ingredients.map((ing, index) => (
|
|
|
|
<li key={index}>{`${ing.quantity} ${ing.unit} ${ing.name}`}</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</div>
|
2025-07-10 16:34:53 -07:00
|
|
|
<div>
|
|
|
|
{showBulkForm ?
|
|
|
|
<AddBulkSteps steps={steps} onChange={setSteps} /> :
|
|
|
|
<AddStepsForm steps={steps} onSubmit={setSteps} />
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
<ul>
|
|
|
|
{steps.map((step) => (
|
|
|
|
<li key={step.idx}>{`${step.idx}. ${step.instructions}`}</li>
|
|
|
|
))}
|
|
|
|
</ul>
|
2025-07-08 10:40:49 -07:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
export default AddRecipe
|