2017-07-25 12:03:42 +02:00
|
|
|
import { Component, Inject, OnInit } from '@angular/core';
|
2017-07-22 11:16:04 +02:00
|
|
|
import { Observable } from 'rxjs/Rx';
|
|
|
|
|
2017-07-22 09:17:50 +02:00
|
|
|
import { Logger } from '@nsalaun/ng-logger';
|
|
|
|
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
|
|
|
|
import { ToastrService } from 'ngx-toastr';
|
|
|
|
|
2017-07-22 11:16:04 +02:00
|
|
|
import { ScheduleService } from './schedule.service';
|
|
|
|
import { Schedule } from './schedule';
|
|
|
|
|
2017-07-25 12:03:42 +02:00
|
|
|
var scheduleFormTmpl = require('./schedule.form.tmpl.html');
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'schedule-list',
|
|
|
|
template: `
|
|
|
|
<div class="row">
|
|
|
|
<table class="table table-striped table-condensed table-hover">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th class="col-md-1">Date de début</th>
|
|
|
|
<th class="col-md-1">Date de fin</th>
|
|
|
|
<th class="col-md-1">Jour</th>
|
|
|
|
<th class="col-md-1">Fréq.</th>
|
|
|
|
<th>Libellé de l'opération</th>
|
|
|
|
<th class="col-md-1">Montant</th>
|
|
|
|
<th class="col-md-2">Catégorie</th>
|
|
|
|
<th class="col-md-1">Actions</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
|
|
|
|
<tbody>
|
|
|
|
<tr>
|
|
|
|
<td colspan="8">
|
|
|
|
<button class="btn btn-success" (click)="add()">
|
|
|
|
Ajouter
|
|
|
|
</button>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
|
|
|
|
<tr *ngFor="let schedule of schedules"
|
|
|
|
[schedule-row]="schedule" (needsReload)="load()">
|
|
|
|
</tr>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
`
|
|
|
|
})
|
|
|
|
export class ScheduleComponent implements OnInit {
|
2017-07-23 00:12:44 +02:00
|
|
|
accountId: number;
|
2017-07-25 12:03:42 +02:00
|
|
|
schedules = [];
|
2017-07-21 23:06:59 +02:00
|
|
|
|
|
|
|
constructor(
|
2017-07-22 09:17:50 +02:00
|
|
|
private toastrService: ToastrService,
|
2017-07-22 11:16:04 +02:00
|
|
|
private scheduleService: ScheduleService,
|
2017-07-22 14:29:29 +02:00
|
|
|
private logger: Logger,
|
2017-07-25 12:03:42 +02:00
|
|
|
@Inject('$modal') private $modal,
|
|
|
|
@Inject('accountIdService') private accountIdService
|
2017-07-23 00:12:44 +02:00
|
|
|
) {}
|
|
|
|
|
2017-07-23 07:53:26 +02:00
|
|
|
$onInit() {
|
2017-07-25 12:03:42 +02:00
|
|
|
this.ngOnInit();
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.logger.log("ngOnInit");
|
2017-07-25 10:06:41 +02:00
|
|
|
this.accountId = this.accountIdService.get();
|
2017-07-21 23:06:59 +02:00
|
|
|
// Load operations on controller initialization.
|
2017-07-22 11:16:04 +02:00
|
|
|
this.load();
|
2017-07-21 23:06:59 +02:00
|
|
|
}
|
2017-07-08 12:20:17 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a new operation at the beginning of th array.
|
|
|
|
*/
|
2017-07-23 07:53:26 +02:00
|
|
|
add() {
|
2017-07-22 11:16:04 +02:00
|
|
|
var schedule = new Schedule();
|
2017-07-23 00:12:44 +02:00
|
|
|
schedule.account_id = this.accountId;
|
2017-07-08 12:20:17 +02:00
|
|
|
|
2017-07-25 12:03:42 +02:00
|
|
|
var title = "New schedule";
|
|
|
|
|
|
|
|
this.$modal({
|
|
|
|
templateUrl: scheduleFormTmpl,
|
|
|
|
controller: function($scope, title, schedule, $save) {
|
|
|
|
$scope.title = title;
|
|
|
|
$scope.operation = schedule;
|
|
|
|
$scope.$save = () => {
|
|
|
|
$scope.$hide();
|
|
|
|
$save($scope.operation);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
locals: {
|
|
|
|
title: title,
|
|
|
|
schedule: schedule,
|
|
|
|
$save: (schedule) => {
|
|
|
|
this.save(schedule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2017-07-08 12:20:17 +02:00
|
|
|
};
|
|
|
|
|
2017-07-23 07:53:26 +02:00
|
|
|
load() {
|
2017-07-25 12:03:42 +02:00
|
|
|
this.logger.log("Loading schedules for accountId", this.accountId);
|
|
|
|
if(!this.accountId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.scheduleService.query(this.accountId)
|
2017-07-22 11:16:04 +02:00
|
|
|
.subscribe((schedules: Schedule[]) => {
|
2017-07-25 12:03:42 +02:00
|
|
|
this.logger.log("Schedules loaded.", schedules);
|
|
|
|
this.schedules = schedules;
|
2017-07-23 00:12:44 +02:00
|
|
|
}, (reason) => {
|
|
|
|
this.logger.log("Got error", reason);
|
2017-07-22 11:16:04 +02:00
|
|
|
}
|
|
|
|
);
|
2017-07-08 12:20:17 +02:00
|
|
|
};
|
|
|
|
|
2017-07-25 12:03:42 +02:00
|
|
|
save(schedule: Schedule) {
|
|
|
|
return this.scheduleService.update(schedule).subscribe((schedule: Schedule) => {
|
|
|
|
this.toastrService.success('Schedule #' + schedule.id + ' saved.');
|
2017-07-08 12:20:17 +02:00
|
|
|
|
2017-07-22 11:16:04 +02:00
|
|
|
this.load();
|
2017-07-22 09:17:50 +02:00
|
|
|
}, (result) => {
|
|
|
|
this.toastrService.error(
|
2017-07-25 12:03:42 +02:00
|
|
|
'Error while saving schedule: ' + result.message
|
2017-07-08 12:20:17 +02:00
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|