72 lines
1.8 KiB
TypeScript
72 lines
1.8 KiB
TypeScript
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
|
|
import { UntypedFormBuilder, UntypedFormGroup } from '@angular/forms';
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
|
|
import { ProductGroup } from '../../core/product-group';
|
|
import { ToasterService } from '../../core/toaster.service';
|
|
import { ProductGroupService } from '../product-group.service';
|
|
|
|
@Component({
|
|
selector: 'app-product-group-detail',
|
|
templateUrl: './product-group-detail.component.html',
|
|
styleUrls: ['./product-group-detail.component.css'],
|
|
})
|
|
export class ProductGroupDetailComponent implements OnInit, AfterViewInit {
|
|
@ViewChild('nameElement', { static: true }) nameElement?: ElementRef;
|
|
form: UntypedFormGroup;
|
|
item: ProductGroup = new ProductGroup();
|
|
|
|
constructor(
|
|
private route: ActivatedRoute,
|
|
private router: Router,
|
|
private fb: UntypedFormBuilder,
|
|
private toaster: ToasterService,
|
|
private ser: ProductGroupService,
|
|
) {
|
|
this.form = this.fb.group({
|
|
name: '',
|
|
});
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.route.data.subscribe((value) => {
|
|
const data = value as { item: ProductGroup };
|
|
|
|
this.showItem(data.item);
|
|
});
|
|
}
|
|
|
|
showItem(item: ProductGroup) {
|
|
this.item = item;
|
|
this.form.setValue({
|
|
name: this.item.name,
|
|
});
|
|
}
|
|
|
|
ngAfterViewInit() {
|
|
setTimeout(() => {
|
|
if (this.nameElement) {
|
|
this.nameElement.nativeElement.focus();
|
|
}
|
|
}, 0);
|
|
}
|
|
|
|
save() {
|
|
this.ser.saveOrUpdate(this.getItem()).subscribe(
|
|
() => {
|
|
this.toaster.show('Success', '');
|
|
this.router.navigateByUrl('/product-groups');
|
|
},
|
|
(error) => {
|
|
this.toaster.show('Danger', error);
|
|
},
|
|
);
|
|
}
|
|
|
|
getItem(): ProductGroup {
|
|
const formModel = this.form.value;
|
|
this.item.name = formModel.name;
|
|
return this.item;
|
|
}
|
|
}
|