accountant-ui/src/scheduler/scheduleRow.component.ts
2017-07-25 23:33:29 +02:00

118 lines
3.0 KiB
TypeScript

// vim: set tw=80 ts=2 sw=2 sts=2 :
import { CurrencyPipe } from '@angular/common';
import { Component, Inject, Input, Output, EventEmitter } from '@angular/core';
import { Logger } from '@nsalaun/ng-logger';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { ToastrService } from 'ngx-toastr';
import { ScheduleDeleteModalComponent } from './scheduleDeleteModal.component';
import { ScheduleEditModalComponent } from './scheduleEditModal.component';
import { ScheduleService } from './schedule.service';
import { Schedule } from './schedule';
@Component({
selector: 'tr[schedule-row]',
host: {
"[id]": "schedule.id",
},
template: `
<td>{{ 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-sm">
<!-- Edit operation. -->
<button type="button" class="btn btn-success"
(click)="modify()" title="edit">
<span class="fa fa-pencil-square-o"></span>
</button>
<!-- Remove operation. -->
<button type="button" class="btn btn-danger"
[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,
private ngbModal: NgbModal
) {}
save(schedule: Schedule) {
return this.scheduleService.update(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() {
const modal = this.ngbModal.open(ScheduleDeleteModalComponent, {
windowClass: 'in'
});
modal.componentInstance.schedule = this.schedule;
modal.result.then((schedule: Schedule) => {
this.delete(schedule);
}, (reason) => function(reason) {
});
}
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() {
const modal = this.ngbModal.open(ScheduleEditModalComponent, {
windowClass: 'in'
});
modal.componentInstance.schedule = this.schedule;
modal.result.then((schedule: Schedule) => {
this.save(schedule);
}, (reason) => function(reason) {
});
}
}