Improve operation workflow.

This commit is contained in:
Alexis Lahouze 2017-06-16 22:26:49 +02:00
parent 4f3c196179
commit e433aed773
2 changed files with 48 additions and 41 deletions

View File

@ -54,13 +54,10 @@ var operationModule = angular.module('accountant.operations', [
* Controller for the operations. * Controller for the operations.
*/ */
.controller('OperationController', function($rootScope, $scope, $routeParams, .controller('OperationController', function($rootScope, $scope, $routeParams,
$uibModal, Notification, Operation, $log) { $uibModal, Notification, Operation, $log, $q) {
var vm = this; var vm = this;
// List of operations.
vm.operations = [];
/* /*
* Add an empty operation. * Add an empty operation.
*/ */
@ -70,14 +67,14 @@ var operationModule = angular.module('accountant.operations', [
account_id: $routeParams.accountId account_id: $routeParams.accountId
}); });
vm.operations.splice(0, 0, operation); return vm.modify(operation);
}; };
/* /*
* Load operations. * Load operations.
*/ */
vm.load = function(begin, end) { vm.load = function(begin, end) {
vm.operations = Operation.query({ return Operation.query({
// eslint-disable-next-line camelcase // eslint-disable-next-line camelcase
account_id: $routeParams.accountId, account_id: $routeParams.accountId,
begin: begin.format('YYYY-MM-DD'), begin: begin.format('YYYY-MM-DD'),
@ -91,10 +88,7 @@ var operationModule = angular.module('accountant.operations', [
vm.togglePointed = function(operation, rowform) { vm.togglePointed = function(operation, rowform) {
operation.pointed = !operation.pointed; operation.pointed = !operation.pointed;
// Save operation if not editing it.
if (!rowform.$visible) {
vm.save(operation); vm.save(operation);
}
}; };
/* /*
@ -107,32 +101,32 @@ var operationModule = angular.module('accountant.operations', [
}; };
/* /*
* Save an operation and emit operationSavedEvent. * Save an operation and return a promise.
*/ */
vm.save = function($data, $index) { vm.save = function(operation) {
// Check if $data is already a resource.
var operation;
if ($data.$save) {
operation = $data;
} else {
operation = vm.operations[$index];
operation = angular.merge(operation, $data);
}
operation.confirmed = true; operation.confirmed = true;
return operation.$save().then(function(data) { return operation.$save().then(function(operation) {
Notification.success('Operation #' + data.id + ' saved.'); Notification.success('Operation #' + operation.id + ' saved.');
$scope.$emit('operationSavedEvent', data); vm.operations = vm.load(moment().date(1).year(2000), moment());
return operation;
}, function(result){
$log.error('Error while saving operation', operation, result);
Notification.error(
'Error while saving operation: ' + result.message
);
return $q.reject(result);
}); });
}; };
/* /*
* Delete an operation and emit operationDeletedEvent. * Delete an operation and return a promise.
*/ */
vm.delete = function(operation, $index) { vm.delete = function(operation) {
var id = operation.id; var id = operation.id;
$uibModal.open({ $uibModal.open({
@ -143,16 +137,31 @@ var operationModule = angular.module('accountant.operations', [
} }
} }
}).result.then(function(operation) { }).result.then(function(operation) {
// FIXME Alexis Lahouze 2017-06-15 Delete operation and reload data return operation.$delete().then(function() {
// to update balances. Notification.success('Operation #' + id + ' deleted.');
$log.info('Delete operation', operation);
vm.operations = vm.load(moment().date(1).year(2000), moment());
return operation;
}, function(result) {
Notification.error(
'An error occurred while trying to delete operation #' +
id + ':<br />' + result
);
return $q.reject(result);
});
}, function() { }, function() {
$log.info('modal-component dismissed at: ' + new Date()); return false;
}); });
}; };
vm.modify = function(operation, $index) { /*
$uibModal.open({ * Open the popup to modify the operation, save it on confirm.
* @returns a promise.
*/
vm.modify = function(operation) {
return $uibModal.open({
component: 'operationModifyModalComponent', component: 'operationModifyModalComponent',
resolve: { resolve: {
operation: function() { operation: function() {
@ -160,11 +169,9 @@ var operationModule = angular.module('accountant.operations', [
} }
} }
}).result.then(function(operation) { }).result.then(function(operation) {
// FIXME Alexis Lahouze 2017-06-15 Save Operation and reload data to return vm.save(operation);
// update balances.
$log.info('Save operation', operation);
}, function() { }, function() {
$log.info('modal-component dismissed at: ' + new Date()); return false;
}); });
}; };
@ -179,7 +186,7 @@ var operationModule = angular.module('accountant.operations', [
vm.onRangeSelected = angular.noop; vm.onRangeSelected = angular.noop;
}); });
vm.load(moment().date(1).year(2000), moment()); vm.operations = vm.load(moment().date(1).year(2000), moment());
}) })
.component('operationModifyModalComponent', { .component('operationModifyModalComponent', {

View File

@ -68,14 +68,14 @@
<!-- Edit operation, for non-canceled operation. --> <!-- Edit operation, for non-canceled operation. -->
<button type="button" class="btn btn-default" <button type="button" class="btn btn-default"
ng-if="!operation.canceled" ng-if="!operation.canceled"
ng-click="operationsCtrl.modify(operation, $index)" title="edit"> ng-click="operationsCtrl.modify(operation)" title="edit">
<span class="fa fa-pencil-square-o"></span> <span class="fa fa-pencil-square-o"></span>
</button> </button>
<!-- Toggle pointed operation, for non-canceled operations. --> <!-- Toggle pointed operation, for non-canceled operations. -->
<button type="button" class="btn btn-default" <button type="button" class="btn btn-default"
ng-if="!operation.canceled" ng-if="!operation.canceled"
ng-click="operationsCtrl.togglePointed(operation, rowform)" ng-click="operationsCtrl.togglePointed(operation)"
ng-class="{active: operation.pointed}" title="point"> ng-class="{active: operation.pointed}" title="point">
<span ng-class="{'fa fa-check-square-o': operation.pointed, 'fa fa-square-o': !operation.pointed}"></span> <span ng-class="{'fa fa-check-square-o': operation.pointed, 'fa fa-square-o': !operation.pointed}"></span>
</button> </button>
@ -91,7 +91,7 @@
<!-- Delete operation, with confirm. --> <!-- Delete operation, with confirm. -->
<button type="button" class="btn btn-default" <button type="button" class="btn btn-default"
ng-if="operation.id && !operation.scheduled_operation_id" ng-if="operation.id && !operation.scheduled_operation_id"
ng-click="operationsCtrl.delete(operation, $index)"> ng-click="operationsCtrl.delete(operation)">
<span class="fa fa-trash-o"></span> <span class="fa fa-trash-o"></span>
</button> </button>
</div> </div>