diff --git a/ui/hv2-ui/src/app/account/account.component.ts b/ui/hv2-ui/src/app/account/account.component.ts index 7333370..e9db08e 100644 --- a/ui/hv2-ui/src/app/account/account.component.ts +++ b/ui/hv2-ui/src/app/account/account.component.ts @@ -1,4 +1,5 @@ -import { Component, Input, OnInit } from '@angular/core'; +import { Component, Input, OnInit, ViewChild } from '@angular/core'; +import { MatButton } from '@angular/material/button'; import { MatTableDataSource } from '@angular/material/table'; import { AccountEntryService, AccountService } from '../data-object-service'; import { Account, AccountEntry, NULL_AccountEntry } from '../data-objects'; @@ -12,6 +13,7 @@ import { MessageService } from '../message.service'; export class AccountComponent implements OnInit { @Input() selectedAccountId: number + @ViewChild('addAccountEntryButton') addAccountEntryButton: MatButton collapse: boolean = false @@ -29,28 +31,48 @@ export class AccountComponent implements OnInit { private messageService: MessageService ) { } - async getAccountAndEntries(): Promise { + async getAccount(): Promise { 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)}`) - this.accountEntries = await this.accountEntryService.getAccountEntrysByAccount(this.selectedAccountId) - this.messageService.add(`AccountEntries: ${JSON.stringify(this.accountEntries, undefined, 4)}`) - this.accountEntriesDataSource = new MatTableDataSource(this.accountEntries) + this.getAccountEntries() } } catch (err) { - this.messageService.add(`Error in getAccountAndEntries: ${JSON.stringify(err, undefined, 4)}`) + this.messageService.add(`Error in getAccount: ${JSON.stringify(err, undefined, 4)}`) } } - addAccountEntry() { + async getAccountEntries(): Promise { + try { + this.accountEntries = await this.accountEntryService.getAccountEntrysByAccount(this.selectedAccountId) + this.messageService.add(`AccountEntries: ${JSON.stringify(this.accountEntries, undefined, 4)}`) + this.accountEntriesDataSource = new MatTableDataSource(this.accountEntries) + } catch (err) { + this.messageService.add(`Error in getAccountEntries: ${JSON.stringify(err, undefined, 4)}`) + } + } + async addAccountEntry(): Promise { + 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}`) + this.newAccountEntry = { 'account': this.account.id, 'amount': 0, 'created_at': '', 'description': '', 'id': 0 } + this.getAccountEntries() + } catch (err) { + this.messageService.add(`Error in addAccountEntry: ${JSON.stringify(err, undefined, 4)}`) + } finally { + this.addAccountEntryButton.disabled = false + } } ngOnInit(): void { this.messageService.add(`AccountComponent.ngOnInit, account: ${this.selectedAccountId}`) - this.getAccountAndEntries() + this.getAccount() } }