|
|
@ -4,11 +4,12 @@ import {FormControl, FormGroup, ReactiveFormsModule, FormArray, FormBuilder} fro
|
|
|
|
import { Ingredient } from '../../model/ingredient.model';
|
|
|
|
import { Ingredient } from '../../model/ingredient.model';
|
|
|
|
import { Unity } from '../../model/unity';
|
|
|
|
import { Unity } from '../../model/unity';
|
|
|
|
import { IngredientService } from '../../service/ingredient.service';
|
|
|
|
import { IngredientService } from '../../service/ingredient.service';
|
|
|
|
import {NgFor} from "@angular/common";
|
|
|
|
import {NgFor, NgIf} from "@angular/common";
|
|
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
@Component({
|
|
|
|
selector: 'app-recipe-form',
|
|
|
|
selector: 'app-recipe-form',
|
|
|
|
standalone: true,
|
|
|
|
standalone: true,
|
|
|
|
imports: [ReactiveFormsModule,NgFor],
|
|
|
|
imports: [ReactiveFormsModule, NgFor, NgIf],
|
|
|
|
templateUrl: './recipe-form.component.html',
|
|
|
|
templateUrl: './recipe-form.component.html',
|
|
|
|
styleUrl: './recipe-form.component.css'
|
|
|
|
styleUrl: './recipe-form.component.css'
|
|
|
|
})
|
|
|
|
})
|
|
|
@ -16,6 +17,7 @@ export class RecipeFormComponent {
|
|
|
|
@Output() formSubmitted = new EventEmitter<Recipe>();
|
|
|
|
@Output() formSubmitted = new EventEmitter<Recipe>();
|
|
|
|
ingredientsList: Ingredient[] = [];
|
|
|
|
ingredientsList: Ingredient[] = [];
|
|
|
|
unity = Object.values(Unity);
|
|
|
|
unity = Object.values(Unity);
|
|
|
|
|
|
|
|
imageBase64: string | null = null;
|
|
|
|
|
|
|
|
|
|
|
|
constructor(private formBuilder: FormBuilder, private ingredientService : IngredientService){}
|
|
|
|
constructor(private formBuilder: FormBuilder, private ingredientService : IngredientService){}
|
|
|
|
|
|
|
|
|
|
|
@ -23,13 +25,15 @@ export class RecipeFormComponent {
|
|
|
|
this.ingredientService.getAll().subscribe(ingredients => {
|
|
|
|
this.ingredientService.getAll().subscribe(ingredients => {
|
|
|
|
this.ingredientsList = ingredients;
|
|
|
|
this.ingredientsList = ingredients;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
recipeForm: FormGroup = this.formBuilder.group({
|
|
|
|
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([])
|
|
|
|
ingredients: this.formBuilder.array([this.newIngredient()]),
|
|
|
|
|
|
|
|
image: new FormControl('', {nonNullable: false})
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
get ingredients():FormArray{
|
|
|
|
get ingredients():FormArray{
|
|
|
@ -48,7 +52,25 @@ export class RecipeFormComponent {
|
|
|
|
this.ingredients.push(this.newIngredient());
|
|
|
|
this.ingredients.push(this.newIngredient());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
removeIngredient(index: number) {
|
|
|
|
removeIngredient(index: number) {
|
|
|
|
|
|
|
|
if (this.ingredients.length > 1) {
|
|
|
|
|
|
|
|
|
|
|
|
this.ingredients.removeAt(index);
|
|
|
|
this.ingredients.removeAt(index);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
onFileChange(event: Event) {
|
|
|
|
|
|
|
|
const input = event.target as HTMLInputElement;
|
|
|
|
|
|
|
|
if (input.files && input.files[0]) {
|
|
|
|
|
|
|
|
const file = input.files[0];
|
|
|
|
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
|
|
|
reader.onload = () => {
|
|
|
|
|
|
|
|
this.imageBase64 = reader.result as string;
|
|
|
|
|
|
|
|
this.recipeForm.patchValue({image: this.imageBase64});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
onSubmit() {
|
|
|
|
onSubmit() {
|
|
|
@ -62,11 +84,13 @@ export class RecipeFormComponent {
|
|
|
|
quantity: ingredient.quantity,
|
|
|
|
quantity: ingredient.quantity,
|
|
|
|
unit: ingredient.unit,
|
|
|
|
unit: ingredient.unit,
|
|
|
|
ingredient: this.ingredientsList.find(ing => ing.$name === ingredient.ingredient)
|
|
|
|
ingredient: this.ingredientsList.find(ing => ing.$name === ingredient.ingredient)
|
|
|
|
}))
|
|
|
|
})),
|
|
|
|
|
|
|
|
$image: this.recipeForm.value.image
|
|
|
|
};
|
|
|
|
};
|
|
|
|
this.formSubmitted.emit(recipe);
|
|
|
|
this.formSubmitted.emit(recipe);
|
|
|
|
this.recipeForm.reset()
|
|
|
|
this.recipeForm.reset()
|
|
|
|
this.ingredients.clear()
|
|
|
|
this.ingredients.clear()
|
|
|
|
|
|
|
|
this.addIngredient()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|