Chore: Moved from Untyped to Stongly Typed forms.

This commit is contained in:
2022-07-15 13:24:25 +05:30
parent facf2df91e
commit 28f9bf2180
78 changed files with 1091 additions and 1004 deletions

View File

@ -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

View File

@ -1,10 +1,10 @@
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { UntypedFormBuilder, UntypedFormControl, UntypedFormGroup } from '@angular/forms';
import { FormControl, FormGroup } from '@angular/forms';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { ActivatedRoute } from '@angular/router';
import { Observable } from 'rxjs';
import { debounceTime, distinctUntilChanged, startWith } from 'rxjs/operators';
import { debounceTime, distinctUntilChanged } from 'rxjs/operators';
import { ToCsvService } from '../../shared/to-csv.service';
import { Employee } from '../employee';
@ -22,7 +22,10 @@ export class EmployeeListComponent implements OnInit, AfterViewInit {
@ViewChild(MatSort, { static: true }) sort?: MatSort;
dataSource: EmployeeListDataSource;
filter: Observable<string>;
form: UntypedFormGroup;
form: FormGroup<{
filter: FormControl<string>;
}>;
list: Employee[] = [];
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
@ -37,16 +40,11 @@ export class EmployeeListComponent implements OnInit, AfterViewInit {
'leavingDate',
];
constructor(
private route: ActivatedRoute,
private fb: UntypedFormBuilder,
private toCsv: ToCsvService,
) {
this.form = this.fb.group({
filter: '',
constructor(private route: ActivatedRoute, private toCsv: ToCsvService) {
this.form = new FormGroup({
filter: new FormControl<string>('', { nonNullable: true }),
});
this.filter = (this.form.get('filter') as UntypedFormControl).valueChanges.pipe(
startWith(''),
this.filter = this.form.controls.filter.valueChanges.pipe(
debounceTime(150),
distinctUntilChanged(),
);

View File

@ -1,4 +1,4 @@
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/internal/Observable';
import { catchError } from 'rxjs/operators';