import { Component, Inject, OnInit } from '@angular/core'; import { FormControl, FormGroup } from '@angular/forms'; import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; import { StockKeepingUnit } from '../../core/product'; @Component({ selector: 'app-journal-dialog', templateUrl: './product-detail-dialog.component.html', styleUrls: ['./product-detail-dialog.component.css'], }) export class ProductDetailDialogComponent implements OnInit { form: FormGroup<{ units: FormControl; fraction: FormControl; productYield: FormControl; costPrice: FormControl; salePrice: FormControl; }>; constructor( public dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public data: { item: StockKeepingUnit; isSold: boolean; isPurchased: boolean }, ) { 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 }), }); } ngOnInit() { this.form.setValue({ units: this.data.item.units, fraction: this.data.item.fraction, productYield: this.data.item.productYield, costPrice: this.data.item.costPrice, salePrice: this.data.item.salePrice, }); } 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 ?? ''; this.data.item.fraction = fraction; this.data.item.productYield = productYield; this.data.item.costPrice = costPrice; this.data.item.salePrice = salePrice; this.dialogRef.close(this.data.item); } }