Chore: Moved from Untyped to Stongly Typed forms.
This commit is contained in:
@ -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, of as observableOf } from 'rxjs';
|
||||
import { debounceTime, distinctUntilChanged, map, startWith, switchMap } from 'rxjs/operators';
|
||||
import { debounceTime, distinctUntilChanged, map, switchMap } from 'rxjs/operators';
|
||||
|
||||
import { Account } from '../core/account';
|
||||
import { AccountBalance } from '../core/account-balance';
|
||||
@ -18,25 +18,27 @@ import { MathService } from '../shared/math.service';
|
||||
})
|
||||
export class ReceiptDialogComponent implements OnInit {
|
||||
accounts: Observable<Account[]>;
|
||||
form: UntypedFormGroup;
|
||||
form: FormGroup<{
|
||||
account: FormControl<string | null>;
|
||||
amount: FormControl<string>;
|
||||
}>;
|
||||
|
||||
account: Account = new Account();
|
||||
accBal: AccountBalance | null = null;
|
||||
|
||||
constructor(
|
||||
public dialogRef: MatDialogRef<ReceiptDialogComponent>,
|
||||
@Inject(MAT_DIALOG_DATA) public data: { journal: Journal; date: string },
|
||||
private fb: UntypedFormBuilder,
|
||||
private math: MathService,
|
||||
private accountSer: AccountService,
|
||||
) {
|
||||
this.form = this.fb.group({
|
||||
account: '',
|
||||
amount: '',
|
||||
this.form = new FormGroup({
|
||||
account: new FormControl(''),
|
||||
amount: new FormControl('', { nonNullable: true }),
|
||||
});
|
||||
this.accBal = null;
|
||||
// Setup Account Autocomplete
|
||||
this.accounts = (this.form.get('account') as UntypedFormControl).valueChanges.pipe(
|
||||
startWith(null),
|
||||
this.accounts = this.form.controls.account.valueChanges.pipe(
|
||||
map((x) => (x !== null && x.length >= 1 ? x : null)),
|
||||
debounceTime(150),
|
||||
distinctUntilChanged(),
|
||||
@ -46,8 +48,8 @@ export class ReceiptDialogComponent implements OnInit {
|
||||
|
||||
ngOnInit() {
|
||||
this.form.setValue({
|
||||
account: this.data.journal.account,
|
||||
amount: this.data.journal.amount,
|
||||
account: this.data.journal.account.name,
|
||||
amount: `${this.data.journal.amount}`,
|
||||
});
|
||||
this.account = this.data.journal.account;
|
||||
}
|
||||
|
||||
@ -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, of as observableOf } 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 { Account } from '../core/account';
|
||||
@ -37,7 +37,17 @@ export class ReceiptComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
@ViewChild('dateElement', { static: true }) dateElement?: ElementRef;
|
||||
public journalObservable = new BehaviorSubject<Journal[]>([]);
|
||||
dataSource: ReceiptDataSource = new ReceiptDataSource(this.journalObservable);
|
||||
form: UntypedFormGroup;
|
||||
form: FormGroup<{
|
||||
date: FormControl<Date>;
|
||||
receiptAccount: FormControl<string>;
|
||||
receiptAmount: FormControl<number>;
|
||||
addRow: FormGroup<{
|
||||
account: FormControl<string | null>;
|
||||
amount: FormControl<string>;
|
||||
}>;
|
||||
narration: FormControl<string>;
|
||||
}>;
|
||||
|
||||
receiptAccounts: Account[] = [];
|
||||
receiptJournal: Journal = new Journal();
|
||||
voucher: Voucher = new Voucher();
|
||||
@ -51,7 +61,6 @@ export class ReceiptComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
constructor(
|
||||
private route: ActivatedRoute,
|
||||
private router: Router,
|
||||
private fb: UntypedFormBuilder,
|
||||
private dialog: MatDialog,
|
||||
private hotkeys: HotkeysService,
|
||||
private toaster: ToasterService,
|
||||
@ -61,29 +70,26 @@ export class ReceiptComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
private ser: VoucherService,
|
||||
private accountSer: AccountService,
|
||||
) {
|
||||
this.form = this.fb.group({
|
||||
date: '',
|
||||
receiptAccount: '',
|
||||
receiptAmount: { value: '', disabled: true },
|
||||
addRow: this.fb.group({
|
||||
account: '',
|
||||
amount: '',
|
||||
this.form = new FormGroup({
|
||||
date: new FormControl(new Date(), { nonNullable: true }),
|
||||
receiptAccount: new FormControl('', { nonNullable: true }),
|
||||
receiptAmount: new FormControl({ value: 0, disabled: true }, { nonNullable: true }),
|
||||
addRow: new FormGroup({
|
||||
account: new FormControl(''),
|
||||
amount: new FormControl('', { nonNullable: true }),
|
||||
}),
|
||||
narration: '',
|
||||
narration: new FormControl('', { nonNullable: true }),
|
||||
});
|
||||
this.accBal = null;
|
||||
// Listen to Account Autocomplete Change
|
||||
this.accounts = (
|
||||
(this.form.get('addRow') as UntypedFormControl).get('account') as UntypedFormControl
|
||||
).valueChanges.pipe(
|
||||
startWith(null),
|
||||
this.accounts = this.form.controls.addRow.controls.account.valueChanges.pipe(
|
||||
map((x) => (x !== null && x.length >= 1 ? x : null)),
|
||||
debounceTime(150),
|
||||
distinctUntilChanged(),
|
||||
switchMap((x) => (x === null ? observableOf([]) : this.accountSer.autocomplete(x))),
|
||||
);
|
||||
// Listen to Receipt Account Change
|
||||
(this.form.get('receiptAccount') as UntypedFormControl).valueChanges.subscribe((x) =>
|
||||
this.form.controls.receiptAccount.valueChanges.subscribe((x) =>
|
||||
this.router.navigate([], {
|
||||
relativeTo: this.route,
|
||||
queryParams: { a: x },
|
||||
@ -152,11 +158,11 @@ export class ReceiptComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
[this.receiptJournal] = this.voucher.journals.filter((x) => x.debit === 1);
|
||||
this.form.setValue({
|
||||
date: moment(this.voucher.date, 'DD-MMM-YYYY').toDate(),
|
||||
receiptAccount: this.receiptJournal.account.id,
|
||||
receiptAccount: this.receiptJournal.account.id!,
|
||||
receiptAmount: this.receiptJournal.amount,
|
||||
addRow: {
|
||||
account: '',
|
||||
amount: 0,
|
||||
amount: '',
|
||||
},
|
||||
narration: this.voucher.narration,
|
||||
});
|
||||
@ -173,10 +179,7 @@ export class ReceiptComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
}
|
||||
|
||||
addRow() {
|
||||
const amount = this.math.parseAmount(
|
||||
(this.form.get('addRow') as UntypedFormControl).value.amount,
|
||||
2,
|
||||
);
|
||||
const amount = this.math.parseAmount(this.form.value.addRow?.amount!, 2);
|
||||
const debit = -1;
|
||||
if (this.account === null || amount <= 0) {
|
||||
return;
|
||||
@ -205,10 +208,7 @@ export class ReceiptComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
}
|
||||
|
||||
resetAddRow() {
|
||||
(this.form.get('addRow') as UntypedFormControl).reset({
|
||||
account: null,
|
||||
amount: null,
|
||||
});
|
||||
this.form.controls.addRow.reset({ account: null, amount: '' });
|
||||
this.account = null;
|
||||
this.accBal = null;
|
||||
setTimeout(() => {
|
||||
@ -225,7 +225,7 @@ export class ReceiptComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
Math.abs(journals.map((x) => x.amount).reduce((p, c) => p + c, 0)),
|
||||
2,
|
||||
);
|
||||
(this.form.get('receiptAmount') as UntypedFormControl).setValue(this.receiptJournal.amount);
|
||||
this.form.controls.receiptAmount.setValue(this.receiptJournal.amount);
|
||||
}
|
||||
|
||||
editRow(row: Journal) {
|
||||
@ -233,7 +233,7 @@ export class ReceiptComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
width: '750px',
|
||||
data: {
|
||||
journal: { ...row },
|
||||
date: moment((this.form.get('date') as UntypedFormControl).value).format('DD-MMM-YYYY'),
|
||||
date: moment(this.form.value.date).format('DD-MMM-YYYY'),
|
||||
},
|
||||
});
|
||||
|
||||
@ -304,7 +304,7 @@ export class ReceiptComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
const formModel = this.form.value;
|
||||
this.voucher.date = moment(formModel.date).format('DD-MMM-YYYY');
|
||||
this.receiptJournal.account.id = formModel.receiptAccount;
|
||||
this.voucher.narration = formModel.narration;
|
||||
this.voucher.narration = formModel.narration!;
|
||||
return this.voucher;
|
||||
}
|
||||
|
||||
@ -340,7 +340,7 @@ export class ReceiptComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
accountSelected(event: MatAutocompleteSelectedEvent): void {
|
||||
const account = event.option.value;
|
||||
this.account = account;
|
||||
const date = moment((this.form.get('date') as UntypedFormControl).value).format('DD-MMM-YYYY');
|
||||
const date = moment(this.form.value.date).format('DD-MMM-YYYY');
|
||||
this.accountSer.balance(account.id as string, date).subscribe((v) => {
|
||||
this.accBal = v;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user