accountant-ui/src/operations/operationList.component.ts

159 lines
3.8 KiB
TypeScript
Raw Normal View History

2017-07-30 16:35:15 +02:00
// vim: set tw=80 ts=2 sw=2 sts=2 :
2017-07-30 09:21:50 +02:00
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';
2017-07-30 09:21:50 +02:00
import { Account } from '../accounts/account';
2017-07-29 22:58:34 +02:00
import { AccountService } from '../accounts/account.service';
import { Operation } from './operation';
import { OperationService } from './operation.service';
import { OperationEditModalComponent } from './operationEditModal.component';
2017-07-30 15:13:36 +02:00
@Component({
2017-07-30 16:35:15 +02:00
selector: 'operation-list',
template: `
2017-07-30 15:13:36 +02:00
<div>
<div class="row">
<div class="col-md-9">
<balance-chart (onUpdate)="onUpdate($event)"
[account]="account"></balance-chart>
</div>
2017-07-30 16:35:15 +02:00
2017-07-30 15:13:36 +02:00
<div class="col-md-3">
<category-chart
[minDate]="minDate"
[maxDate]="maxDate"
[account]="account"></category-chart>
</div>
</div>
<div class="row">
<table class="table table-striped table-condensed table-hover">
<thead>
<tr>
2017-07-30 15:34:12 +02:00
<th>#</th>
2017-07-30 15:13:36 +02:00
<th>Date d'op.</th>
<th>Libell&eacute; de l'op&eacute;ration</th>
<th>Montant</th>
<th>Solde</th>
<th>Cat&eacute;gorie</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="6">
<button class="btn btn-success" (click)="add()">
Ajouter
</button>
</td>
</tr>
<tr [operation-row]="operation"
[account]="account"
(needsReload)="load(minDate, maxDate)"
*ngFor="let operation of operations">
</tr>
</tbody>
</table>
</div>
</div>
`
})
export class OperationListComponent implements OnInit {
2017-07-30 16:35:15 +02:00
private account: Account;
private minDate: Date;
private maxDate: Date;
private operations: Operation[];
constructor(
@Inject("accountIdService") private accountIdService,
private toastrService: ToastrService,
private operationService: OperationService,
private accountService: AccountService,
private logger: Logger,
private ngbModal: NgbModal,
) {}
ngOnInit() {
this.accountService.get(this.accountIdService.get()).subscribe(account => {
this.account = account
});
}
/*
* Add an empty operation.
*/
add() {
var operation = new Operation();
operation.account_id = this.accountIdService.get();
return this.modify(operation);
};
/*
* Load operations.
*/
load(minDate, maxDate) {
this.minDate = minDate;
this.maxDate = maxDate;
return this.operationService.query(
this.accountIdService.get(),
minDate,
maxDate
).subscribe((operations: Operation[]) => {
this.operations = operations.reverse();
});
};
/*
* Save an operation and return a promise.
*/
save(operation) {
operation.confirmed = true;
var observable: Observable<Operation>;
if(operation.id){
observable = this.operationService.update(operation);
} else {
observable = this.operationService.create(operation);
2017-07-30 09:21:50 +02:00
}
2017-07-30 16:35:15 +02:00
return observable.subscribe((operation) => {
this.toastrService.success('Operation #' + operation.id + ' saved.');
this.load(this.minDate, this.maxDate);
return operation;
}, (result) => {
this.toastrService.error(
'Error while saving operation: ' + result.message
);
});
};
modify(operation) {
// FIXME Alexis Lahouze 2017-06-15 i18n
const modal = this.ngbModal.open(OperationEditModalComponent, {
windowClass: 'in'
});
modal.componentInstance.operation = operation;
modal.result.then((operation: Operation) => {
this.save(operation);
}, (reason) => {
});
};
onUpdate(dateRange) {
this.load(dateRange.minDate, dateRange.maxDate);
};
};