Qunatity of ingredient in recipe

master
Matis MAZINGUE 10 months ago
parent 2b8834ee47
commit 7ea9ff6295

@ -1,5 +1,4 @@
<form (ngSubmit)="onSubmit(); recipeForm.ngSubmit.emit()" #recipeForm="ngForm" class="ng-submitted"> <form (ngSubmit)="onSubmit(); recipeForm.ngSubmit.emit()" #recipeForm="ngForm" class="ng-submitted">
<div class="form-group my-2 col-md-4"> <div class="form-group my-2 col-md-4">
<label for="name">Name</label> <label for="name">Name</label>
<input class="form-control" <input class="form-control"
@ -9,8 +8,7 @@
[(ngModel)]="recipe.name" [(ngModel)]="recipe.name"
name="name" name="name"
required required
#nameField="ngModel" #nameField="ngModel">
>
<div *ngIf="nameField.invalid && (nameField.dirty || nameField.touched || recipeForm.submitted)" class="text-danger"> <div *ngIf="nameField.invalid && (nameField.dirty || nameField.touched || recipeForm.submitted)" class="text-danger">
<div *ngIf="nameField.errors && nameField.errors['required']">Name is required.</div> <div *ngIf="nameField.errors && nameField.errors['required']">Name is required.</div>
</div> </div>
@ -25,8 +23,7 @@
required required
rows="2" rows="2"
maxlength="{{ maxDescriptionLength }}" maxlength="{{ maxDescriptionLength }}"
#descriptionField="ngModel" #descriptionField="ngModel"></textarea>
></textarea>
<div class="text-muted text-end">Characters left: {{ maxDescriptionLength - recipe.description.length }}</div> <div class="text-muted text-end">Characters left: {{ maxDescriptionLength - recipe.description.length }}</div>
<div *ngIf="descriptionField.invalid && (descriptionField.dirty || descriptionField.touched || recipeForm.submitted)" class="text-danger"> <div *ngIf="descriptionField.invalid && (descriptionField.dirty || descriptionField.touched || recipeForm.submitted)" class="text-danger">
<div *ngIf="descriptionField.errors && descriptionField.errors['required']">Description is required.</div> <div *ngIf="descriptionField.errors && descriptionField.errors['required']">Description is required.</div>
@ -58,8 +55,7 @@
type="number" type="number"
[(ngModel)]="ingredientQuantities[ingredientId]" [(ngModel)]="ingredientQuantities[ingredientId]"
name="quantity_{{ ingredientId }}" name="quantity_{{ ingredientId }}"
min="1" min="1" />
/>
<button mat-raised-button color="warn" type="button" (click)="removeIngredient(ingredientId)">Remove</button> <button mat-raised-button color="warn" type="button" (click)="removeIngredient(ingredientId)">Remove</button>
</div> </div>
</div> </div>
@ -67,9 +63,7 @@
At least one ingredient is required. At least one ingredient is required.
</div> </div>
<!-- Bouton d'ajout de recette -->
<button mat-raised-button type="submit" [disabled]="!recipeForm.form.valid || getIngredientKeys().length === 0" [ngClass]="{'disabled-button': getIngredientKeys().length === 0}"> <button mat-raised-button type="submit" [disabled]="!recipeForm.form.valid || getIngredientKeys().length === 0" [ngClass]="{'disabled-button': getIngredientKeys().length === 0}">
Add Recipe Add Recipe
</button> </button>
</form> </form>

@ -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
}); });

