import React, { useState } from 'react'; import { addRecipe } from "../services/frontendApi.js"; import { useNavigate } from "react-router-dom"; import AddBulkIngredients from "../components/AddBulkIngredients.tsx" import AddIngredientsForm from "../components/AddIngredientsForm.tsx" import AddStepsForm from "../components/AddStepsForm.tsx" import AddBulkSteps from "../components/AddBulkSteps.tsx" import { type Ingredient, type Step } from "../types/Recipe"; function AddRecipe() { const [newRecipeId, setNewRecipeId] = useState(null); const navigate = useNavigate(); const [ingredients, setIngredients] = useState([]); const [steps, setSteps] = useState([]); const [showBulkForm, setShowBulkForm] = useState(true); const [recipeName, setRecipeName] = useState(""); const [recipeCuisine, setRecipeCuisine] = useState(""); const addRecipeForm = async (event: React.FormEvent) => { event.preventDefault(); const stepsHash = Object.fromEntries( steps.map(step => [step.idx, step.instructions]) ); if (recipeName && recipeCuisine && Object.keys(stepsHash).length > 0 && ingredients.length > 0) { const recipeData = { name: recipeName, cuisine: recipeCuisine, ingredients: ingredients, steps: stepsHash } console.log(recipeData) const data = await addRecipe(recipeData); setNewRecipeId(data.id); } else { alert('missing required data') } }; React.useEffect(() => { if (newRecipeId !== null && newRecipeId !== undefined) { navigate(`/recipe/${newRecipeId}`); } }, [newRecipeId, navigate]); return (
setRecipeName(e.target.value)} /> setRecipeCuisine(e.target.value)} />
{showBulkForm ? : }
    {ingredients.map((ing, index) => (
  • {`${ing.quantity} ${ing.unit} ${ing.name}`}
  • ))}
{showBulkForm ? : }
    {steps.map((step) => (
  • {`${step.idx}. ${step.instructions}`}
  • ))}
) } export default AddRecipe