|
|
@ -8,6 +8,8 @@ import { Router } from '@angular/router';
|
|
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
|
|
import { MatFormFieldModule } from '@angular/material/form-field';
|
|
|
|
import { MatFormFieldModule } from '@angular/material/form-field';
|
|
|
|
import { MatSelectModule } from '@angular/material/select';
|
|
|
|
import { MatSelectModule } from '@angular/material/select';
|
|
|
|
|
|
|
|
import { RecipeService } from '../services/recipe.service';
|
|
|
|
|
|
|
|
import { IngredientRecipe } from '../models/ingredient-recipe';
|
|
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
@Component({
|
|
|
|
selector: 'app-recipe-add',
|
|
|
|
selector: 'app-recipe-add',
|
|
|
@ -33,6 +35,7 @@ export class RecipeAddComponent implements OnInit {
|
|
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
constructor(
|
|
|
|
private ingredientService: IngredientService,
|
|
|
|
private ingredientService: IngredientService,
|
|
|
|
|
|
|
|
private recipeService: RecipeService,
|
|
|
|
private router: Router
|
|
|
|
private router: Router
|
|
|
|
) {}
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
|
@ -87,19 +90,31 @@ export class RecipeAddComponent implements OnInit {
|
|
|
|
onSubmit(): void {
|
|
|
|
onSubmit(): void {
|
|
|
|
if (this.isSubmitting) return; // Prevent multiple submissions
|
|
|
|
if (this.isSubmitting) return; // Prevent multiple submissions
|
|
|
|
|
|
|
|
|
|
|
|
this.recipe.ingredients = this.getIngredientKeys().map(
|
|
|
|
|
|
|
|
(id) => {
|
|
|
|
|
|
|
|
const ingredient = this.ingredientService.getIngredient(id);
|
|
|
|
|
|
|
|
return new Ingredient(id, ingredient.name);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.isSubmitting = true; // Set flag to prevent multiple submissions
|
|
|
|
this.isSubmitting = true; // Set flag to prevent multiple submissions
|
|
|
|
|
|
|
|
|
|
|
|
const recipes = JSON.parse(localStorage.getItem('recipes') || '[]');
|
|
|
|
// Get the current recipes
|
|
|
|
|
|
|
|
const recipes = this.recipeService.getRecipes();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Set the new recipe ID
|
|
|
|
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);
|
|
|
|
|
|
|
|
localStorage.setItem('recipes', JSON.stringify(recipes));
|
|
|
|
// Add the ingredients with quantities
|
|
|
|
|
|
|
|
this.recipe.ingredients = this.getIngredientKeys().map(id => {
|
|
|
|
|
|
|
|
const ingredient = this.ingredientService.getIngredient(id);
|
|
|
|
|
|
|
|
return new Ingredient(id, ingredient.name);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Save the recipe
|
|
|
|
|
|
|
|
this.recipeService.addRecipe(this.recipe);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Save the ingredient quantities
|
|
|
|
|
|
|
|
this.getIngredientKeys().forEach(id => {
|
|
|
|
|
|
|
|
const quantity = this.ingredientQuantities[id];
|
|
|
|
|
|
|
|
const ingredientRecipe = new IngredientRecipe(id, this.recipe.id, quantity);
|
|
|
|
|
|
|
|
this.recipeService.addIngredientRecipe(ingredientRecipe);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Navigate back to the list
|
|
|
|
this.router.navigate(['/list']).then(() => {
|
|
|
|
this.router.navigate(['/list']).then(() => {
|
|
|
|
this.isSubmitting = false; // Reset flag after navigation
|
|
|
|
this.isSubmitting = false; // Reset flag after navigation
|
|
|
|
});
|
|
|
|
});
|
|
|
|