// vim: set tw=80 ts=2 sw=2 sts=2 :
import { Component, Inject, OnInit } from '@angular/core';
import { Observable } from 'rxjs/Rx';
import { Logger } from '@nsalaun/ng-logger';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { ToastrService } from 'ngx-toastr';
import { Account } from './account';
import { AccountBalances } from './accountBalances';
import { AccountService } from './account.service';
import { AccountEditModalComponent } from './accountEditModal.component';
@Component({
selector: 'account-list',
template: `
Nom du compte |
Solde courant |
Solde pointé |
Découvert autorisé |
Actions |
|
`,
})
export class AccountListComponent implements OnInit {
accounts: Account[];
constructor(
private AccountService: AccountService,
private ToastrService: ToastrService,
private Logger: Logger,
private NgbModal: NgbModal
) {
}
ngOnInit() {
// Load accounts.
this.load();
}
load() {
this.AccountService.query().subscribe(accounts => {
this.accounts = accounts;
});
};
/*
* Add an empty account.
*/
add() {
const modal = this.NgbModal.open(AccountEditModalComponent, {
windowClass: 'in'
});
modal.componentInstance.account = new Account();
modal.result.then((account: Account) => {
this.Logger.log("Modal closed => save account", account);
this.save(account);
}, (reason) => function(reason) {
});
};
/*
* Save account.
*/
save(account) {
this.AccountService.create(account).subscribe(account => {
this.ToastrService.success('Account #' + account.id + ' saved.');
this.load();
}, result => {
this.Logger.error('Error while saving account', account, result);
this.ToastrService.error(
'Error while saving account: ' + result.message
);
});
};
};