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-08 12:20:17 +02:00
|
|
|
var scheduleFormTmpl = require('./schedule.form.tmpl.html'),
|
|
|
|
scheduleDeleteTmpl = require('./schedule.delete.tmpl.html');
|
|
|
|
|
2017-07-23 07:44:03 +02:00
|
|
|
export class ScheduleComponent {
|
2017-07-23 00:12:44 +02:00
|
|
|
accountId: number;
|
2017-07-21 23:06:59 +02:00
|
|
|
operations = [];
|
|
|
|
|
|
|
|
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-21 23:06:59 +02:00
|
|
|
private $modal
|
2017-07-23 00:12:44 +02:00
|
|
|
) {}
|
|
|
|
|
|
|
|
$onInit = function() {
|
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-21 23:06:59 +02:00
|
|
|
add = function() {
|
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-22 11:16:04 +02:00
|
|
|
return this.modify(schedule);
|
2017-07-08 12:20:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load operations.
|
|
|
|
*/
|
2017-07-21 23:06:59 +02:00
|
|
|
load = function() {
|
2017-07-23 00:12:44 +02:00
|
|
|
return this.scheduleService.query(this.accountId)
|
2017-07-22 11:16:04 +02:00
|
|
|
.subscribe((schedules: Schedule[]) => {
|
|
|
|
this.operations = 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
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Save operation.
|
|
|
|
*/
|
2017-07-22 11:16:04 +02:00
|
|
|
save = function(operation: Schedule) {
|
|
|
|
let subscription: Observable<Schedule>;
|
|
|
|
|
|
|
|
if(operation.id) {
|
2017-07-22 14:29:29 +02:00
|
|
|
this.logger.log("updating schedule", operation);
|
2017-07-22 11:16:04 +02:00
|
|
|
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);
|
2017-07-22 11:16:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return subscription.subscribe((operation: Schedule) => {
|
2017-07-22 09:17:50 +02:00
|
|
|
this.toastrService.success('Scheduled operation #' + operation.id + ' saved.');
|
2017-07-08 12:20:17 +02:00
|
|
|
|
2017-07-22 11:16:04 +02:00
|
|
|
this.load();
|
2017-07-08 12:20:17 +02:00
|
|
|
|
|
|
|
return operation;
|
2017-07-22 09:17:50 +02:00
|
|
|
}, (result) => {
|
|
|
|
this.toastrService.error(
|
2017-07-08 12:20:17 +02:00
|
|
|
'Error while saving scheduled operation: ' + result.message
|
|
|
|
);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete an operation and return a promise.
|
|
|
|
*/
|
2017-07-22 11:16:04 +02:00
|
|
|
confirmDelete = function(operation: Schedule) {
|
2017-07-08 12:20:17 +02:00
|
|
|
var title = "Delete operation #" + operation.id;
|
|
|
|
|
2017-07-21 23:06:59 +02:00
|
|
|
this.$modal({
|
2017-07-08 12:20:17 +02:00
|
|
|
templateUrl: scheduleDeleteTmpl,
|
|
|
|
controller: function($scope, title, operation, $delete) {
|
|
|
|
$scope.title = title;
|
|
|
|
$scope.operation = operation;
|
2017-07-22 09:17:50 +02:00
|
|
|
$scope.$delete = () => {
|
2017-07-08 12:20:17 +02:00
|
|
|
$scope.$hide();
|
|
|
|
$delete($scope.operation);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
locals: {
|
|
|
|
title: title,
|
|
|
|
operation: operation,
|
2017-07-22 09:17:50 +02:00
|
|
|
$delete: (operation) => {
|
|
|
|
this.delete(operation);
|
|
|
|
}
|
2017-07-08 12:20:17 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete operation.
|
|
|
|
*/
|
2017-07-22 11:16:04 +02:00
|
|
|
delete = function(operation: Schedule) {
|
2017-07-08 12:20:17 +02:00
|
|
|
var id = operation.id;
|
|
|
|
|
2017-07-22 11:16:04 +02:00
|
|
|
return this.scheduleService.delete(operation).subscribe(() => {
|
2017-07-22 09:17:50 +02:00
|
|
|
this.toastrService.success('Scheduled operation #' + id + ' deleted.');
|
2017-07-08 12:20:17 +02:00
|
|
|
|
2017-07-22 11:16:04 +02:00
|
|
|
this.load();
|
2017-07-08 12:20:17 +02:00
|
|
|
|
|
|
|
return operation;
|
2017-07-22 09:17:50 +02:00
|
|
|
}, (result) => {
|
|
|
|
this.toastrService.error(
|
2017-07-08 12:20:17 +02:00
|
|
|
'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-22 11:16:04 +02:00
|
|
|
modify = function(operation: Schedule) {
|
2017-07-08 12:20:17 +02:00
|
|
|
// FIXME Alexis Lahouze 2017-06-15 i18n
|
|
|
|
var title = "Operation";
|
|
|
|
|
|
|
|
if (operation.id) {
|
|
|
|
title = title + " #" + operation.id;
|
|
|
|
}
|
|
|
|
|
2017-07-21 23:06:59 +02:00
|
|
|
this.$modal({
|
2017-07-08 12:20:17 +02:00
|
|
|
templateUrl: scheduleFormTmpl,
|
|
|
|
controller: function($scope, title, operation, $save) {
|
|
|
|
$scope.title = title;
|
|
|
|
$scope.operation = operation;
|
2017-07-22 09:17:50 +02:00
|
|
|
$scope.$save = () => {
|
2017-07-08 12:20:17 +02:00
|
|
|
$scope.$hide();
|
|
|
|
$save($scope.operation);
|
|
|
|
};
|
|
|
|
},
|
|
|
|
locals: {
|
|
|
|
title: title,
|
|
|
|
operation: operation,
|
2017-07-22 09:17:50 +02:00
|
|
|
$save: (operation) => {
|
|
|
|
this.save(operation);
|
|
|
|
}
|
2017-07-08 12:20:17 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|