accountant-ui/src/scheduler/schedule.component.ts

159 lines
4.3 KiB
TypeScript
Raw Normal View History

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';
import { ScheduleService } from './schedule.service';
import { Schedule } from './schedule';
var scheduleFormTmpl = require('./schedule.form.tmpl.html'),
scheduleDeleteTmpl = require('./schedule.delete.tmpl.html');
2017-07-23 07:44:03 +02:00
export class ScheduleComponent {
accountId: number;
operations = [];
constructor(
2017-07-22 09:17:50 +02:00
private toastrService: ToastrService,
private scheduleService: ScheduleService,
2017-07-22 14:29:29 +02:00
private logger: Logger,
private $modal,
private accountIdService
) {}
2017-07-23 07:53:26 +02:00
$onInit() {
this.accountId = this.accountIdService.get();
// Load operations on controller initialization.
this.load();
}
/*
* Add a new operation at the beginning of th array.
*/
2017-07-23 07:53:26 +02:00
add() {
var schedule = new Schedule();
schedule.account_id = this.accountId;
return this.modify(schedule);
};
/*
* Load operations.
*/
2017-07-23 07:53:26 +02:00
load() {
return this.scheduleService.query(this.accountId)
.subscribe((schedules: Schedule[]) => {
this.operations = schedules;
}, (reason) => {
this.logger.log("Got error", reason);
}
);
};
/*
* Save operation.
*/
2017-07-23 07:53:26 +02:00
save(operation: Schedule) {
let subscription: Observable<Schedule>;
if(operation.id) {
2017-07-22 14:29:29 +02:00
this.logger.log("updating schedule", operation);
subscription = this.scheduleService.update(operation);
2017-07-22 14:29:29 +02:00
} else {
this.logger.log("creating schedule", operation);
subscription = this.scheduleService.create(operation);
}
return subscription.subscribe((operation: Schedule) => {
2017-07-22 09:17:50 +02:00
this.toastrService.success('Scheduled operation #' + operation.id + ' saved.');
this.load();
return operation;
2017-07-22 09:17:50 +02:00
}, (result) => {
this.toastrService.error(
'Error while saving scheduled operation: ' + result.message
);
});
};
/*
* Delete an operation and return a promise.
*/
2017-07-23 07:53:26 +02:00
confirmDelete(operation: Schedule) {
var title = "Delete operation #" + operation.id;
this.$modal({
templateUrl: scheduleDeleteTmpl,
controller: function($scope, title, operation, $delete) {
$scope.title = title;
$scope.operation = operation;
2017-07-22 09:17:50 +02:00
$scope.$delete = () => {
$scope.$hide();
$delete($scope.operation);
};
},
locals: {
title: title,
operation: operation,
2017-07-22 09:17:50 +02:00
$delete: (operation) => {
this.delete(operation);
}
}
});
};
/*
* Delete operation.
*/
2017-07-23 07:53:26 +02:00
delete(operation: Schedule) {
var id = operation.id;
return this.scheduleService.delete(operation).subscribe(() => {
2017-07-22 09:17:50 +02:00
this.toastrService.success('Scheduled operation #' + id + ' deleted.');
this.load();
return operation;
2017-07-22 09:17:50 +02:00
}, (result) => {
this.toastrService.error(
'An error occurred while trying to delete scheduled operation #' +
id + ':<br />' + result
);
});
};
/*
* Open the popup to modify the operation, save it on confirm.
* @returns a promise.
*/
2017-07-23 07:53:26 +02:00
modify(operation: Schedule) {
// FIXME Alexis Lahouze 2017-06-15 i18n
var title = "Operation";
if (operation.id) {
title = title + " #" + operation.id;
}
this.$modal({
templateUrl: scheduleFormTmpl,
controller: function($scope, title, operation, $save) {
$scope.title = title;
$scope.operation = operation;
2017-07-22 09:17:50 +02:00
$scope.$save = () => {
$scope.$hide();
$save($scope.operation);
};
},
locals: {
title: title,
operation: operation,
2017-07-22 09:17:50 +02:00
$save: (operation) => {
this.save(operation);
}
}
});
};
};