hv2-all-in-one/ui/hv2-ui/src/app/account/account.component.ts

91 lines
3.5 KiB
TypeScript
Raw Normal View History

2021-09-09 18:39:18 +02:00
import { Component, Input, OnInit, OnChanges, ViewChild } from '@angular/core';
2021-09-09 16:39:05 +02:00
import { MatButton } from '@angular/material/button';
2021-09-09 15:49:49 +02:00
import { MatTableDataSource } from '@angular/material/table';
import { AccountEntryService, AccountService } from '../data-object-service';
import { Account, AccountEntry, NULL_AccountEntry } from '../data-objects';
2021-09-09 18:00:24 +02:00
import { ExtApiService } from '../ext-data-object-service';
import { Saldo } from '../ext-data-objects';
2021-09-09 15:49:49 +02:00
import { MessageService } from '../message.service';
@Component({
selector: 'app-account',
templateUrl: './account.component.html',
styleUrls: ['./account.component.css']
})
export class AccountComponent implements OnInit {
@Input() selectedAccountId: number
2021-09-09 16:39:05 +02:00
@ViewChild('addAccountEntryButton') addAccountEntryButton: MatButton
2021-09-09 15:49:49 +02:00
2021-09-09 15:52:38 +02:00
collapse: boolean = false
2021-09-09 15:49:49 +02:00
account: Account
accountEntries: AccountEntry[]
accountEntriesDataSource: MatTableDataSource<AccountEntry>
accountEntriesDisplayedColumns: string[] = [ "description", "amount", "createdAt" ]
2021-09-09 18:00:24 +02:00
saldo: Saldo
2021-09-09 15:49:49 +02:00
newAccountEntry: AccountEntry = NULL_AccountEntry
constructor(
private accountService: AccountService,
private accountEntryService: AccountEntryService,
2021-09-09 18:00:24 +02:00
private extApiService: ExtApiService,
2021-09-09 15:49:49 +02:00
private messageService: MessageService
) { }
2021-09-09 16:39:05 +02:00
async getAccount(): Promise<void> {
2021-09-09 15:49:49 +02:00
try {
if (this.selectedAccountId) {
this.messageService.add(`Trying to load account ${this.selectedAccountId} and entries`)
this.account = await this.accountService.getAccount(this.selectedAccountId)
this.messageService.add(`Account: ${JSON.stringify(this.account, undefined, 4)}`)
2021-09-09 16:39:05 +02:00
this.getAccountEntries()
2021-09-09 15:49:49 +02:00
}
} catch (err) {
2021-09-09 16:39:05 +02:00
this.messageService.add(`Error in getAccount: ${JSON.stringify(err, undefined, 4)}`)
2021-09-09 15:49:49 +02:00
}
}
2021-09-09 16:39:05 +02:00
async getAccountEntries(): Promise<void> {
try {
this.accountEntries = await this.accountEntryService.getAccountEntrysByAccount(this.selectedAccountId)
2021-09-09 18:00:24 +02:00
this.accountEntries.reverse()
2021-09-09 16:39:05 +02:00
this.messageService.add(`AccountEntries: ${JSON.stringify(this.accountEntries, undefined, 4)}`)
this.accountEntriesDataSource = new MatTableDataSource<AccountEntry>(this.accountEntries)
2021-09-09 18:00:24 +02:00
this.saldo = await this.extApiService.getAccountSaldo(this.selectedAccountId)
2021-09-09 16:39:05 +02:00
} catch (err) {
this.messageService.add(`Error in getAccountEntries: ${JSON.stringify(err, undefined, 4)}`)
}
}
2021-09-09 15:49:49 +02:00
2021-09-09 16:39:05 +02:00
async addAccountEntry(): Promise<void> {
try {
this.addAccountEntryButton.disabled = true
this.newAccountEntry.account = this.account.id
this.messageService.add(`addAccountEntry: ${ JSON.stringify(this.newAccountEntry, undefined, 4) }`)
this.newAccountEntry = await this.accountEntryService.postAccountEntry(this.newAccountEntry)
this.messageService.add(`New accountEntry created: ${this.newAccountEntry.id}`)
2021-09-09 18:00:24 +02:00
this.newAccountEntry = { 'account': this.account.id, 'amount': undefined, 'created_at': '', 'description': '', 'id': 0 }
2021-09-09 16:39:05 +02:00
this.getAccountEntries()
} catch (err) {
this.messageService.add(`Error in addAccountEntry: ${JSON.stringify(err, undefined, 4)}`)
} finally {
this.addAccountEntryButton.disabled = false
}
2021-09-09 15:49:49 +02:00
}
ngOnInit(): void {
this.messageService.add(`AccountComponent.ngOnInit, account: ${this.selectedAccountId}`)
2021-09-09 16:39:05 +02:00
this.getAccount()
2021-09-09 15:49:49 +02:00
}
2021-09-09 18:39:18 +02:00
ngOnChanges(): void {
this.messageService.add(`AccountComponent.ngOnChanges, account: ${this.selectedAccountId}`)
this.getAccount()
}
2021-09-09 15:49:49 +02:00
}