This commit is contained in:
Amritanshu
2019-06-15 23:09:43 +05:30
parent 3dcf1c4c42
commit 459ab244ff
70 changed files with 2140 additions and 103 deletions

View File

@ -0,0 +1,76 @@
import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {TaxService} from '../tax.service';
import {Tax} from '../../core/tax';
import {ActivatedRoute, Router} from '@angular/router';
import {ToasterService} from '../../core/toaster.service';
import {FormBuilder, FormGroup} from '@angular/forms';
@Component({
selector: 'app-tax-detail',
templateUrl: './tax-detail.component.html',
styleUrls: ['./tax-detail.component.css']
})
export class TaxDetailComponent implements OnInit, AfterViewInit {
@ViewChild('nameElement', { static: true }) nameElement: ElementRef;
form: FormGroup;
item: Tax;
constructor(
private route: ActivatedRoute,
private router: Router,
private fb: FormBuilder,
private toaster: ToasterService,
private ser: TaxService
) {
this.createForm();
}
createForm() {
this.form = this.fb.group({
name: '',
rate: ''
});
}
ngOnInit() {
this.route.data
.subscribe((data: { item: Tax }) => {
this.showItem(data.item);
});
}
showItem(item: Tax) {
this.item = item;
this.form.setValue({
name: this.item.name || '',
rate: this.item.rate || ''
});
}
ngAfterViewInit() {
setTimeout(() => {
this.nameElement.nativeElement.focus();
}, 0);
}
save() {
this.ser.saveOrUpdate(this.getItem())
.subscribe(
(result) => {
this.toaster.show('Success', '');
this.router.navigateByUrl('/taxes');
},
(error) => {
this.toaster.show('Danger', error.error);
}
);
}
getItem(): Tax {
const formModel = this.form.value;
this.item.name = formModel.name;
this.item.rate = formModel.rate;
return this.item;
}
}