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-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-09 17:06:40 -07:00
|
|
|
const [ingredients, setIngredients] = useState<{ quantity: number; unit: string; name: string }[]>([]);
|
2025-07-08 11:13:01 -07:00
|
|
|
|
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,
|
2025-07-09 17:06:40 -07:00
|
|
|
cuisine: cuisineElement.value,
|
|
|
|
ingredients: ingredients,
|
|
|
|
steps: { 1: 'step 1', 2: 'step 2' }
|
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-09 17:06:40 -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>
|
2025-07-09 17:06:40 -07:00
|
|
|
<AddBulkIngredients onChange={setIngredients} />
|
2025-07-09 14:43:45 -07:00
|
|
|
</div>
|
2025-07-08 10:40:49 -07:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
export default AddRecipe
|