|
|
@ -3,29 +3,37 @@ import { FormBuilder, FormsModule, ReactiveFormsModule, Validators } from '@angu
|
|
|
|
import { Router } from '@angular/router';
|
|
|
|
import { Router } from '@angular/router';
|
|
|
|
import { Ingredient, IngredientEntry, Recipe } from '../../cookbook/type';
|
|
|
|
import { Ingredient, IngredientEntry, Recipe } from '../../cookbook/type';
|
|
|
|
import { RecipeService } from '../recipe.service';
|
|
|
|
import { RecipeService } from '../recipe.service';
|
|
|
|
|
|
|
|
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({
|
|
|
|
@Component({
|
|
|
|
selector: 'app-recipe-add',
|
|
|
|
selector: 'app-recipe-add',
|
|
|
|
standalone: true,
|
|
|
|
standalone: true,
|
|
|
|
imports: [ReactiveFormsModule, FormsModule],
|
|
|
|
imports: [ReactiveFormsModule, FormsModule, MatFormFieldModule, MatOption, MatSelect, MatInputModule, MatButton, MatFormFieldModule],
|
|
|
|
templateUrl: './recipe-add.component.html',
|
|
|
|
templateUrl: './recipe-add.component.html',
|
|
|
|
})
|
|
|
|
})
|
|
|
|
export class RecipeAddComponent {
|
|
|
|
export class RecipeAddComponent {
|
|
|
|
createForm = this.formBuilder.group({
|
|
|
|
createForm = this.formBuilder.group({
|
|
|
|
name: ['', Validators.maxLength(256)],
|
|
|
|
name: ['', Validators.maxLength(256)],
|
|
|
|
description: ['', Validators.maxLength(512)],
|
|
|
|
description: ['', Validators.maxLength(512)],
|
|
|
|
|
|
|
|
image: '',
|
|
|
|
selectedIngredient: '',
|
|
|
|
selectedIngredient: '',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
ingredientEntries: IngredientEntry[] = [];
|
|
|
|
ingredientEntries: IngredientEntry[] = [];
|
|
|
|
ingredients: Ingredient[] = [{
|
|
|
|
ingredients: Ingredient[] = [];
|
|
|
|
id: 1,
|
|
|
|
|
|
|
|
name: 'Paprika',
|
|
|
|
selectedFilename: string = '';
|
|
|
|
}];
|
|
|
|
getIngredient(n: number): Ingredient {
|
|
|
|
|
|
|
|
return this.ingredients.find(v => v.id === n)!
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#recipeId: number = -1;
|
|
|
|
#recipeId: number = -1;
|
|
|
|
@Input()
|
|
|
|
@Input()
|
|
|
|
set recipeId(recipeId: string) {
|
|
|
|
set id(recipeId: string) {
|
|
|
|
if (recipeId === undefined) return;
|
|
|
|
if (recipeId === undefined) return;
|
|
|
|
this.#recipeId = parseInt(recipeId);
|
|
|
|
this.#recipeId = parseInt(recipeId);
|
|
|
|
const recipe = this.recipes.get(this.#recipeId);
|
|
|
|
const recipe = this.recipes.get(this.#recipeId);
|
|
|
@ -38,8 +46,13 @@ export class RecipeAddComponent {
|
|
|
|
description: recipe.description,
|
|
|
|
description: recipe.description,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get recipeId() {
|
|
|
|
|
|
|
|
return this.#recipeId;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
constructor(private formBuilder: FormBuilder, private recipes: RecipeService, private router: Router) {}
|
|
|
|
constructor(private formBuilder: FormBuilder, private recipes: RecipeService, private router: Router) {
|
|
|
|
|
|
|
|
this.ingredients = this.recipes.getAllIngredients();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
onSubmit(): void {
|
|
|
|
onSubmit(): void {
|
|
|
|
if (this.createForm.invalid) {
|
|
|
|
if (this.createForm.invalid) {
|
|
|
@ -49,7 +62,7 @@ export class RecipeAddComponent {
|
|
|
|
const partial: Omit<Recipe, 'id'> = {
|
|
|
|
const partial: Omit<Recipe, 'id'> = {
|
|
|
|
name: value.name!,
|
|
|
|
name: value.name!,
|
|
|
|
description: value.description!,
|
|
|
|
description: value.description!,
|
|
|
|
image: '',
|
|
|
|
image: value.image ?? '',
|
|
|
|
ingredients: this.ingredientEntries,
|
|
|
|
ingredients: this.ingredientEntries,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
if (this.#recipeId === -1) {
|
|
|
|
if (this.#recipeId === -1) {
|
|
|
@ -62,6 +75,8 @@ export class RecipeAddComponent {
|
|
|
|
|
|
|
|
|
|
|
|
onAddIngredient(): void {
|
|
|
|
onAddIngredient(): void {
|
|
|
|
const value = this.createForm.value;
|
|
|
|
const value = this.createForm.value;
|
|
|
|
|
|
|
|
console.log(value);
|
|
|
|
|
|
|
|
|
|
|
|
if (!value.selectedIngredient) {
|
|
|
|
if (!value.selectedIngredient) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -75,4 +90,18 @@ export class RecipeAddComponent {
|
|
|
|
quantity: 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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|