import styles from "../../css/ApplicationFormCss/ApplicationForm.module.css" import modalStyles from "../../css/ApplicationFormCss/AlertModal.module.css"; import React, { useEffect, useState } from "react"; import { useNavigate } from "react-router-dom"; const RecipeApplication = () => { const [recipeName, setRecipeName] = useState(''); const [recipeDesc, setRecipeDesc] = useState(''); const [recipeCategory, setRecipeCategory] = useState(''); const [recipeOrigin, setRecipeOrigin] = useState(''); const [recipeVideoUrl, setRecipeVideoUrl] = useState(''); const [recipeMealThumb, setRecipeMealThumb] = useState(''); const [ingredients, setIngredients] = useState([{ ingredient: '', dose: '' }]); const [showModal, setShowModal] = useState(false); const [isLoggedIn, setIsLoggedIn] = useState(false); const navigate = useNavigate(); useEffect(() => { const token = localStorage.getItem('token'); setIsLoggedIn(!!token); }, []); const handleSubmit = async (e) => { e.preventDefault(); try { const token = localStorage.getItem('token'); const payload = { recipeName: recipeName, recipeDesc: recipeDesc, recipeCategory: recipeCategory, recipeOrigin: recipeOrigin, recipeVideoURL: recipeVideoUrl, recipeMealThumb: recipeMealThumb, ingredients: ingredients }; const response = await fetch('http://localhost:8080/api/recipes/add', { method: 'POST', headers: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' }, body: JSON.stringify(payload) }); if (response.ok) { setShowModal(true); setTimeout(() => { setShowModal(false); navigate('/'); }, 2500); } else { alert('Failed to submit application'); } } catch (error) { console.error('Error submitting application:', error); alert('An error occurred while submitting the application.'); } }; const handleAddIngredient = () => { setIngredients([...ingredients, { ingredient: '', dose: '' }]); }; const handleRemoveIngredient = (index) => { const newIngredients = ingredients.filter((_, i) => i !== index); setIngredients(newIngredients); }; const handleIngredientChange = (index, value) => { const newIngredients = [...ingredients]; newIngredients[index].ingredient = value; setIngredients(newIngredients); }; const handleDoseChange = (index, value) => { const newIngredients = [...ingredients]; newIngredients[index].dose = value; setIngredients(newIngredients); }; if (!isLoggedIn) { return (
Your application has been submitted successfully. You will be redirected to the homepage shortly.