recipe_app/frontend/src/pages/AddRecipe.tsx

60 lines
1.6 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 AddIngredientsForm from "../components/AddIngredientsForm.tsx"
import AddBulkIngredients from "../components/AddBulkIngredients.tsx"
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-08 10:40:49 -07:00
const addRecipeForm = async (event: React.FormEvent) => {
event.preventDefault();
const nameElement = document.getElementById('ar-name') as HTMLInputElement | null;
const cuisineElement = document.getElementById('ar-cuisine') as HTMLInputElement | null;
if (nameElement && cuisineElement) {
2025-07-08 11:13:01 -07:00
const recipeData = {
name: nameElement.value,
cuisine: cuisineElement.value
}
const data = await addRecipe(recipeData);
setNewRecipeId(data.id);
}
2025-07-08 10:40:49 -07:00
2025-07-08 11:13:01 -07:00
};
React.useEffect(() => {
if (newRecipeId !== null) {
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"
className="ar-name"
id="ar-name"
/>
<input
type="text"
placeholder="cuisine"
className="ar-cusine"
id="ar-cuisine"
/>
<button type="submit" className="ar-button">
submit
</button>
</form>
2025-07-09 14:43:45 -07:00
<div>
<AddIngredientsForm />
<AddBulkIngredients />
</div>
2025-07-08 10:40:49 -07:00
</div>
)
}
export default AddRecipe