import { Component, Inject, OnInit } from '@angular/core'; import { FormBuilder, 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; constructor( public dialogRef: MatDialogRef, @Inject(MAT_DIALOG_DATA) public data: { item: StockKeepingUnit; isSold: boolean; isPurchased: boolean }, private fb: FormBuilder, ) { this.form = this.fb.group({ units: '', fraction: '', fractionUnits: '', productYield: '', price: '', salePrice: '', }); } ngOnInit() { this.form.setValue({ units: this.data.item.units, fraction: '' + this.data.item.fraction, fractionUnits: this.data.item.fractionUnits, productYield: '' + this.data.item.productYield, price: '' + this.data.item.price, salePrice: '' + this.data.item.salePrice, }); } accept(): void { const formValue = this.form.value; const fraction = +formValue.fraction; if (fraction < 1) { return; } const productYield = +formValue.productYield; if (productYield < 0 || productYield > 1) { return; } const price = +formValue.price; if (price < 0) { return; } const salePrice = +formValue.salePrice; if (salePrice < 0) { return; } this.data.item.units = formValue.units; this.data.item.fraction = fraction; this.data.item.fractionUnits = formValue.fractionUnits; this.data.item.productYield = productYield; this.data.item.price = price; this.data.item.salePrice = salePrice; this.dialogRef.close(this.data.item); } }