Now all that is needed is to make it ready for strict compiling. Removed eslint-plugin-prettier as it is not recommended and causes errors for both eslint and prettier Bumped to v8.0.0
83 lines
2.5 KiB
TypeScript
83 lines
2.5 KiB
TypeScript
import { Component, Inject, OnInit } from '@angular/core';
|
|
import { FormBuilder, 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 { Account } from '../core/account';
|
|
import { AccountService } from '../core/account.service';
|
|
import { MathService } from '../shared/math.service';
|
|
|
|
@Component({
|
|
selector: 'app-journal-dialog',
|
|
templateUrl: './journal-dialog.component.html',
|
|
styleUrls: ['./journal-dialog.component.css'],
|
|
})
|
|
export class JournalDialogComponent implements OnInit {
|
|
accounts: Observable<Account[]>;
|
|
form: FormGroup;
|
|
account: Account;
|
|
accBal: any;
|
|
|
|
constructor(
|
|
public dialogRef: MatDialogRef<JournalDialogComponent>,
|
|
@Inject(MAT_DIALOG_DATA) public data: any,
|
|
private fb: FormBuilder,
|
|
private math: MathService,
|
|
private accountSer: AccountService,
|
|
) {
|
|
this.createForm();
|
|
this.setupAccountAutocomplete();
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.form.setValue({
|
|
debit: `${this.data.journal.debit}`,
|
|
account: this.data.journal.account,
|
|
amount: this.data.journal.amount,
|
|
});
|
|
this.account = this.data.journal.account;
|
|
}
|
|
|
|
createForm() {
|
|
this.form = this.fb.group({
|
|
debit: '1',
|
|
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))),
|
|
);
|
|
}
|
|
|
|
displayFn(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 = this.math.journalAmount(formValue.amount, +formValue.debit);
|
|
this.data.journal.debit = amount.debit;
|
|
this.data.journal.account = this.account;
|
|
this.data.journal.amount = amount.amount;
|
|
this.dialogRef.close(this.data.journal);
|
|
}
|
|
}
|