import { CdkScrollableModule } from '@angular/cdk/scrolling'; import { Component, inject, OnInit } from '@angular/core'; import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms'; import { MatButtonModule } from '@angular/material/button'; import { MatCheckboxModule } from '@angular/material/checkbox'; import { MAT_DIALOG_DATA, MatDialogModule, MatDialogRef } from '@angular/material/dialog'; import { MatFormFieldModule } from '@angular/material/form-field'; import { MatInputModule } from '@angular/material/input'; import { MatSelectModule } from '@angular/material/select'; import { MenuCategory } from '../../core/menu-category'; import { StockKeepingUnit } from '../../core/product'; @Component({ selector: 'app-product-detail-dialog', templateUrl: './product-detail-dialog.component.html', styleUrls: ['./product-detail-dialog.component.css'], standalone: true, imports: [ CdkScrollableModule, MatButtonModule, MatCheckboxModule, MatDialogModule, MatFormFieldModule, MatInputModule, MatSelectModule, ReactiveFormsModule, ], }) export class ProductDetailDialogComponent implements OnInit { private readonly dialogRef = inject>(MatDialogRef); menuCategories: MenuCategory[] = []; data = inject<{ item: StockKeepingUnit; units: string; fraction: number; productYield: number; costPrice: number; salePrice: number; menuCategory: string; hasHappyHour: boolean; isNotAvailable: boolean; menuCategories: MenuCategory[]; }>(MAT_DIALOG_DATA); form: FormGroup<{ units: FormControl; fraction: FormControl; productYield: FormControl; costPrice: FormControl; salePrice: FormControl; menuCategory: FormControl; hasHappyHour: FormControl; isNotAvailable: FormControl; }>; constructor() { this.form = new FormGroup({ units: new FormControl('', { nonNullable: true }), fraction: new FormControl(1, { nonNullable: true }), productYield: new FormControl(1, { nonNullable: true }), costPrice: new FormControl(0, { nonNullable: true }), salePrice: new FormControl(0, { nonNullable: true }), menuCategory: new FormControl('', { nonNullable: true }), hasHappyHour: new FormControl(false, { nonNullable: true }), isNotAvailable: new FormControl(false, { nonNullable: true }), }); } ngOnInit(): void { // Populate from the row being edited this.form.setValue({ units: this.data.item.units ?? '', fraction: this.data.item.fraction ?? 1, productYield: this.data.item.productYield ?? 1, costPrice: this.data.item.costPrice ?? 0, salePrice: this.data.item.salePrice ?? 0, menuCategory: this.data.item.menuCategory?.id ?? '', hasHappyHour: this.data.item.hasHappyHour ?? false, isNotAvailable: this.data.item.isNotAvailable ?? false, }); this.menuCategories = this.data.menuCategories; } accept(): void { const formValue = this.form.value; const fraction = formValue.fraction ?? 0; if (fraction < 1) { return; } const productYield = formValue.productYield ?? 0; if (productYield < 0 || productYield > 1) { return; } const costPrice = formValue.costPrice ?? 0; if (costPrice < 0) { return; } const salePrice = formValue.salePrice ?? 0; if (salePrice < 0) { return; } this.data.item.units = (formValue.units ?? '').trim(); this.data.item.fraction = fraction; this.data.item.productYield = productYield; this.data.item.costPrice = costPrice; this.data.item.salePrice = salePrice; this.data.item.hasHappyHour = formValue.hasHappyHour ?? false; this.data.item.isNotAvailable = formValue.isNotAvailable ?? false; if (this.data.item.menuCategory === null || this.data.item.menuCategory === undefined) { this.data.item.menuCategory = new MenuCategory(); } this.data.item.menuCategory.id = formValue.menuCategory ?? ''; this.data.item.menuCategory.name = this.menuCategories.find((mc) => mc.id === formValue.menuCategory)?.name ?? ''; this.dialogRef.close(this.data.item); } }