|
|
|
@ -3,34 +3,37 @@ import { FormBuilder, FormsModule, ReactiveFormsModule, Validators } from '@angu
|
|
|
|
|
import { Router } from '@angular/router';
|
|
|
|
|
import { Ingredient, IngredientEntry, Recipe } from '../../cookbook/type';
|
|
|
|
|
import { RecipeService } from '../recipe.service';
|
|
|
|
|
import {MatSelectModule} from '@angular/material/select';
|
|
|
|
|
import {MatInputModule} from '@angular/material/input';
|
|
|
|
|
import {MatFormFieldModule} from '@angular/material/form-field';
|
|
|
|
|
import {MatButtonModule} from '@angular/material/button';
|
|
|
|
|
import { MatInputModule } from '@angular/material/input';
|
|
|
|
|
import { MatFormFieldModule } from '@angular/material/form-field';
|
|
|
|
|
import { MatButton } from '@angular/material/button';
|
|
|
|
|
import { MatOption } from '@angular/material/core';
|
|
|
|
|
import { MatSelect } from '@angular/material/select';
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'app-recipe-add',
|
|
|
|
|
standalone: true,
|
|
|
|
|
imports: [ReactiveFormsModule, FormsModule,MatFormFieldModule, MatInputModule, MatSelectModule, MatButtonModule],
|
|
|
|
|
imports: [ReactiveFormsModule, FormsModule, MatFormFieldModule, MatOption, MatSelect, MatInputModule, MatButton, MatFormFieldModule],
|
|
|
|
|
templateUrl: './recipe-add.component.html',
|
|
|
|
|
})
|
|
|
|
|
export class RecipeAddComponent {
|
|
|
|
|
createForm = this.formBuilder.group({
|
|
|
|
|
name: ['', Validators.maxLength(256)],
|
|
|
|
|
description: ['', Validators.maxLength(512)],
|
|
|
|
|
image: '',
|
|
|
|
|
selectedIngredient: '',
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
ingredientEntries: IngredientEntry[] = [];
|
|
|
|
|
ingredients: Ingredient[] = [];
|
|
|
|
|
|
|
|
|
|
selectedFilename: string = '';
|
|
|
|
|
getIngredient(n: number): Ingredient {
|
|
|
|
|
return this.ingredients.find(v => v.id === n)!
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#recipeId: number = -1;
|
|
|
|
|
@Input()
|
|
|
|
|
set recipeId(recipeId: string) {
|
|
|
|
|
set id(recipeId: string) {
|
|
|
|
|
if (recipeId === undefined) return;
|
|
|
|
|
this.#recipeId = parseInt(recipeId);
|
|
|
|
|
const recipe = this.recipes.get(this.#recipeId);
|
|
|
|
@ -43,6 +46,9 @@ export class RecipeAddComponent {
|
|
|
|
|
description: recipe.description,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
get recipeId() {
|
|
|
|
|
return this.#recipeId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constructor(private formBuilder: FormBuilder, private recipes: RecipeService, private router: Router) {
|
|
|
|
|
this.ingredients = this.recipes.getAllIngredients();
|
|
|
|
@ -56,7 +62,7 @@ export class RecipeAddComponent {
|
|
|
|
|
const partial: Omit<Recipe, 'id'> = {
|
|
|
|
|
name: value.name!,
|
|
|
|
|
description: value.description!,
|
|
|
|
|
image: '',
|
|
|
|
|
image: value.image ?? '',
|
|
|
|
|
ingredients: this.ingredientEntries,
|
|
|
|
|
};
|
|
|
|
|
if (this.#recipeId === -1) {
|
|
|
|
@ -76,16 +82,26 @@ export class RecipeAddComponent {
|
|
|
|
|
}
|
|
|
|
|
const id = parseInt(value.selectedIngredient!);
|
|
|
|
|
if (this.ingredientEntries.find((ingredient) => ingredient.idIngredient === id)) {
|
|
|
|
|
console.log("oh");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
console.log("ah");
|
|
|
|
|
console.log(this.ingredients.find(v => v.id === id)?.name)
|
|
|
|
|
console.log(this.ingredients);
|
|
|
|
|
this.ingredientEntries.push({
|
|
|
|
|
idIngredient: id,
|
|
|
|
|
idRecipe: -1,
|
|
|
|
|
quantity: 1,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onFileSelected(event: Event): void {
|
|
|
|
|
const file = (event.target as HTMLInputElement).files![0];
|
|
|
|
|
if (file) {
|
|
|
|
|
this.selectedFilename = file.name;
|
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
reader.onload = (event) => {
|
|
|
|
|
this.createForm.patchValue({
|
|
|
|
|
image: event.target!.result?.toString()
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
reader.readAsDataURL(file);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|