Files
brewman/overlord/src/app/product/product-detail/product-detail.component.ts
2020-11-23 16:42:54 +05:30

133 lines
3.8 KiB
TypeScript

import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { MatDialog } from '@angular/material/dialog';
import { ActivatedRoute, Router } from '@angular/router';
import { Product } from '../../core/product';
import { ProductGroup } from '../../core/product-group';
import { ToasterService } from '../../core/toaster.service';
import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component';
import { ProductService } from '../product.service';
@Component({
selector: 'app-product-detail',
templateUrl: './product-detail.component.html',
styleUrls: ['./product-detail.component.css'],
})
export class ProductDetailComponent implements OnInit, AfterViewInit {
@ViewChild('nameElement', { static: true }) nameElement?: ElementRef;
form: FormGroup;
productGroups: ProductGroup[] = [];
item: Product = new Product();
constructor(
private route: ActivatedRoute,
private router: Router,
private dialog: MatDialog,
private fb: FormBuilder,
private toaster: ToasterService,
private ser: ProductService,
) {
this.form = this.fb.group({
code: { value: '', disabled: true },
name: '',
units: '',
fraction: '',
fractionUnits: '',
productYield: '',
price: '',
salePrice: '',
isPurchased: '',
isSold: '',
isActive: '',
productGroup: '',
});
}
ngOnInit() {
this.route.data.subscribe((value) => {
const data = value as { item: Product; productGroups: ProductGroup[] };
this.productGroups = data.productGroups;
this.showItem(data.item);
});
}
showItem(item: Product) {
this.item = item;
this.form.setValue({
code: this.item.code || '(Auto)',
name: this.item.name || '',
units: this.item.units || '',
fraction: this.item.fraction || '',
fractionUnits: this.item.fractionUnits || '',
productYield: this.item.productYield || '',
price: this.item.price || '',
salePrice: this.item.salePrice || '',
isPurchased: this.item.isPurchased,
isSold: this.item.isSold,
isActive: this.item.isActive,
productGroup: this.item.productGroup.id ? this.item.productGroup.id : '',
});
}
ngAfterViewInit() {
setTimeout(() => {
if (this.nameElement) this.nameElement.nativeElement.focus();
}, 0);
}
save() {
this.ser.saveOrUpdate(this.getItem()).subscribe(
() => {
this.toaster.show('Success', '');
this.router.navigateByUrl('/products');
},
(error) => {
this.toaster.show('Danger', error);
},
);
}
delete() {
this.ser.delete(this.item.id as string).subscribe(
() => {
this.toaster.show('Success', '');
this.router.navigateByUrl('/products');
},
(error) => {
this.toaster.show('Danger', error);
},
);
}
confirmDelete(): void {
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
width: '250px',
data: { title: 'Delete Product?', content: 'Are you sure? This cannot be undone.' },
});
dialogRef.afterClosed().subscribe((result: boolean) => {
if (result) {
this.delete();
}
});
}
getItem(): Product {
const formModel = this.form.value;
this.item.name = formModel.name;
this.item.units = formModel.units;
this.item.fraction = +formModel.fraction;
this.item.fractionUnits = formModel.fractionUnits;
this.item.productYield = +formModel.productYield;
this.item.price = +formModel.price;
this.item.salePrice = +formModel.salePrice;
this.item.isPurchased = formModel.isPurchased;
this.item.isSold = formModel.isSold;
this.item.isActive = formModel.isActive;
this.item.productGroup.id = formModel.productGroup;
return this.item;
}
}