2017-07-25 08:55:34 +02:00
|
|
|
// vim: set tw=80 ts=2 sw=2 sts=2 :
|
|
|
|
import { CurrencyPipe } from '@angular/common';
|
2017-07-25 16:14:48 +02:00
|
|
|
import { Component, Inject, Input, Output, EventEmitter } from '@angular/core';
|
2017-07-25 08:55:34 +02:00
|
|
|
|
|
|
|
import { Logger } from '@nsalaun/ng-logger';
|
2017-07-25 15:58:54 +02:00
|
|
|
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
|
2017-07-25 08:55:34 +02:00
|
|
|
import { ToastrService } from 'ngx-toastr';
|
|
|
|
|
2017-07-25 15:58:54 +02:00
|
|
|
import { ScheduleDeleteModalComponent } from './scheduleDeleteModal.component';
|
2017-07-25 17:18:04 +02:00
|
|
|
import { ScheduleEditModalComponent } from './scheduleEditModal.component';
|
2017-07-25 08:55:34 +02:00
|
|
|
import { ScheduleService } from './schedule.service';
|
|
|
|
import { Schedule } from './schedule';
|
|
|
|
|
2017-07-25 15:58:54 +02:00
|
|
|
var scheduleFormTmpl = require('./schedule.form.tmpl.html');
|
2017-07-25 08:55:34 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'tr[schedule-row]',
|
|
|
|
host: {
|
|
|
|
"[id]": "schedule.id",
|
|
|
|
},
|
|
|
|
template: `
|
|
|
|
<td class="col-md-1">{{ schedule.start_date | date: "yyyy-MM-dd" }}</td>
|
|
|
|
|
|
|
|
|
|
|
|
<td>{{ schedule.stop_date | date: "yyyy-MM-dd" }}</td>
|
|
|
|
|
|
|
|
<td>{{ schedule.day }}</td>
|
|
|
|
|
|
|
|
<td>{{ schedule.frequency }}</td>
|
|
|
|
|
|
|
|
<td>{{ schedule.label }}</td>
|
|
|
|
|
|
|
|
<td>{{ schedule.value | currency:"EUR":true }}</td>
|
|
|
|
|
|
|
|
<td>{{ schedule.category }}</td>
|
|
|
|
|
|
|
|
<td>
|
|
|
|
<div class="btn-group btn-group-xs">
|
|
|
|
<!-- Edit operation. -->
|
|
|
|
<button type="button" class="btn btn-default"
|
|
|
|
(click)="modify()" title="edit">
|
|
|
|
<span class="fa fa-pencil-square-o"></span>
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<!-- Remove operation. -->
|
|
|
|
<button type="button" class="btn btn-default"
|
|
|
|
[hidden]="!schedule.id"
|
|
|
|
(click)="confirmDelete()"
|
|
|
|
title="remove">
|
|
|
|
<span class="fa fa-trash"></span>
|
|
|
|
</button>
|
|
|
|
</div>
|
|
|
|
</td>
|
|
|
|
`
|
|
|
|
})
|
|
|
|
export class ScheduleRowComponent {
|
|
|
|
@Input('schedule-row') schedule: Schedule;
|
|
|
|
@Output() needsReload: EventEmitter<void> = new EventEmitter<void>();
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private scheduleService: ScheduleService,
|
|
|
|
private logger: Logger,
|
|
|
|
private toastrService: ToastrService,
|
2017-07-25 15:58:54 +02:00
|
|
|
@Inject('$modal') private $modal,
|
|
|
|
private ngbModal: NgbModal
|
2017-07-25 08:55:34 +02:00
|
|
|
) {}
|
|
|
|
|
|
|
|
save(schedule: Schedule) {
|
|
|
|
return this.scheduleService.create(schedule).subscribe((schedule: Schedule) => {
|
|
|
|
this.toastrService.success('Schedule #' + schedule.id + ' saved.');
|
|
|
|
|
|
|
|
this.needsReload.emit();
|
|
|
|
}, result => {
|
|
|
|
this.toastrService.error(
|
|
|
|
'Error while saving schedule: ' + result.message
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
confirmDelete() {
|
|
|
|
var title = "Delete schedule #" + this.schedule.id;
|
|
|
|
|
2017-07-25 15:58:54 +02:00
|
|
|
const modal = this.ngbModal.open(ScheduleDeleteModalComponent, {
|
|
|
|
windowClass: 'in'
|
|
|
|
});
|
|
|
|
|
|
|
|
modal.componentInstance.schedule = this.schedule;
|
|
|
|
|
|
|
|
modal.result.then((schedule: Schedule) => {
|
|
|
|
this.delete(schedule);
|
|
|
|
}, (reason) => function(reason) {
|
2017-07-25 08:55:34 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
delete(schedule: Schedule) {
|
|
|
|
var id = schedule.id;
|
|
|
|
|
|
|
|
return this.scheduleService.delete(schedule).subscribe(() => {
|
|
|
|
this.toastrService.success('Schedule #' + id + ' deleted.');
|
|
|
|
|
|
|
|
this.needsReload.emit();
|
|
|
|
}, result => {
|
|
|
|
this.toastrService.error(
|
|
|
|
'An error occurred while trying to delete schedule #' + id + ':<br />'
|
|
|
|
+ result.message
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
modify() {
|
|
|
|
// FIXME Alexis Lahouze 2017-06-15 i18n
|
|
|
|
var title = "Modify schedule #" + this.schedule.id;
|
|
|
|
|
|
|
|
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: this.schedule,
|
|
|
|
$save: (operation) => {
|
|
|
|
this.save(operation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|