Chore: Moved from Untyped to Stongly Typed forms.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
|
||||
import { UntypedFormBuilder, UntypedFormControl, UntypedFormGroup } from '@angular/forms';
|
||||
import { FormControl, FormGroup } from '@angular/forms';
|
||||
import { MatDialog } from '@angular/material/dialog';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import * as moment from 'moment';
|
||||
@@ -17,7 +17,18 @@ import { EmployeeService } from '../employee.service';
|
||||
})
|
||||
export class EmployeeDetailComponent implements OnInit, AfterViewInit {
|
||||
@ViewChild('nameElement', { static: true }) nameElement?: ElementRef;
|
||||
form: UntypedFormGroup;
|
||||
form: FormGroup<{
|
||||
code: FormControl<string | number>;
|
||||
name: FormControl<string | null>;
|
||||
designation: FormControl<string | null>;
|
||||
salary: FormControl<number | string | null>;
|
||||
points: FormControl<number | string | null>;
|
||||
isActive: FormControl<boolean>;
|
||||
costCentre: FormControl<string | null>;
|
||||
joiningDate: FormControl<Date>;
|
||||
leavingDate: FormControl<Date | null>;
|
||||
}>;
|
||||
|
||||
costCentres: CostCentre[] = [];
|
||||
item: Employee = new Employee();
|
||||
|
||||
@@ -25,23 +36,22 @@ export class EmployeeDetailComponent implements OnInit, AfterViewInit {
|
||||
private route: ActivatedRoute,
|
||||
private router: Router,
|
||||
private dialog: MatDialog,
|
||||
private fb: UntypedFormBuilder,
|
||||
private toaster: ToasterService,
|
||||
private ser: EmployeeService,
|
||||
) {
|
||||
this.form = this.fb.group({
|
||||
code: { value: '', disabled: true },
|
||||
name: '',
|
||||
designation: '',
|
||||
salary: '',
|
||||
points: '',
|
||||
isActive: '',
|
||||
costCentre: '',
|
||||
joiningDate: '',
|
||||
leavingDate: '',
|
||||
this.form = new FormGroup({
|
||||
code: new FormControl<string | number>({ value: 0, disabled: true }, { nonNullable: true }),
|
||||
name: new FormControl<string | null>(null),
|
||||
designation: new FormControl<string | null>(null),
|
||||
salary: new FormControl<number | string | null>(null),
|
||||
points: new FormControl<number | string | null>(null),
|
||||
isActive: new FormControl<boolean>(true, { nonNullable: true }),
|
||||
costCentre: new FormControl<string | null>(null),
|
||||
joiningDate: new FormControl(new Date(), { nonNullable: true }),
|
||||
leavingDate: new FormControl<Date | null>(null),
|
||||
});
|
||||
// Listen to IsActive Changes
|
||||
(this.form.get('isActive') as UntypedFormControl).valueChanges.subscribe((x) => {
|
||||
this.form.controls.isActive.valueChanges.subscribe((x) => {
|
||||
this.item.isActive = x;
|
||||
});
|
||||
}
|
||||
@@ -64,10 +74,10 @@ export class EmployeeDetailComponent implements OnInit, AfterViewInit {
|
||||
salary: this.item.salary || '',
|
||||
points: this.item.points || '',
|
||||
isActive: this.item.isActive,
|
||||
costCentre: this.item.costCentre.id,
|
||||
costCentre: this.item.costCentre.id!,
|
||||
joiningDate: this.item.joiningDate
|
||||
? moment(this.item.joiningDate, 'DD-MMM-YYYY').toDate()
|
||||
: '',
|
||||
: new Date(),
|
||||
leavingDate: this.item.isActive
|
||||
? null
|
||||
: moment(this.item.leavingDate, 'DD-MMM-YYYY').toDate(),
|
||||
@@ -121,12 +131,12 @@ export class EmployeeDetailComponent implements OnInit, AfterViewInit {
|
||||
|
||||
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.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
|
||||
|
||||
Reference in New Issue
Block a user