Merge branch 'refs/heads/formAdvanced' into formFront

# Conflicts:
#	daidokoro/src/app/component/recipe-list/recipe-list.component.html
formFront
Dorian HODIN 10 months ago
commit 7cc4820475

@ -2,12 +2,32 @@
<label for="id">ID: </label> <label for="id">ID: </label>
<input id="id" type="number" formControlName="id"> <input id="id" type="number" formControlName="id">
<label for="name">name: </label> <label for="name">Name: </label>
<input id="name" type="text" formControlName="name"> <input id="name" type="text" formControlName="name">
<label for="description">description: </label> <label for="description">Description: </label>
<input id="description" type="text" formControlName="description"> <input id="description" type="text" formControlName="description">
<div formArrayName="ingredients">
<div *ngFor="let ingredient of ingredients.controls; let i=index" [formGroupName]="i">
<label for="ingredient-quantity">Quantity:</label>
<input id="ingredient-quantity" type="number" formControlName="quantity">
<label for="ingredient-unit">Unit:</label>
<select id="ingredient-unit" formControlName="unit">
<option *ngFor="let unit of unity" [value]="unit">{{ unit }}</option>
</select>
<label for="ingredient-name">Ingredient:</label>
<select id="ingredient-name" formControlName="ingredient">
<option *ngFor="let ingredient of ingredientsList" [value]="ingredient.$name">{{ ingredient.$name }}</option>
</select>
<button type="button" (click)="removeIngredient(i)">Remove</button>
</div>
</div>
<button type="button" (click)="addIngredient()">Add Ingredient</button>
<p>Complete the form to enable the button.</p> <p>Complete the form to enable the button.</p>
<button type="submit" [disabled]="!recipeForm.valid">Submit</button> <button type="submit" [disabled]="!recipeForm.valid">Submit</button>

@ -1,24 +1,55 @@
import {Component, EventEmitter, Output} from '@angular/core'; import {Component, EventEmitter, Output} from '@angular/core';
import {Recipe} from "../../model/recipe.model"; import {Recipe} from "../../model/recipe.model";
import {FormControl, FormGroup, ReactiveFormsModule} from "@angular/forms"; import {FormControl, FormGroup, ReactiveFormsModule, FormArray, FormBuilder} from "@angular/forms";
import { Ingredient } from '../../model/ingredient.model';
import { Unity } from '../../model/unity';
import { IngredientService } from '../../service/ingredient.service';
import {NgFor} from "@angular/common";
@Component({ @Component({
selector: 'app-recipe-form', selector: 'app-recipe-form',
standalone: true, standalone: true,
imports: [ReactiveFormsModule], imports: [ReactiveFormsModule,NgFor],
templateUrl: './recipe-form.component.html', templateUrl: './recipe-form.component.html',
styleUrl: './recipe-form.component.css' styleUrl: './recipe-form.component.css'
}) })
export class RecipeFormComponent { export class RecipeFormComponent {
@Output() formSubmitted = new EventEmitter<Recipe>(); @Output() formSubmitted = new EventEmitter<Recipe>();
ingredientsList: Ingredient[] = [];
unity = Object.values(Unity);
constructor(private formBuilder: FormBuilder, private ingredientService : IngredientService){}
recipeForm = new FormGroup({ ngOnInit(): void {
this.ingredientService.getAll().subscribe(ingredients => {
this.ingredientsList = ingredients;
});
}
recipeForm: FormGroup = this.formBuilder.group({
id: new FormControl('', { nonNullable: true }), id: new FormControl('', { nonNullable: true }),
name: new FormControl('', { nonNullable: true }), name: new FormControl('', { nonNullable: true }),
description: new FormControl('', { nonNullable: true }), description: new FormControl('', { nonNullable: true }),
ingredients: this.formBuilder.array([])
});
get ingredients():FormArray{
return this.recipeForm.get('ingredients') as FormArray;
}
newIngredient(): FormGroup{
return this.formBuilder.group({
quantity: new FormControl(0, { nonNullable: true }),
unit: new FormControl('', { nonNullable: true }),
ingredient: new FormControl('', { nonNullable: true })
}); });
}
addIngredient() {
this.ingredients.push(this.newIngredient());
}
removeIngredient(index: number) {
this.ingredients.removeAt(index);
}
onSubmit() { onSubmit() {
if(this.recipeForm.valid){ if(this.recipeForm.valid){
@ -27,11 +58,18 @@ export class RecipeFormComponent {
$name: this.recipeForm.value.name!, $name: this.recipeForm.value.name!,
$description: this.recipeForm.value.description!, $description: this.recipeForm.value.description!,
$createdAt: new Date(), $createdAt: new Date(),
$ingredients: [] $ingredients: this.recipeForm.value.ingredients!.map((ingredient : any) => ({
quantity: ingredient.quantity,
unit: ingredient.unit,
ingredient: this.ingredientsList.find(ing => ing.$name === ingredient.ingredient)
}))
}; };
this.formSubmitted.emit(recipe); this.formSubmitted.emit(recipe);
this.recipeForm.reset() this.recipeForm.reset()
this.ingredients.clear()
} }
} }
} }

@ -1,6 +1,7 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import {Ingredient} from "../model/ingredient.model"; import {Ingredient} from "../model/ingredient.model";
import {$INGREDIENTS} from "../data/ingredient.stub"; import {$INGREDIENTS} from "../data/ingredient.stub";
import {Observable, of} from "rxjs";
@Injectable({ @Injectable({
@ -8,9 +9,7 @@ import {$INGREDIENTS} from "../data/ingredient.stub";
}) })
export class IngredientService { export class IngredientService {
constructor() { } getAll() : Observable<Ingredient[]> {
return of($INGREDIENTS);
getAll() : Ingredient[] {
return $INGREDIENTS;
} }
} }

Loading…
Cancel
Save