brewman/overlord/src/app/product-group/product-group-detail/product-group-detail.compon...

74 lines
1.8 KiB
TypeScript
Raw Normal View History

import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {ProductGroupService} from '../product-group.service';
import {ProductGroup} from '../../core/product-group';
import {ActivatedRoute, Router} from '@angular/router';
import {ToasterService} from '../../core/toaster.service';
import {FormBuilder, FormGroup} from '@angular/forms';
@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: FormGroup;
item: ProductGroup;
constructor(
private route: ActivatedRoute,
private router: Router,
private fb: FormBuilder,
private toaster: ToasterService,
private ser: ProductGroupService
) {
this.createForm();
}
createForm() {
this.form = this.fb.group({
name: ''
});
}
ngOnInit() {
this.route.data
.subscribe((data: { item: ProductGroup }) => {
this.showItem(data.item);
});
}
showItem(item: ProductGroup) {
this.item = item;
this.form.setValue({
name: this.item.name,
});
}
ngAfterViewInit() {
setTimeout(() => {
this.nameElement.nativeElement.focus();
}, 0);
}
save() {
this.ser.saveOrUpdate(this.getItem())
.subscribe(
(result) => {
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;
}
}