Files
brewman/overlord/src/app/employee/employee-detail/employee-detail.component.ts
tanshu 8996516978 Added loading bar
Fixed product, account and employee details for add new.
2018-06-10 13:58:01 +05:30

137 lines
3.9 KiB
TypeScript

import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {FormBuilder, FormGroup} from '@angular/forms';
import {MatDialog} from '@angular/material';
import {ActivatedRoute, Router} from '@angular/router';
import * as moment from 'moment';
import {ToasterService} from '../../core/toaster.service';
import {EmployeeService} from '../employee.service';
import {Employee} from '../employee';
import {CostCentre} from '../../cost-centre/cost-centre';
import {ConfirmDialogComponent} from '../../shared/confirm-dialog/confirm-dialog.component';
@Component({
selector: 'app-employee-detail',
templateUrl: './employee-detail.component.html',
styleUrls: ['./employee-detail.component.css']
})
export class EmployeeDetailComponent implements OnInit, AfterViewInit {
@ViewChild('nameElement') nameElement: ElementRef;
form: FormGroup;
costCentres: CostCentre[];
item: Employee;
constructor(
private route: ActivatedRoute,
private router: Router,
private dialog: MatDialog,
private fb: FormBuilder,
private toaster: ToasterService,
private ser: EmployeeService
) {
this.createForm();
this.listenToIsActiveChanges();
}
createForm() {
this.form = this.fb.group({
code: {value: '', disabled: true},
name: '',
designation: '',
salary: '',
points: '',
isActive: '',
costCentre: '',
joiningDate: '',
leavingDate: ''
});
}
ngOnInit() {
this.route.data
.subscribe((data: { item: Employee, costCentres: CostCentre[] }) => {
this.costCentres = data.costCentres;
this.showItem(data.item);
});
}
showItem(item: Employee) {
this.item = item;
this.form.setValue({
code: this.item.code || '(Auto)',
name: this.item.name || '',
designation: this.item.designation || '',
salary: this.item.salary || '',
points: this.item.points || '',
isActive: this.item.isActive,
costCentre: this.item.costCentre.id,
joiningDate: this.item.joiningDate ? moment(this.item.joiningDate, 'DD-MMM-YYYY').toDate() : '',
leavingDate: this.item.isActive ? null : moment(this.item.leavingDate, 'DD-MMM-YYYY').toDate()
});
}
ngAfterViewInit() {
setTimeout(() => {
this.nameElement.nativeElement.focus();
}, 0);
}
listenToIsActiveChanges(): void {
this.form.get('isActive').valueChanges
.subscribe(x => this.item.isActive = x);
}
save() {
this.ser.saveOrUpdate(this.getItem())
.subscribe(
(result) => {
this.toaster.show('Success', '');
this.router.navigateByUrl('/Employees');
},
(error) => {
this.toaster.show('Danger', error.error);
}
);
}
delete() {
this.ser.delete(this.item.id)
.subscribe(
(result) => {
this.toaster.show('Success', '');
this.router.navigateByUrl('/Employees');
},
(error) => {
this.toaster.show('Danger', error.error);
}
);
}
confirmDelete(): void {
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
width: '250px',
data: {title: 'Delete Employee?', content: 'Are you sure? This cannot be undone.'}
});
dialogRef.afterClosed().subscribe((result: boolean) => {
if (result) {
this.delete();
}
});
}
getItem(): Employee {
const formModel = this.form.value;
this.item.name = formModel.name;
this.item.designation = formModel.designation;
this.item.salary = formModel.salary;
this.item.points = formModel.points;
this.item.isActive = formModel.isActive;
this.item.costCentre.id = formModel.costCentre;
this.item.joiningDate = moment(formModel.joiningDate).format('DD-MMM-YYYY');
this.item.leavingDate = (this.item.isActive) ? null : moment(formModel.leavingDate).format('DD-MMM-YYYY');
return this.item;
}
}