[d7b7f00] | 1 | import styles from "../../css/ApplicationFormCss/ApplicationForm.module.css"
|
---|
| 2 | import modalStyles from "../../css/ApplicationFormCss/AlertModal.module.css";
|
---|
| 3 | import React, { useEffect, useState } from "react";
|
---|
| 4 | import { useNavigate } from "react-router-dom";
|
---|
| 5 |
|
---|
| 6 | const RecipeApplication = () => {
|
---|
| 7 | const [recipeName, setRecipeName] = useState('');
|
---|
| 8 | const [recipeDesc, setRecipeDesc] = useState('');
|
---|
| 9 | const [recipeCategory, setRecipeCategory] = useState('');
|
---|
| 10 | const [recipeOrigin, setRecipeOrigin] = useState('');
|
---|
| 11 | const [recipeVideoUrl, setRecipeVideoUrl] = useState('');
|
---|
| 12 | const [recipeMealThumb, setRecipeMealThumb] = useState('');
|
---|
| 13 | const [ingredients, setIngredients] = useState([{ ingredient: '', dose: '' }]);
|
---|
| 14 |
|
---|
| 15 | const [showModal, setShowModal] = useState(false);
|
---|
| 16 | const [isLoggedIn, setIsLoggedIn] = useState(false);
|
---|
| 17 | const navigate = useNavigate();
|
---|
| 18 |
|
---|
| 19 | useEffect(() => {
|
---|
| 20 | const token = localStorage.getItem('token');
|
---|
| 21 | setIsLoggedIn(!!token);
|
---|
| 22 | }, []);
|
---|
| 23 |
|
---|
| 24 | const handleSubmit = async (e) => {
|
---|
| 25 | e.preventDefault();
|
---|
| 26 | try {
|
---|
| 27 | const token = localStorage.getItem('token');
|
---|
| 28 |
|
---|
| 29 | const payload = {
|
---|
| 30 | recipeName: recipeName,
|
---|
| 31 | recipeDesc: recipeDesc,
|
---|
| 32 | recipeCategory: recipeCategory,
|
---|
| 33 | recipeOrigin: recipeOrigin,
|
---|
| 34 | recipeVideoURL: recipeVideoUrl,
|
---|
| 35 | recipeMealThumb: recipeMealThumb,
|
---|
| 36 | ingredients: ingredients
|
---|
| 37 | };
|
---|
| 38 |
|
---|
| 39 | const response = await fetch('http://localhost:8080/api/recipes/add', {
|
---|
| 40 | method: 'POST',
|
---|
| 41 | headers: {
|
---|
| 42 | 'Authorization': `Bearer ${token}`,
|
---|
| 43 | 'Content-Type': 'application/json'
|
---|
| 44 | },
|
---|
| 45 | body: JSON.stringify(payload)
|
---|
| 46 | });
|
---|
| 47 |
|
---|
| 48 | if (response.ok) {
|
---|
| 49 | setShowModal(true);
|
---|
| 50 | setTimeout(() => {
|
---|
| 51 | setShowModal(false);
|
---|
| 52 | navigate('/');
|
---|
| 53 | }, 2500);
|
---|
| 54 | } else {
|
---|
| 55 | alert('Failed to submit application');
|
---|
| 56 | }
|
---|
| 57 | } catch (error) {
|
---|
| 58 | console.error('Error submitting application:', error);
|
---|
| 59 | alert('An error occurred while submitting the application.');
|
---|
| 60 | }
|
---|
| 61 | };
|
---|
| 62 |
|
---|
| 63 | const handleAddIngredient = () => {
|
---|
| 64 | setIngredients([...ingredients, { ingredient: '', dose: '' }]);
|
---|
| 65 | };
|
---|
| 66 |
|
---|
| 67 | const handleRemoveIngredient = (index) => {
|
---|
| 68 | const newIngredients = ingredients.filter((_, i) => i !== index);
|
---|
| 69 | setIngredients(newIngredients);
|
---|
| 70 | };
|
---|
| 71 |
|
---|
| 72 | const handleIngredientChange = (index, value) => {
|
---|
| 73 | const newIngredients = [...ingredients];
|
---|
| 74 | newIngredients[index].ingredient = value;
|
---|
| 75 | setIngredients(newIngredients);
|
---|
| 76 | };
|
---|
| 77 |
|
---|
| 78 | const handleDoseChange = (index, value) => {
|
---|
| 79 | const newIngredients = [...ingredients];
|
---|
| 80 | newIngredients[index].dose = value;
|
---|
| 81 | setIngredients(newIngredients);
|
---|
| 82 | };
|
---|
| 83 |
|
---|
| 84 | if (!isLoggedIn) {
|
---|
| 85 | return (
|
---|
| 86 | <div className={styles.formContainer}>
|
---|
| 87 | <div className={styles.messageContainer}>
|
---|
| 88 | <h2 className={styles.formTitle}>Please log in to add your recipe.</h2>
|
---|
| 89 | <button onClick={() => navigate('/login')} className={styles.loginButton}>
|
---|
| 90 | Go to Login
|
---|
| 91 | </button>
|
---|
| 92 | </div>
|
---|
| 93 | </div>
|
---|
| 94 | );
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | return (
|
---|
| 98 | <div className={styles.formContainer}>
|
---|
| 99 | <form onSubmit={handleSubmit} className={styles.applicationForm}>
|
---|
| 100 | <h2 className={styles.formTitle}>Apply for your recipe to be added!</h2>
|
---|
| 101 |
|
---|
| 102 | <div className={styles.formGroup}>
|
---|
| 103 | <label htmlFor="recipeName">Recipe Name:</label>
|
---|
| 104 | <input
|
---|
| 105 | type="text"
|
---|
| 106 | id="recipeName"
|
---|
| 107 | onChange={(e) => setRecipeName(e.target.value)}
|
---|
| 108 | placeholder="Enter recipe name"
|
---|
| 109 | required
|
---|
| 110 | />
|
---|
| 111 | </div>
|
---|
| 112 |
|
---|
| 113 | <div className={styles.formGroup}>
|
---|
| 114 | <label htmlFor="description">Recipe Description:</label>
|
---|
| 115 | <textarea
|
---|
| 116 | id="description"
|
---|
| 117 | rows="6"
|
---|
| 118 | placeholder="Write a guide on how to prepare the recipe..."
|
---|
| 119 | onChange={(e) => setRecipeDesc(e.target.value)}
|
---|
| 120 | required
|
---|
| 121 | style={{ resize: "none" }}
|
---|
| 122 | />
|
---|
| 123 | </div>
|
---|
| 124 |
|
---|
| 125 | <div className={styles.formGroup}>
|
---|
| 126 | <div className={styles.innerFormContainer}>
|
---|
| 127 | <div className={styles.innerFormData}>
|
---|
| 128 | <label htmlFor="category">Category:</label>
|
---|
| 129 | <input
|
---|
| 130 | type="text"
|
---|
| 131 | id="category"
|
---|
| 132 | onChange={(e) => setRecipeCategory(e.target.value)}
|
---|
| 133 | placeholder={"Breakfast, Lunch, Beef..."}
|
---|
| 134 | />
|
---|
| 135 | </div>
|
---|
| 136 | <div className={styles.innerFormData}>
|
---|
| 137 | <label htmlFor="origin">Nationality:</label>
|
---|
| 138 | <input
|
---|
| 139 | type="text"
|
---|
| 140 | id="origin"
|
---|
| 141 | onChange={(e) => setRecipeOrigin(e.target.value)}
|
---|
| 142 | placeholder={"Italian, French, German..."}
|
---|
| 143 | />
|
---|
| 144 | </div>
|
---|
| 145 | </div>
|
---|
| 146 | </div>
|
---|
| 147 |
|
---|
| 148 | <div className={styles.formGroup}>
|
---|
| 149 | <label>Ingredients:</label>
|
---|
| 150 | {ingredients.map((ingredientItem, index) => (
|
---|
| 151 | <div key={index} className={styles.innerFormContainer}>
|
---|
| 152 | <div className={styles.innerFormData}>
|
---|
| 153 | <input
|
---|
| 154 | type="text"
|
---|
| 155 | placeholder="Ingredient"
|
---|
| 156 | value={ingredientItem.ingredient}
|
---|
| 157 | onChange={(e) => handleIngredientChange(index, e.target.value)}
|
---|
| 158 | required
|
---|
| 159 | />
|
---|
| 160 | </div>
|
---|
| 161 | <div className={styles.innerFormData}>
|
---|
| 162 | <input
|
---|
| 163 | type="text"
|
---|
| 164 | placeholder="Dose"
|
---|
| 165 | value={ingredientItem.dose}
|
---|
| 166 | onChange={(e) => handleDoseChange(index, e.target.value)}
|
---|
| 167 | required
|
---|
| 168 | />
|
---|
| 169 | </div>
|
---|
| 170 | <button type="button" className={styles.removeIngredientButton} onClick={() => handleRemoveIngredient(index)}>Remove</button>
|
---|
| 171 | </div>
|
---|
| 172 | ))}
|
---|
| 173 | <button type="button" className={styles.addIngredientButton} onClick={handleAddIngredient}>Add Ingredient</button>
|
---|
| 174 | </div>
|
---|
| 175 |
|
---|
| 176 | <div className={styles.formGroup}>
|
---|
| 177 | <label htmlFor="mealThumb">Meal Thumbnail:</label>
|
---|
| 178 | <input
|
---|
| 179 | type="url"
|
---|
| 180 | id="mealThumb"
|
---|
| 181 | onChange={(e) => setRecipeMealThumb(e.target.value)}
|
---|
| 182 | placeholder="Insert url of a picture of the recipe..."
|
---|
| 183 | required
|
---|
| 184 | />
|
---|
| 185 | </div>
|
---|
| 186 |
|
---|
| 187 | <div className={styles.formGroup}>
|
---|
| 188 | <label htmlFor="video">Video URL:</label>
|
---|
| 189 | <input
|
---|
| 190 | type="url"
|
---|
| 191 | id="video"
|
---|
| 192 | onChange={(e) => setRecipeVideoUrl(e.target.value)}
|
---|
| 193 | placeholder="Insert url of the video of the recipe..."
|
---|
| 194 | required
|
---|
| 195 | />
|
---|
| 196 | </div>
|
---|
| 197 |
|
---|
| 198 | <div className={styles.buttonContainer}>
|
---|
| 199 | <button type="submit" className={styles.submitButton}>
|
---|
| 200 | Submit Application
|
---|
| 201 | </button>
|
---|
| 202 | </div>
|
---|
| 203 | </form>
|
---|
| 204 |
|
---|
| 205 | {showModal && (
|
---|
| 206 | <>
|
---|
| 207 | <div className={`${modalStyles.modalOverlay} ${modalStyles.show}`}></div>
|
---|
| 208 | <div className={`${modalStyles.alertModal} ${modalStyles.show}`}>
|
---|
| 209 | <h3>Application Submitted!</h3>
|
---|
| 210 | <p>Your application has been submitted successfully. You will be redirected to the homepage shortly.</p>
|
---|
| 211 | <button className={modalStyles.closeButton} onClick={() => setShowModal(false)}>
|
---|
| 212 | Close
|
---|
| 213 | </button>
|
---|
| 214 | </div>
|
---|
| 215 | </>
|
---|
| 216 | )}
|
---|
| 217 | </div>
|
---|
| 218 | );
|
---|
| 219 | };
|
---|
| 220 |
|
---|
| 221 | export default RecipeApplication;
|
---|