diff --git a/src/javascripts/ng-admin/Crud/delete/DeleteController.js b/src/javascripts/ng-admin/Crud/delete/DeleteController.js index 32cd9433..49461c2f 100644 --- a/src/javascripts/ng-admin/Crud/delete/DeleteController.js +++ b/src/javascripts/ng-admin/Crud/delete/DeleteController.js @@ -29,36 +29,40 @@ export default class DeleteController { deleteOne($event) { const entityName = this.entity.name(); - const { $state, $translate, notification, progression } = this; - const fromState = $state.current.name; - const fromParams = $state.current.params; - let toState; - let toParams; - if (fromParams.entity === entityName && - fromParams.id === this.entityId) { - toState = $state.get('list'); - toParams = { - entity: entityName, - ...$state.params, - }; - } - + const { $translate, notification, progression } = this; progression.start(); - return this.WriteQueries.deleteOne(this.view, this.entityId) - .then(() => this.previousStateParametersDeferred.promise) - .then(previousStateParameters => { - // if previous page was related to deleted entity, redirect to list - if(toState){ - return $state.go(toState, toParams); + + return this.previousStateParametersDeferred.promise + .then((previousStateParameters) => { + const fromState = 'delete'; + const fromParams = previousStateParameters; + let toState; + let toParams; + + // if previous page was related to deleted entity, + // redirect to list + if (fromParams.entity === entityName && + fromParams.id === this.entityId) { + toState = this.$state.get('list'); + toParams = { + entity: entityName, + ...this.$state.params, + }; } - return this.back(); - }) - // no need to call progression.done() in case of success, as it's called by the view dislayed afterwards - .then(() => $translate('DELETE_SUCCESS')) - .then(text => notification.log(text, { addnCls: 'humane-flatty-success' })) - .catch(error => { - progression.done(); - this.HttpErrorService.handleError($event, toState, toParams, fromState, fromParams, error); + + return this.WriteQueries.deleteOne(this.view, this.entityId) + .then(() => { + if(toState){ + return this.$state.go(toState, toParams); + } + return this.back(); + }) + .then(() => $translate('DELETE_SUCCESS')) + .then(text => notification.log(text, { addnCls: 'humane-flatty-success' })) + .catch(error => { + progression.done(); + this.HttpErrorService.handleError($event, toState, toParams, fromState, fromParams, error); + }); }); } diff --git a/src/javascripts/test/unit/Crud/delete/DeleteControllerSpec.js b/src/javascripts/test/unit/Crud/delete/DeleteControllerSpec.js index c7e7d382..c03a2e1a 100644 --- a/src/javascripts/test/unit/Crud/delete/DeleteControllerSpec.js +++ b/src/javascripts/test/unit/Crud/delete/DeleteControllerSpec.js @@ -19,7 +19,7 @@ describe('DeleteController', function () { }); var $state = { go: jasmine.createSpy('$state.go'), - get: jasmine.createSpy('$state.get'), + get: jasmine.createSpy('$state.get').and.callFake(state => state), params: {} }; var writeQueries = {