Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/javascripts/ng-admin/Crud/delete/DeleteController.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export default class DeleteController {
constructor($scope, $window, $state, $q, $translate, WriteQueries, Configuration, progression, notification, params, view, entry, HttpErrorService) {
constructor($scope, $window, $state, $injector, $q, $translate, WriteQueries, Configuration, progression, notification, params, view, entry, HttpErrorService) {
this.$scope = $scope;
this.$window = $window;
this.$state = $state;
this.$injector = $injector;
this.$translate = $translate;
this.WriteQueries = WriteQueries;
this.config = Configuration();
Expand Down Expand Up @@ -30,7 +31,7 @@ export default class DeleteController {
deleteOne($event) {
return new Promise((resolve, reject) => {
const entityName = this.entity.name();
const { $translate, notification, progression } = this;
const { $translate, notification, progression, entry, view } = this;
progression.start();

this.previousStateParametersDeferred.promise
Expand All @@ -49,21 +50,35 @@ export default class DeleteController {
entity: entityName,
...this.$state.params,
};

}

return this.WriteQueries.deleteOne(this.view, this.entityId)
.then(() => {
.then(() => view.onSubmitSuccess() && this.$injector.invoke(
view.onSubmitSuccess(),
view,
{ $event, entity: this.entity, entry, controller: this, form: this.form, progression, notification }
))
.then(customHandlerReturnValue => {
if (customHandlerReturnValue === false) return new Promise(innerResolve => innerResolve());
$translate('DELETE_SUCCESS')
.then(text => notification.log(text, { addnCls: 'humane-flatty-success' }))
.then(() => progression.done());
if(toState){
return this.$state.go(toState, toParams);
}
return this.back();
})
.then(() => $translate('DELETE_SUCCESS'))
.then(text => notification.log(text, { addnCls: 'humane-flatty-success' }))
.then(() => {
resolve();
})
.catch(error => {
const customHandlerReturnValue = view.onSubmitError() && this.$injector.invoke(
view.onSubmitError(),
view,
{ $event, error, entity: this.entity, entry, controller: this, form: this.form, progression, notification }
);
if (customHandlerReturnValue === false) return;
progression.done();
this.HttpErrorService.handleError($event, toState, toParams, fromState, fromParams, error);
reject();
Expand All @@ -89,4 +104,4 @@ export default class DeleteController {
}
}

DeleteController.$inject = ['$scope', '$window', '$state', '$q', '$translate', 'WriteQueries', 'NgAdminConfiguration', 'progression', 'notification', 'params', 'view', 'entry', 'HttpErrorService'];
DeleteController.$inject = ['$scope', '$window', '$state', '$injector', '$q', '$translate', 'WriteQueries', 'NgAdminConfiguration', 'progression', 'notification', 'params', 'view', 'entry', 'HttpErrorService'];
29 changes: 19 additions & 10 deletions src/javascripts/test/unit/Crud/delete/DeleteControllerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ describe('DeleteController', function () {
Entity = require('admin-config/lib/Entity/Entity'),
humane = require('humane-js');

var $scope, $window, $q;
beforeEach(inject(function ($controller, $rootScope, _$window_, _$q_) {
var $scope, $window, $q, $injector;
beforeEach(inject(function ($controller, $rootScope, _$window_, _$q_, _$injector_) {
$scope = $rootScope.$new();
$window = _$window_;
$q = _$q_;
$injector = _$injector_;
}));

describe('deleteOne', function() {
Expand Down Expand Up @@ -53,10 +54,12 @@ describe('DeleteController', function () {
title: () => 'Deleting a post',
description: () => 'Remove a post',
actions: () => [],
getEntity: () => entity
getEntity: () => entity,
onSubmitError: () => () => true,
onSubmitSuccess: () => () => true,
};

let deleteController = new DeleteController($scope, $window, $state, $q, $translate, writeQueries, Configuration, progression, notification, {
let deleteController = new DeleteController($scope, $window, $state, $injector, $q, $translate, writeQueries, Configuration, progression, notification, {
id: deletedId,
entity: 'post'
}, view, entry, HttpErrorService);
Expand All @@ -80,10 +83,12 @@ describe('DeleteController', function () {
title: () => 'Deleting a post',
description: () => 'Remove a post',
actions: () => [],
getEntity: () => entity
getEntity: () => entity,
onSubmitError: () => () => true,
onSubmitSuccess: () => () => true,
};

let deleteController = new DeleteController($scope, $window, $state, $q, $translate, writeQueries, Configuration, progression, notification, {
let deleteController = new DeleteController($scope, $window, $state, $injector, $q, $translate, writeQueries, Configuration, progression, notification, {
id: deletedId,
entity: 'post'
}, view, entry, HttpErrorService);
Expand All @@ -110,11 +115,13 @@ describe('DeleteController', function () {
title: () => 'Deleting a comment',
description: () => 'Remove a comment',
actions: () => [],
getEntity: () => new Entity('comment')
getEntity: () => new Entity('comment'),
onSubmitError: () => () => true,
onSubmitSuccess: () => () => true,
};

let $window = { history: { back: jasmine.createSpy('$window.history.back') } };
let deleteController = new DeleteController($scope, $window, $state, $q, $translate, writeQueries, Configuration, progression, notification, {
let deleteController = new DeleteController($scope, $window, $state, $injector, $q, $translate, writeQueries, Configuration, progression, notification, {
id: commentId,
entity: 'comment'
}, view, entry, HttpErrorService);
Expand Down Expand Up @@ -144,10 +151,12 @@ describe('DeleteController', function () {
title: () => 'Deleting a post',
description: () => 'Remove a post',
actions: () => [],
getEntity: () => entity
getEntity: () => entity,
onSubmitError: () => () => true,
onSubmitSuccess: () => () => true,
};

let deleteController = new DeleteController($scope, $window, $state, $q, $translate, writeQueries, Configuration, progression, notification, {
let deleteController = new DeleteController($scope, $window, $state, $injector, $q, $translate, writeQueries, Configuration, progression, notification, {
id: deletedId,
entity: 'post'
}, view, entry, HttpErrorService);
Expand Down