Chore: Moved from Untyped to Stongly Typed forms.

This commit is contained in:
tanshu committed 2022-07-15 13:24:25 +05:30
1 parent facf2df91e
commit 28f9bf2180
78 files changed
+1091 -1004

No files matched your search

+62 -61
View File
@@ -1,10 +1,5 @@
import { Component, OnInit } from '@angular/core';
import {
UntypedFormArray,
UntypedFormBuilder,
UntypedFormControl,
UntypedFormGroup,
} from '@angular/forms';
import { FormArray, FormControl, FormGroup } from '@angular/forms';
import { MatDialog } from '@angular/material/dialog';
import { ActivatedRoute, Router } from '@angular/router';
import * as moment from 'moment';
@@ -32,14 +27,31 @@ export class SettingsComponent implements OnInit {
accountTypes: AccountType[] = [];
voucherTypes: AccountType[] = [];
lockInfoForm: UntypedFormGroup;
lockInfoForm: FormGroup<{
validFrom: FormControl<moment.Moment | null>;
validTill: FormControl<moment.Moment | null>;
index: FormControl<number>;
lockOlder: FormControl<boolean>;
olderRolling: FormControl<boolean>;
olderDate: FormControl<moment.Moment | null>;
olderDays: FormControl<number | null>;
lockNewer: FormControl<boolean>;
newerRolling: FormControl<boolean>;
newerDate: FormControl<moment.Moment | null>;
newerDays: FormControl<number | null>;
accountTypes: FormArray<FormGroup<{ accountType: FormControl<boolean> }>>;
voucherTypes: FormArray<FormGroup<{ voucherType: FormControl<boolean> }>>;
}>;
lockInformation: LockInfo[];
lockOlder: boolean = false;
olderRolling: boolean = false;
lockNewer: boolean = false;
newerRolling: boolean = false;
rebaseDataForm: UntypedFormGroup;
rebaseDataForm: FormGroup<{
date: FormControl<moment.Moment>;
}>;
maintenance: { enabled: boolean; user: string };
@@ -50,7 +62,6 @@ export class SettingsComponent implements OnInit {
constructor(
private route: ActivatedRoute,
private router: Router,
private fb: UntypedFormBuilder,
private dialog: MatDialog,
private toaster: ToasterService,
public auth: AuthService,
@@ -58,24 +69,24 @@ export class SettingsComponent implements OnInit {
) {
const startDate = moment().date(1);
const finishDate = moment().date(startDate.daysInMonth());
this.lockInfoForm = this.fb.group({
validFrom: '',
validTill: '',
index: 0,
lockOlder: '',
olderRolling: '',
olderDate: finishDate,
olderDays: 0,
lockNewer: '',
newerRolling: '',
newerDate: finishDate,
newerDays: 0,
accountTypes: this.fb.array([]),
voucherTypes: this.fb.array([]),
this.lockInfoForm = new FormGroup({
validFrom: new FormControl<moment.Moment | null>(null),
validTill: new FormControl<moment.Moment | null>(null),
index: new FormControl<number>(0, { nonNullable: true }),
lockOlder: new FormControl<boolean>(false, { nonNullable: true }),
olderRolling: new FormControl<boolean>(false, { nonNullable: true }),
olderDate: new FormControl<moment.Moment | null>(finishDate),
olderDays: new FormControl<number | null>(0),
lockNewer: new FormControl<boolean>(false, { nonNullable: true }),
newerRolling: new FormControl<boolean>(false, { nonNullable: true }),
newerDate: new FormControl<moment.Moment | null>(finishDate),
newerDays: new FormControl<number | null>(0),
accountTypes: new FormArray<FormGroup<{ accountType: FormControl<boolean> }>>([]),
voucherTypes: new FormArray<FormGroup<{ voucherType: FormControl<boolean> }>>([]),
});
this.lockInformation = [];
this.rebaseDataForm = this.fb.group({
date: moment(),
this.rebaseDataForm = new FormGroup({
date: new FormControl<moment.Moment>(moment(), { nonNullable: true }),
});
this.listenToLockForm();
@@ -100,24 +111,20 @@ export class SettingsComponent implements OnInit {
showLockInformation(info: LockInfo[]) {
this.lockInformation = info;
this.lockInfoForm.setControl(
'accountTypes',
this.fb.array(
this.accountTypes.map((x) =>
this.fb.group({
accountType: false,
}),
),
this.lockInfoForm.controls.accountTypes.reset();
this.accountTypes.forEach((x) =>
this.lockInfoForm.controls.accountTypes.push(
new FormGroup({
accountType: new FormControl<boolean>(false, { nonNullable: true }),
}),
),
);
this.lockInfoForm.setControl(
'voucherTypes',
this.fb.array(
this.voucherTypes.map((x) =>
this.fb.group({
voucherType: false,
}),
),
this.lockInfoForm.controls.voucherTypes.reset();
this.voucherTypes.forEach((x) =>
this.lockInfoForm.controls.voucherTypes.push(
new FormGroup({
voucherType: new FormControl<boolean>(false, { nonNullable: true }),
}),
),
);
this.dataSource = new LockDataSource(this.lockObservable);
@@ -125,16 +132,16 @@ export class SettingsComponent implements OnInit {
}
listenToLockForm() {
(this.lockInfoForm.get('lockOlder') as UntypedFormControl).valueChanges.subscribe((x) => {
this.lockInfoForm.controls.lockOlder.valueChanges.subscribe((x) => {
this.lockOlder = x;
});
(this.lockInfoForm.get('lockNewer') as UntypedFormControl).valueChanges.subscribe((x) => {
this.lockInfoForm.controls.lockNewer.valueChanges.subscribe((x) => {
this.lockNewer = x;
});
(this.lockInfoForm.get('olderRolling') as UntypedFormControl).valueChanges.subscribe((x) => {
this.lockInfoForm.controls.olderRolling.valueChanges.subscribe((x) => {
this.olderRolling = x;
});
(this.lockInfoForm.get('newerRolling') as UntypedFormControl).valueChanges.subscribe((x) => {
this.lockInfoForm.controls.newerRolling.valueChanges.subscribe((x) => {
this.newerRolling = x;
});
}
@@ -142,44 +149,40 @@ export class SettingsComponent implements OnInit {
saveLock() {
const item = new LockInfo();
if (this.lockOlder && this.olderRolling) {
item.start.days = +(this.lockInfoForm.get('olderDays') as UntypedFormControl).value;
item.start.days = +this.lockInfoForm.value.olderDays!;
} else if (this.lockOlder && !this.olderRolling) {
item.start.date = moment(
(this.lockInfoForm.get('olderDate') as UntypedFormControl).value,
).format('DD-MMM-YYYY');
item.start.date = this.lockInfoForm.value.olderDate!.format('DD-MMM-YYYY');
}
if (this.lockNewer && this.newerRolling) {
item.finish.days = +(this.lockInfoForm.get('newerDays') as UntypedFormControl).value;
item.finish.days = +this.lockInfoForm.value.newerDays!;
}
if (this.lockNewer && !this.newerRolling) {
item.finish.date = moment(
(this.lockInfoForm.get('newerDate') as UntypedFormControl).value,
).format('DD-MMM-YYYY');
item.finish.date = this.lockInfoForm.value.newerDate!.format('DD-MMM-YYYY');
}
const atArray = this.lockInfoForm.get('accountTypes') as UntypedFormArray;
const atArray = this.lockInfoForm.controls.accountTypes;
this.accountTypes.forEach((at, index) => {
if (atArray.controls[index].value.accountType) {
item.accountTypes.push({ id: at.id, name: at.name });
}
});
const vtArray = this.lockInfoForm.get('voucherTypes') as UntypedFormArray;
const vtArray = this.lockInfoForm.controls.voucherTypes;
this.voucherTypes.forEach((vt, index) => {
if (vtArray.controls[index].value.voucherType) {
item.voucherTypes.push({ id: vt.id, name: vt.name });
}
});
const validFrom = (this.lockInfoForm.get('validFrom') as UntypedFormControl).value;
const validFrom = this.lockInfoForm.value.validFrom;
if (validFrom) {
item.validFrom = moment(validFrom).format('DD-MMM-YYYY');
}
const validTill = (this.lockInfoForm.get('validTill') as UntypedFormControl).value;
const validTill = this.lockInfoForm.value.validTill;
if (validTill) {
item.validTill = moment(validTill).format('DD-MMM-YYYY');
}
item.index = +(this.lockInfoForm.get('index') as UntypedFormControl).value;
item.index = +this.lockInfoForm.value.index!;
this.ser.setLockInformation(item).subscribe(
(result) => {
@@ -199,9 +202,7 @@ export class SettingsComponent implements OnInit {
}
confirmRebase(): void {
const rebaseDate = moment((this.rebaseDataForm.get('date') as UntypedFormControl).value).format(
'DD-MMM-YYYY',
);
const rebaseDate = this.rebaseDataForm.value.date!.format('DD-MMM-YYYY');
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
width: '250px',
data: {
@@ -4,7 +4,6 @@ import { Observable } from 'rxjs/internal/Observable';
import { catchError } from 'rxjs/operators';
import { ErrorLoggerService } from '../core/error-logger.service';
import { Product } from '../core/product';
import { LockInfo } from './lock-info';