diff --git a/src/operations/index.js b/src/operations/index.js index 8db69ba..f49476a 100644 --- a/src/operations/index.js +++ b/src/operations/index.js @@ -54,13 +54,10 @@ var operationModule = angular.module('accountant.operations', [ * Controller for the operations. */ .controller('OperationController', function($rootScope, $scope, $routeParams, - $uibModal, Notification, Operation, $log) { + $uibModal, Notification, Operation, $log, $q) { var vm = this; - // List of operations. - vm.operations = []; - /* * Add an empty operation. */ @@ -70,14 +67,14 @@ var operationModule = angular.module('accountant.operations', [ account_id: $routeParams.accountId }); - vm.operations.splice(0, 0, operation); + return vm.modify(operation); }; /* * Load operations. */ vm.load = function(begin, end) { - vm.operations = Operation.query({ + return Operation.query({ // eslint-disable-next-line camelcase account_id: $routeParams.accountId, begin: begin.format('YYYY-MM-DD'), @@ -91,10 +88,7 @@ var operationModule = angular.module('accountant.operations', [ vm.togglePointed = function(operation, rowform) { 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) { - // Check if $data is already a resource. - var operation; - - if ($data.$save) { - operation = $data; - } else { - operation = vm.operations[$index]; - operation = angular.merge(operation, $data); - } - + vm.save = function(operation) { operation.confirmed = true; - return operation.$save().then(function(data) { - Notification.success('Operation #' + data.id + ' saved.'); + return operation.$save().then(function(operation) { + 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; $uibModal.open({ @@ -143,16 +137,31 @@ var operationModule = angular.module('accountant.operations', [ } } }).result.then(function(operation) { - // FIXME Alexis Lahouze 2017-06-15 Delete operation and reload data - // to update balances. - $log.info('Delete operation', operation); + return operation.$delete().then(function() { + Notification.success('Operation #' + id + ' deleted.'); + + 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 + ':
' + result + ); + + return $q.reject(result); + }); }, 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', resolve: { operation: function() { @@ -160,11 +169,9 @@ var operationModule = angular.module('accountant.operations', [ } } }).result.then(function(operation) { - // FIXME Alexis Lahouze 2017-06-15 Save Operation and reload data to - // update balances. - $log.info('Save operation', operation); + return vm.save(operation); }, 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.load(moment().date(1).year(2000), moment()); + vm.operations = vm.load(moment().date(1).year(2000), moment()); }) .component('operationModifyModalComponent', { diff --git a/src/operations/operations.html b/src/operations/operations.html index 6c2282b..8a68ade 100644 --- a/src/operations/operations.html +++ b/src/operations/operations.html @@ -68,14 +68,14 @@ @@ -91,7 +91,7 @@