@ -2,6 +2,7 @@ import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router'; import { ActivatedRoute } from '@angular/router';
import { Recipe } from '../models/recipe'; import { Recipe } from '../models/recipe';
import { IngredientRecipe } from '../models/ingredient-recipe'; import { IngredientRecipe } from '../models/ingredient-recipe';
import { RecipeService } from '../services/recipe.service';
import { CommonModule } from '@angular/common'; import { CommonModule } from '@angular/common';
import { MatButtonModule } from '@angular/material/button'; import { MatButtonModule } from '@angular/material/button';
import { MatCardModule } from '@angular/material/card'; import { MatCardModule } from '@angular/material/card';
@ -17,32 +18,24 @@ export class RecipeComponent implements OnInit {
recipe: Recipe | undefined; recipe: Recipe | undefined;
ingredientRecipes: IngredientRecipe[] = []; ingredientRecipes: IngredientRecipe[] = [];
constructor(private route: ActivatedRoute) {} constructor(
private route: ActivatedRoute,
private recipeService: RecipeService
) {}
ngOnInit(): void { ngOnInit(): void {
const recipeId = this.route.snapshot.paramMap.get('id'); const recipeId = this.route.snapshot.paramMap.get('id');
const recipes: Recipe[] = JSON.parse( this.recipe = this.recipeService.getRecipes().find(
localStorage.getItem('recipes') || '[]' (r) => r.id.toString() === recipeId
); );
this.recipe = recipes.find((r) => r.id.toString() === recipeId);
if (this.recipe) { if (this.recipe) {
this.ingredientRecipes = this.getIngredientRecipes(this.recipe.id); this.ingredientRecipes = this.recipeService.getIngredientRecipes(this.recipe.id);
} }
} }
getIngredientRecipes(recipeId: number): IngredientRecipe[] {
const ingredientRecipes: IngredientRecipe[] = JSON.parse(
localStorage.getItem('ingredientRecipes') || '[]'
);
return ingredientRecipes.filter((ir) => ir.idRecipe === recipeId);
}
getIngredientQuantity(ingredientId: number): number { getIngredientQuantity(ingredientId: number): number {
const ingredientRecipe = this.ingredientRecipes.find( return this.recipeService.getIngredientQuantity(ingredientId, this.ingredientRecipes);
(ir) => ir.idIngredient === ingredientId
);
return ingredientRecipe ? ingredientRecipe.quantity : 0;
} }
navBack(): void { navBack(): void {

@ -1,11 +1,13 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Recipe } from '../models/recipe'; import { Recipe } from '../models/recipe';
import { IngredientRecipe } from '../models/ingredient-recipe';
@Injectable({ @Injectable({
providedIn: 'root', providedIn: 'root',
}) })
export class RecipeService { export class RecipeService {
private localStorageKey = 'recipes'; private localStorageKey = 'recipes';
private ingredientRecipeKey = 'ingredientRecipes';
getRecipes(): Recipe[] { getRecipes(): Recipe[] {
const recipesJson = localStorage.getItem(this.localStorageKey); const recipesJson = localStorage.getItem(this.localStorageKey);
@ -17,4 +19,24 @@ export class RecipeService {
recipes.push(recipe); recipes.push(recipe);
localStorage.setItem(this.localStorageKey, JSON.stringify(recipes)); localStorage.setItem(this.localStorageKey, JSON.stringify(recipes));
} }
getIngredientRecipes(recipeId: number): IngredientRecipe[] {
const ingredientRecipes: IngredientRecipe[] = JSON.parse(
localStorage.getItem(this.ingredientRecipeKey) || '[]'
);
return ingredientRecipes.filter((ir) => ir.idRecipe === recipeId);
}
addIngredientRecipe(ingredientRecipe: IngredientRecipe): void {
const ingredientRecipes = this.getIngredientRecipes(ingredientRecipe.idRecipe);
ingredientRecipes.push(ingredientRecipe);
localStorage.setItem(this.ingredientRecipeKey, JSON.stringify(ingredientRecipes));
}
getIngredientQuantity(ingredientId: number, ingredientRecipes: IngredientRecipe[]): number {
const ingredientRecipe = ingredientRecipes.find(
(ir) => ir.idIngredient === ingredientId
);
return ingredientRecipe ? ingredientRecipe.quantity : 0;
}
} }

Loading…
Cancel
Save