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,9 +1,9 @@
import { Component, Inject, OnInit } from '@angular/core';
import { UntypedFormBuilder, UntypedFormControl, UntypedFormGroup } from '@angular/forms';
import { FormControl, FormGroup } from '@angular/forms';
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { Observable } from 'rxjs';
import { debounceTime, distinctUntilChanged, startWith, switchMap } from 'rxjs/operators';
import { debounceTime, distinctUntilChanged, map, switchMap } from 'rxjs/operators';
import { Batch } from '../core/batch';
import { BatchService } from '../core/batch.service';
@ -17,23 +17,27 @@ import { MathService } from '../shared/math.service';
})
export class IssueDialogComponent implements OnInit {
batches: Observable<Batch[]>;
form: UntypedFormGroup;
form: FormGroup<{
batch: FormControl<Batch | string | null>;
quantity: FormControl<string>;
}>;
batch: Batch = new Batch();
constructor(
public dialogRef: MatDialogRef<IssueDialogComponent>,
@Inject(MAT_DIALOG_DATA) public data: { inventory: Inventory; date: string },
private fb: UntypedFormBuilder,
private math: MathService,
private batchSer: BatchService,
) {
this.form = this.fb.group({
batch: '',
quantity: '',
this.form = new FormGroup({
batch: new FormControl<Batch | string | null>(null),
quantity: new FormControl<string>('', { nonNullable: true }),
});
// Listen to Batch Autocomplete Change
this.batches = (this.form.get('batch') as UntypedFormControl).valueChanges.pipe(
startWith(''),
this.batches = this.form.controls.batch.valueChanges.pipe(
map((x) => (x instanceof Batch ? x.name : x)),
map((x) => (x !== null && x.length >= 1 ? x : '')),
debounceTime(150),
distinctUntilChanged(),
switchMap((x) => this.batchSer.autocomplete(this.data.date, x)),
@ -43,7 +47,7 @@ export class IssueDialogComponent implements OnInit {
ngOnInit() {
this.form.setValue({
batch: this.data.inventory.batch,
quantity: this.data.inventory.quantity,
quantity: `${this.data.inventory.quantity}`,
});
this.batch = this.data.inventory.batch as Batch;
}

View File

@ -1,5 +1,5 @@
import { AfterViewInit, Component, ElementRef, OnInit, OnDestroy, ViewChild } from '@angular/core';
import { UntypedFormBuilder, UntypedFormControl, UntypedFormGroup } from '@angular/forms';
import { FormControl, FormGroup } from '@angular/forms';
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
import { MatDialog } from '@angular/material/dialog';
import { ActivatedRoute, Router } from '@angular/router';
@ -7,7 +7,7 @@ import { Hotkey, HotkeysService } from 'angular2-hotkeys';
import { round } from 'mathjs';
import * as moment from 'moment';
import { BehaviorSubject, Observable } from 'rxjs';
import { debounceTime, distinctUntilChanged, map, startWith, switchMap } from 'rxjs/operators';
import { debounceTime, distinctUntilChanged, map, switchMap } from 'rxjs/operators';
import { AuthService } from '../auth/auth.service';
import { Batch } from '../core/batch';
@ -39,7 +39,18 @@ export class IssueComponent implements OnInit, AfterViewInit, OnDestroy {
public gridObservable = new BehaviorSubject<IssueGridItem[]>([]);
dataSource: IssueDataSource = new IssueDataSource(this.inventoryObservable);
gridDataSource: IssueGridDataSource = new IssueGridDataSource(this.gridObservable);
form: UntypedFormGroup;
form: FormGroup<{
date: FormControl<Date>;
source: FormControl<string | null>;
destination: FormControl<string | null>;
amount: FormControl<number>;
addRow: FormGroup<{
batch: FormControl<Batch | string | null>;
quantity: FormControl<string>;
}>;
narration: FormControl<string>;
}>;
voucher: Voucher = new Voucher();
costCentres: CostCentre[] = [];
batch: Batch | null = null;
@ -52,7 +63,6 @@ export class IssueComponent implements OnInit, AfterViewInit, OnDestroy {
constructor(
private route: ActivatedRoute,
private router: Router,
private fb: UntypedFormBuilder,
private dialog: MatDialog,
private hotkeys: HotkeysService,
private toaster: ToasterService,
@ -62,22 +72,21 @@ export class IssueComponent implements OnInit, AfterViewInit, OnDestroy {
private batchSer: BatchService,
private issueGridSer: IssueGridService,
) {
this.form = this.fb.group({
date: '',
source: '',
destination: '',
amount: { value: '', disabled: true },
addRow: this.fb.group({
batch: '',
quantity: '',
this.form = new FormGroup({
date: new FormControl(new Date(), { nonNullable: true }),
source: new FormControl<string | null>(null),
destination: new FormControl<string | null>(null),
amount: new FormControl({ value: 0, disabled: true }, { nonNullable: true }),
addRow: new FormGroup({
batch: new FormControl<Batch | string | null>(null),
quantity: new FormControl<string>('', { nonNullable: true }),
}),
narration: '',
narration: new FormControl('', { nonNullable: true }),
});
// Listen to Batch Autocomplete Change
this.batches = (
(this.form.get('addRow') as UntypedFormControl).get('batch') as UntypedFormControl
).valueChanges.pipe(
startWith('null'),
this.batches = this.form.controls.addRow.controls.batch.valueChanges.pipe(
map((x) => (x instanceof Batch ? x.name : x)),
map((x) => (x !== null && x.length >= 1 ? x : '')),
debounceTime(150),
distinctUntilChanged(),
switchMap((x) =>
@ -85,7 +94,7 @@ export class IssueComponent implements OnInit, AfterViewInit, OnDestroy {
),
);
// Listen to Date Change
(this.form.get('date') as UntypedFormControl).valueChanges
this.form.controls.date.valueChanges
.pipe(map((x) => moment(x).format('DD-MMM-YYYY')))
.subscribe((x) => this.showGrid(x));
}
@ -138,8 +147,8 @@ export class IssueComponent implements OnInit, AfterViewInit, OnDestroy {
this.voucher = voucher;
this.form.setValue({
date: moment(this.voucher.date, 'DD-MMM-YYYY').toDate(),
source: (this.voucher.source as CostCentre).id,
destination: (this.voucher.destination as CostCentre).id,
source: (this.voucher.source as CostCentre).id!,
destination: (this.voucher.destination as CostCentre).id!,
amount: Math.abs(this.voucher.inventories.map((x) => x.amount).reduce((p, c) => p + c, 0)),
addRow: {
batch: '',
@ -160,7 +169,7 @@ export class IssueComponent implements OnInit, AfterViewInit, OnDestroy {
}
addRow() {
const formValue = (this.form.get('addRow') as UntypedFormControl).value;
const formValue = this.form.value.addRow!;
const quantity = this.math.parseAmount(formValue.quantity, 2);
const isConsumption = this.form.value.source === '7b845f95-dfef-fa4a-897c-f0baf15284a3';
if (this.batch === null || quantity <= 0) {
@ -194,10 +203,7 @@ export class IssueComponent implements OnInit, AfterViewInit, OnDestroy {
}
resetAddRow() {
(this.form.get('addRow') as UntypedFormControl).reset({
batch: null,
quantity: '',
});
this.form.controls.addRow.reset();
this.batch = null;
setTimeout(() => {
if (this.batchElement) {
@ -212,7 +218,7 @@ export class IssueComponent implements OnInit, AfterViewInit, OnDestroy {
Math.abs(this.voucher.inventories.map((x) => x.amount).reduce((p, c) => p + c, 0)),
2,
);
(this.form.get('amount') as UntypedFormControl).setValue(amount);
this.form.controls.amount.setValue(amount);
}
editRow(row: Inventory) {
@ -282,9 +288,9 @@ export class IssueComponent implements OnInit, AfterViewInit, OnDestroy {
getVoucher(): Voucher {
const formModel = this.form.value;
this.voucher.date = moment(formModel.date).format('DD-MMM-YYYY');
(this.voucher.source as CostCentre).id = formModel.source;
(this.voucher.destination as CostCentre).id = formModel.destination;
this.voucher.narration = formModel.narration;
(this.voucher.source as CostCentre).id = formModel.source!;
(this.voucher.destination as CostCentre).id = formModel.destination!;
this.voucher.narration = formModel.narration!;
return this.voucher;
}