Files
brewman/overlord/src/app/receipt/receipt-dialog.component.ts
Amritanshu 72044476a8 Feature: Lazy loading
Lazy loaded everything
TODO: The cash flow module when clicking on sub-links, it reloads the whole page, it needs to be diagnosed and fixed, this problem also exists in the other modules
TODO: Rename folders and modules such as account to accounts to match the url
2019-06-13 16:36:43 +05:30

78 lines
2.3 KiB
TypeScript

import {Component, Inject, OnInit} from '@angular/core';
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import {debounceTime, distinctUntilChanged, map, startWith, switchMap} from 'rxjs/operators';
import {Account} from '../core/account';
import {AccountService} from '../core/account.service';
import {FormBuilder, FormGroup} from '@angular/forms';
import {Observable, of as observableOf} from 'rxjs';
@Component({
selector: 'app-receipt-dialog',
templateUrl: './receipt-dialog.component.html',
styleUrls: ['./receipt-dialog.component.css']
})
export class ReceiptDialogComponent implements OnInit {
accounts: Observable<Account[]>;
form: FormGroup;
account: Account;
accBal: any;
constructor(
public dialogRef: MatDialogRef<ReceiptDialogComponent>,
@Inject(MAT_DIALOG_DATA) public data: any,
private fb: FormBuilder,
private accountSer: AccountService) {
this.createForm();
this.setupAccountAutocomplete();
}
ngOnInit() {
this.form.setValue({
account: this.data.journal.account,
amount: this.data.journal.amount
});
this.account = this.data.journal.account;
}
createForm() {
this.form = this.fb.group({
account: '',
amount: ''
});
this.accBal = null;
}
setupAccountAutocomplete(): void {
const control = this.form.get('account');
this.accounts = control.valueChanges
.pipe(
startWith(null),
map(x => (x !== null && x.length >= 1) ? x : null),
debounceTime(150),
distinctUntilChanged(),
switchMap(x => (x === null) ? observableOf([]) : this.accountSer.autocomplete(x))
);
}
displayAccount(account?: Account): string | undefined {
return account ? account.name : undefined;
}
accountSelected(event: MatAutocompleteSelectedEvent): void {
this.account = event.option.value;
this.accountSer.balance(this.account.id, this.data.date).subscribe((v) => {
this.accBal = v;
});
}
accept(): void {
const formValue = this.form.value;
const amount = +formValue.amount;
this.data.journal.account = this.account;
this.data.journal.amount = amount;
this.dialogRef.close(this.data.journal);
}
}