135 lines
3.9 KiB
TypeScript
135 lines
3.9 KiB
TypeScript
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
|
|
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
|
|
import { MatDialog } from '@angular/material/dialog';
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
import * as moment from 'moment';
|
|
|
|
import { CostCentre } from '../../core/cost-centre';
|
|
import { ToasterService } from '../../core/toaster.service';
|
|
import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component';
|
|
import { Employee } from '../employee';
|
|
import { EmployeeService } from '../employee.service';
|
|
|
|
@Component({
|
|
selector: 'app-employee-detail',
|
|
templateUrl: './employee-detail.component.html',
|
|
styleUrls: ['./employee-detail.component.css'],
|
|
})
|
|
export class EmployeeDetailComponent implements OnInit, AfterViewInit {
|
|
@ViewChild('nameElement', { static: true }) nameElement?: ElementRef;
|
|
form: FormGroup;
|
|
costCentres: CostCentre[] = [];
|
|
item: Employee = new Employee();
|
|
|
|
constructor(
|
|
private route: ActivatedRoute,
|
|
private router: Router,
|
|
private dialog: MatDialog,
|
|
private fb: FormBuilder,
|
|
private toaster: ToasterService,
|
|
private ser: EmployeeService,
|
|
) {
|
|
this.form = this.fb.group({
|
|
code: { value: '', disabled: true },
|
|
name: '',
|
|
designation: '',
|
|
salary: '',
|
|
points: '',
|
|
isActive: '',
|
|
costCentre: '',
|
|
joiningDate: '',
|
|
leavingDate: '',
|
|
});
|
|
// Listen to IsActive Changes
|
|
(this.form.get('isActive') as FormControl).valueChanges.subscribe((x) => {
|
|
this.item.isActive = x;
|
|
});
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.route.data.subscribe((value) => {
|
|
const data = value as { 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(() => {
|
|
if (this.nameElement) this.nameElement.nativeElement.focus();
|
|
}, 0);
|
|
}
|
|
|
|
save() {
|
|
this.ser.saveOrUpdate(this.getItem()).subscribe(
|
|
() => {
|
|
this.toaster.show('Success', '');
|
|
this.router.navigateByUrl('/employees');
|
|
},
|
|
(error) => {
|
|
this.toaster.show('Danger', error);
|
|
},
|
|
);
|
|
}
|
|
|
|
delete() {
|
|
this.ser.delete(this.item.id as string).subscribe(
|
|
() => {
|
|
this.toaster.show('Success', '');
|
|
this.router.navigateByUrl('/employees');
|
|
},
|
|
(error) => {
|
|
this.toaster.show('Danger', 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;
|
|
}
|
|
}
|