|
|
@ -29,6 +29,7 @@ export class RecipeAddComponent implements OnInit {
|
|
|
|
selectedIngredient: number | null = null;
|
|
|
|
selectedIngredient: number | null = null;
|
|
|
|
imageError: string | null = null;
|
|
|
|
imageError: string | null = null;
|
|
|
|
maxDescriptionLength: number = 200; // Maximum length for description
|
|
|
|
maxDescriptionLength: number = 200; // Maximum length for description
|
|
|
|
|
|
|
|
isSubmitting: boolean = false; // Flag to prevent multiple submissions
|
|
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
constructor(
|
|
|
|
private ingredientService: IngredientService,
|
|
|
|
private ingredientService: IngredientService,
|
|
|
@ -66,9 +67,14 @@ export class RecipeAddComponent implements OnInit {
|
|
|
|
addIngredient(): void {
|
|
|
|
addIngredient(): void {
|
|
|
|
if (this.selectedIngredient !== null && !this.ingredientQuantities[this.selectedIngredient]) {
|
|
|
|
if (this.selectedIngredient !== null && !this.ingredientQuantities[this.selectedIngredient]) {
|
|
|
|
this.ingredientQuantities[this.selectedIngredient] = 1; // Default quantity
|
|
|
|
this.ingredientQuantities[this.selectedIngredient] = 1; // Default quantity
|
|
|
|
|
|
|
|
this.selectedIngredient = null; // Clear the selection
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
removeIngredient(id: number): void {
|
|
|
|
|
|
|
|
delete this.ingredientQuantities[id];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
getIngredientKeys(): number[] {
|
|
|
|
getIngredientKeys(): number[] {
|
|
|
|
return Object.keys(this.ingredientQuantities).map(key => parseInt(key, 10));
|
|
|
|
return Object.keys(this.ingredientQuantities).map(key => parseInt(key, 10));
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -79,15 +85,7 @@ export class RecipeAddComponent implements OnInit {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
onSubmit(): void {
|
|
|
|
onSubmit(): void {
|
|
|
|
if (!this.recipe.name.trim() || !this.recipe.description.trim() || !this.recipe.image) {
|
|
|
|
if (this.isSubmitting) return; // Prevent multiple submissions
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.recipe.ingredients = this.getIngredientKeys().map(
|
|
|
|
this.recipe.ingredients = this.getIngredientKeys().map(
|
|
|
|
(id) => {
|
|
|
|
(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') || '[]');
|
|
|
|
const recipes = JSON.parse(localStorage.getItem('recipes') || '[]');
|
|
|
|
this.recipe.id = recipes.length ? recipes[recipes.length - 1].id + 1 : 1;
|
|
|
|
this.recipe.id = recipes.length ? recipes[recipes.length - 1].id + 1 : 1;
|
|
|
|
recipes.push(this.recipe);
|
|
|
|
recipes.push(this.recipe);
|
|
|
|
localStorage.setItem('recipes', JSON.stringify(recipes));
|
|
|
|
localStorage.setItem('recipes', JSON.stringify(recipes));
|
|
|
|
this.router.navigate(['/list']);
|
|
|
|
this.router.navigate(['/list']).then(() => {
|
|
|
|
|
|
|
|
this.isSubmitting = false; // Reset flag after navigation
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|