brewman/overlord/src/app/product/product-detail/product-detail-dialog.component.ts

66 lines
1.8 KiB
TypeScript
Raw Normal View History

import { Component, Inject, OnInit } from '@angular/core';
2022-07-11 14:42:38 +00:00
import { UntypedFormBuilder, UntypedFormGroup } 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 {
2022-07-11 14:42:38 +00:00
form: UntypedFormGroup;
constructor(
public dialogRef: MatDialogRef<ProductDetailDialogComponent>,
@Inject(MAT_DIALOG_DATA)
public data: { item: StockKeepingUnit; isSold: boolean; isPurchased: boolean },
2022-07-11 14:42:38 +00:00
private fb: UntypedFormBuilder,
) {
this.form = this.fb.group({
units: '',
fraction: '',
productYield: '',
costPrice: '',
salePrice: '',
});
}
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;
if (fraction < 1) {
return;
}
const productYield = +formValue.productYield;
if (productYield < 0 || productYield > 1) {
return;
}
const costPrice = +formValue.costPrice;
if (costPrice < 0) {
return;
}
const salePrice = +formValue.salePrice;
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);
}
}