diff --git a/src/app/recipe-add/recipe-add.component.html b/src/app/recipe-add/recipe-add.component.html index f7c9bca..3c17437 100644 --- a/src/app/recipe-add/recipe-add.component.html +++ b/src/app/recipe-add/recipe-add.component.html @@ -11,7 +11,7 @@ required #nameField="ngModel" > -
+
Name is required.
@@ -28,7 +28,7 @@ #descriptionField="ngModel" >
Characters left: {{ maxDescriptionLength - recipe.description.length }}
-
+
Description is required.
@@ -36,11 +36,8 @@
- +
{{ imageError }}
-
- Image is required. -
@@ -63,8 +60,16 @@ name="quantity_{{ ingredientId }}" min="1" /> + +
+ At least one ingredient is required. +
- + + + diff --git a/src/app/recipe-add/recipe-add.component.scss b/src/app/recipe-add/recipe-add.component.scss index e69de29..cd02af5 100644 --- a/src/app/recipe-add/recipe-add.component.scss +++ b/src/app/recipe-add/recipe-add.component.scss @@ -0,0 +1,4 @@ +.disabled-button { + background-color: grey !important; + cursor: not-allowed !important; +} \ No newline at end of file diff --git a/src/app/recipe-add/recipe-add.component.ts b/src/app/recipe-add/recipe-add.component.ts index 80185cf..23ccd4a 100644 --- a/src/app/recipe-add/recipe-add.component.ts +++ b/src/app/recipe-add/recipe-add.component.ts @@ -29,6 +29,7 @@ export class RecipeAddComponent implements OnInit { selectedIngredient: number | null = null; imageError: string | null = null; maxDescriptionLength: number = 200; // Maximum length for description + isSubmitting: boolean = false; // Flag to prevent multiple submissions constructor( private ingredientService: IngredientService, @@ -66,9 +67,14 @@ export class RecipeAddComponent implements OnInit { addIngredient(): void { if (this.selectedIngredient !== null && !this.ingredientQuantities[this.selectedIngredient]) { this.ingredientQuantities[this.selectedIngredient] = 1; // Default quantity + this.selectedIngredient = null; // Clear the selection } } + removeIngredient(id: number): void { + delete this.ingredientQuantities[id]; + } + getIngredientKeys(): number[] { return Object.keys(this.ingredientQuantities).map(key => parseInt(key, 10)); } @@ -79,15 +85,7 @@ export class RecipeAddComponent implements OnInit { } onSubmit(): void { - if (!this.recipe.name.trim() || !this.recipe.description.trim() || !this.recipe.image) { - alert('Please fill out Name, Description, and Image fields.'); - return; - } - - if (this.recipe.description.length > this.maxDescriptionLength) { - alert(`Description should not exceed ${this.maxDescriptionLength} characters.`); - return; - } + if (this.isSubmitting) return; // Prevent multiple submissions this.recipe.ingredients = this.getIngredientKeys().map( (id) => { @@ -96,10 +94,14 @@ export class RecipeAddComponent implements OnInit { } ); + this.isSubmitting = true; // Set flag to prevent multiple submissions + const recipes = JSON.parse(localStorage.getItem('recipes') || '[]'); this.recipe.id = recipes.length ? recipes[recipes.length - 1].id + 1 : 1; recipes.push(this.recipe); localStorage.setItem('recipes', JSON.stringify(recipes)); - this.router.navigate(['/list']); + this.router.navigate(['/list']).then(() => { + this.isSubmitting = false; // Reset flag after navigation + }); } }