import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core'; import { ToasterService } from '../../core/toaster.service'; import { ActivatedRoute, Router } from '@angular/router'; import { ProductService } from '../product.service'; import { Product } from '../../core/product'; import { MenuCategory } from '../../core/menu-category'; import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component'; import { MatDialog } from '@angular/material/dialog'; import { FormBuilder, FormGroup } from '@angular/forms'; import { SaleCategory } from "../../core/sale-category"; @Component({ selector: 'app-product-detail', templateUrl: './product-detail.component.html', styleUrls: ['./product-detail.component.css'] }) export class ProductDetailComponent implements OnInit, AfterViewInit { @ViewChild('name', { static: true }) nameElement: ElementRef; form: FormGroup; menuCategories: MenuCategory[]; saleCategories: SaleCategory[]; item: Product; constructor( private route: ActivatedRoute, private router: Router, private dialog: MatDialog, private fb: FormBuilder, private toaster: ToasterService, private ser: ProductService ) { this.createForm(); } createForm() { this.form = this.fb.group({ code: {value: '', disabled: true}, name: '', units: '', menuCategory: '', saleCategory: '', price: '', hasHappyHour: '', isNotAvailable: '', quantity: '', isActive: '' }); } ngOnInit() { this.route.data .subscribe((data: { item: Product, menuCategories: MenuCategory[], saleCategories: SaleCategory[] }) => { this.menuCategories = data.menuCategories; this.saleCategories = data.saleCategories; 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 || '', menuCategory: this.item.menuCategory.id ? this.item.menuCategory.id : '', saleCategory: this.item.saleCategory.id ? this.item.saleCategory.id : '', price: this.item.price || '', hasHappyHour: this.item.hasHappyHour, isNotAvailable: this.item.isNotAvailable, quantity: this.item.quantity || '', isActive: this.item.isActive }); } ngAfterViewInit() { setTimeout(() => { this.nameElement.nativeElement.focus(); }, 0); } save() { this.ser.saveOrUpdate(this.getItem()) .subscribe( (result) => { this.toaster.show('Success', ''); this.router.navigateByUrl('/products'); }, (error) => { this.toaster.show('Danger', error.error); } ); } delete() { this.ser.delete(this.item.id) .subscribe( (result) => { this.toaster.show('Success', ''); this.router.navigateByUrl('/products'); }, (error) => { this.toaster.show('Danger', error.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.menuCategory.id = formModel.menuCategory; this.item.saleCategory.id = formModel.saleCategory; this.item.price = +formModel.price; this.item.hasHappyHour = formModel.hasHappyHour; this.item.isNotAvailable = formModel.isNotAvailable; this.item.quantity = +formModel.quantity; this.item.isActive = formModel.isActive; return this.item; } }