2021-09-09 16:39:05 +02:00
|
|
|
import { Component, Input, OnInit, ViewChild } from '@angular/core';
|
|
|
|
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';
|
|
|
|
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" ]
|
|
|
|
|
|
|
|
newAccountEntry: AccountEntry = NULL_AccountEntry
|
|
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private accountService: AccountService,
|
|
|
|
private accountEntryService: AccountEntryService,
|
|
|
|
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)
|
|
|
|
this.messageService.add(`AccountEntries: ${JSON.stringify(this.accountEntries, undefined, 4)}`)
|
|
|
|
this.accountEntriesDataSource = new MatTableDataSource<AccountEntry>(this.accountEntries)
|
|
|
|
} 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}`)
|
|
|
|
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
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|