accountant-ui/src/accounts/accountList.component.ts

105 lines
2.4 KiB
TypeScript
Raw Normal View History

2017-07-21 00:45:04 +02:00
// vim: set tw=80 ts=2 sw=2 sts=2 :
import { Component, Inject, OnInit } from '@angular/core';
2017-07-14 10:24:46 +02:00
import { Observable } from 'rxjs/Rx';
2017-07-16 22:25:50 +02:00
import { Logger } from '@nsalaun/ng-logger';
2017-07-20 10:32:05 +02:00
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { ToastrService } from 'ngx-toastr';
2017-07-14 10:24:46 +02:00
import { Account } from './account';
import { AccountBalances } from './accountBalances';
import { AccountService } from './account.service';
2017-07-20 10:32:05 +02:00
import { AccountEditModalComponent } from './accountEditModal.component';
@Component({
2017-07-21 00:45:04 +02:00
selector: 'account-list',
template: `
<div class="row">
<div class="col s12">
2017-08-27 18:19:46 +02:00
<table class="bordered highlight responsive-table">
<thead>
<tr>
<th>Nom du compte</th>
<th>Solde courant</th>
<th>Solde pointé</th>
<th>Découvert autorisé</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="5">
2017-08-27 20:02:08 +02:00
<button mz-button class="green" (click)="add()">
Ajouter
</button>
</td>
</tr>
<tr *ngFor="let account of accounts"
[account-row]="account" (needsReload)="load()">
</tr>
</tbody>
</table>
</div>
</div>
`,
})
export class AccountListComponent implements OnInit {
2017-07-21 00:45:04 +02:00
accounts: Account[];
constructor(
2017-07-22 08:42:17 +02:00
private accountService: AccountService,
private toastrService: ToastrService,
private logger: Logger,
private ngbModal: NgbModal
2017-07-21 00:45:04 +02:00
) {
}
ngOnInit() {
2017-07-21 00:45:04 +02:00
// Load accounts.
this.load();
}
load() {
2017-07-22 08:42:17 +02:00
this.logger.log("Load accounts.");
this.accountService.query().subscribe(accounts => {
this.accounts = accounts;
2017-07-21 00:45:04 +02:00
});
};
/*
* Add an empty account.
*/
add() {
2017-08-08 23:37:45 +02:00
const modal = this.ngbModal.open(AccountEditModalComponent, {
size: 'lg'
});
2017-07-21 00:45:04 +02:00
modal.componentInstance.account = new Account();
modal.result.then((account: Account) => {
2017-07-22 08:42:17 +02:00
this.logger.log("Modal closed => save account", account);
2017-07-21 00:45:04 +02:00
this.save(account);
}, (reason) => function(reason) {
});
};
/*
* Save account.
*/
save(account) {
2017-07-22 08:42:17 +02:00
this.accountService.create(account).subscribe(account => {
this.toastrService.success('Account #' + account.id + ' saved.');
2017-07-21 00:45:04 +02:00
this.load();
}, result => {
2017-07-22 08:42:17 +02:00
this.logger.error('Error while saving account', account, result);
2017-07-21 00:45:04 +02:00
2017-07-22 08:42:17 +02:00
this.toastrService.error(
2017-07-21 00:45:04 +02:00
'Error while saving account: ' + result.message
);
});
};
};