Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFR] Display progress bar when deleting #1140

Merged
merged 4 commits into from
Jun 29, 2016
Merged
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
24 changes: 13 additions & 11 deletions src/javascripts/ng-admin/Crud/delete/BatchDeleteController.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
export default class BatchDeleteController {
constructor($scope, $state, $translate, WriteQueries, notification, view) {
constructor($scope, $state, $translate, WriteQueries, progression, notification, view) {
this.$scope = $scope;
this.$state = $state;
this.$translate = $translate;
this.WriteQueries = WriteQueries;
this.progression = progression;
this.notification = notification;
this.view = view;
this.entity = view.getEntity();
Expand All @@ -20,17 +21,16 @@ export default class BatchDeleteController {

batchDelete() {
const entityName = this.entity.name();
const { $translate, $state, notification } = this;

this.WriteQueries.batchDelete(this.view, this.entityIds)
.then(() => {
$state.go($state.get('list'), angular.extend({
entity: entityName
}, $state.params));
$translate('BATCH_DELETE_SUCCESS').then(text => notification.log(text, { addnCls: 'humane-flatty-success' }));
})
const { $translate, $state, progression, notification } = this;
progression.start();
return this.WriteQueries.batchDelete(this.view, this.entityIds)
.then(() => $state.go($state.get('list'), angular.extend({ entity: entityName }, $state.params)))
// no need to call progression.done() in case of success, as it's called by the view dislayed afterwards
.then(() => $translate('BATCH_DELETE_SUCCESS'))
.then(text => notification.log(text, { addnCls: 'humane-flatty-success' }))
.catch(error => {
const errorMessage = this.config.getErrorMessageFor(this.view, error) | 'ERROR_MESSAGE';
progression.done();
$translate(errorMessage, {
status: error && error.status,
details: error && error.data && typeof error.data === 'object' ? JSON.stringify(error.data) : {}
Expand All @@ -49,7 +49,9 @@ export default class BatchDeleteController {
this.$state = undefined;
this.$translate = undefined;
this.WriteQueries = undefined;
this.progression = undefined;
this.notification = undefined;
}
}

BatchDeleteController.$inject = ['$scope', '$state', '$translate', 'WriteQueries', 'notification', 'view'];
BatchDeleteController.$inject = ['$scope', '$state', '$translate', 'WriteQueries', 'progression', 'notification', 'view'];
21 changes: 13 additions & 8 deletions src/javascripts/ng-admin/Crud/delete/DeleteController.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default class DeleteController {
constructor($scope, $window, $state, $q, $translate, WriteQueries, Configuration, notification, params, view, entry) {
constructor($scope, $window, $state, $q, $translate, WriteQueries, Configuration, progression, notification, params, view, entry) {
this.$scope = $scope;
this.$window = $window;
this.$state = $state;
Expand All @@ -13,6 +13,7 @@ export default class DeleteController {
this.description = view.description();
this.actions = view.actions();
this.entity = view.getEntity();
this.progression = progression;
this.notification = notification;
this.$scope.entry = entry;
this.$scope.view = view;
Expand All @@ -27,23 +28,25 @@ export default class DeleteController {

deleteOne() {
const entityName = this.entity.name();
const { $translate, notification } = this;

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 (previousStateParameters.entity === entityName && previousStateParameters.id === this.entityId) {
this.$state.go(this.$state.get('list'), angular.extend({
return this.$state.go(this.$state.get('list'), angular.extend({
entity: entityName
}, this.$state.params));
} else {
this.back();
}
$translate('DELETE_SUCCESS').then(text => notification.log(text, { addnCls: 'humane-flatty-success' }));
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 => {
const errorMessage = this.config.getErrorMessageFor(this.view, error) | 'ERROR_MESSAGE';
progression.done();
$translate(errorMessage, {
status: error && error.status,
details: error && error.data && typeof error.data === 'object' ? JSON.stringify(error.data) : {}
Expand All @@ -63,7 +66,9 @@ export default class DeleteController {
this.WriteQueries = undefined;
this.view = undefined;
this.entity = undefined;
this.progression = undefined;
this.notification = undefined;
}
}

DeleteController.$inject = ['$scope', '$window', '$state', '$q', '$translate', 'WriteQueries', 'NgAdminConfiguration', 'notification', 'params', 'view', 'entry'];
DeleteController.$inject = ['$scope', '$window', '$state', '$q', '$translate', 'WriteQueries', 'NgAdminConfiguration', 'progression', 'notification', 'params', 'view', 'entry'];
12 changes: 6 additions & 6 deletions src/javascripts/ng-admin/Crud/form/FormController.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ export default class FormController {
view,
{ $event, entity, entry, route, controller: this, form: this.form, progression, notification }
))
.then(customHandlerReturnValue => {
if (customHandlerReturnValue === false) return;
.then(customHandlerReturnValue => (customHandlerReturnValue === false) ?
new Promise(resolve => resolve()) :
$state.go(this.$state.get(route), { entity: entity.name(), id: entry.identifierValue })
.then(() => progression.done())
.then(() => $translate('CREATION_SUCCESS'))
.then(text => notification.log(text, { addnCls: 'humane-flatty-success' }));
})
)
.then(() => progression.done())
.then(() => $translate('CREATION_SUCCESS'))
.then(text => notification.log(text, { addnCls: 'humane-flatty-success' }))
.catch(error => {
const errorMessage = this.config.getErrorMessageFor(this.view, error) | 'ERROR_MESSAGE';
const customHandlerReturnValue = view.onSubmitError() && this.$injector.invoke(
Expand Down
12 changes: 8 additions & 4 deletions src/javascripts/test/unit/Crud/delete/DeleteControllerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('DeleteController', function () {
}));

describe('deleteOne', function() {
var $translate = text => ({ then: () => text });
var $translate = text => text;
var Configuration = () => ({
getErrorMessageFor: () => '',
});
Expand All @@ -25,6 +25,10 @@ describe('DeleteController', function () {
var writeQueries = {
deleteOne: jasmine.createSpy('writeQueries.deleteOne').and.callFake(() => $q.when())
};
var progression = {
start: () => true,
done: () => true,
};
var notification = humane;
var params = {
id: 3,
Expand All @@ -49,7 +53,7 @@ describe('DeleteController', function () {
getEntity: () => entity
};

let deleteController = new DeleteController($scope, $window, $state, $q, $translate, writeQueries, Configuration, notification, {
let deleteController = new DeleteController($scope, $window, $state, $q, $translate, writeQueries, Configuration, progression, notification, {
id: deletedId,
entity: 'post'
}, view, entry);
Expand All @@ -76,7 +80,7 @@ describe('DeleteController', function () {
getEntity: () => entity
};

let deleteController = new DeleteController($scope, $window, $state, $q, $translate, writeQueries, Configuration, notification, {
let deleteController = new DeleteController($scope, $window, $state, $q, $translate, writeQueries, Configuration, progression, notification, {
id: deletedId,
entity: 'post'
}, view, entry);
Expand Down Expand Up @@ -107,7 +111,7 @@ describe('DeleteController', function () {
};

let $window = { history: { back: jasmine.createSpy('$window.history.back') } };
let deleteController = new DeleteController($scope, $window, $state, $q, $translate, writeQueries, Configuration, notification, {
let deleteController = new DeleteController($scope, $window, $state, $q, $translate, writeQueries, Configuration, progression, notification, {
id: commentId,
entity: 'comment'
}, view, entry);
Expand Down