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] Use http error service handlers inside view events #1339

Merged
merged 2 commits into from
Apr 27, 2017
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
25 changes: 13 additions & 12 deletions src/javascripts/ng-admin/Crud/delete/BatchDeleteController.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default class BatchDeleteController {
constructor($scope, $state, $translate, WriteQueries, progression, notification, view) {
constructor($scope, $state, $translate, WriteQueries, progression, notification, view, HttpErrorService) {
this.$scope = $scope;
this.$state = $state;
this.$translate = $translate;
Expand All @@ -15,29 +15,30 @@ export default class BatchDeleteController {
this.actions = view.actions();
this.loadingPage = false;
this.fields = view.fields();
this.HttpErrorService = HttpErrorService;

$scope.$on('$destroy', this.destroy.bind(this));
}

batchDelete() {
batchDelete($event) {
const entityName = this.entity.name();
const { $translate, $state, progression, notification } = this;
const fromState = $state.current.name;
const fromParams = $state.current.params;
const toState = $state.get('list');
const toParams = {
entity: entityName,
...$state.params,
};
progression.start();
return this.WriteQueries.batchDelete(this.view, this.entityIds)
.then(() => $state.go($state.get('list'), angular.extend({ entity: entityName }, $state.params)))
.then(() => $state.go(toState, toParams))
// 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) : {}
})
.catch(angular.identity) // See https://github.com/angular-translate/angular-translate/issues/1516
.then(text => notification.log(text, { addnCls: 'humane-flatty-error' }));
this.HttpErrorService.handleError($event, toState, toParams, fromState, fromParams, error);
});
}

Expand All @@ -57,4 +58,4 @@ export default class BatchDeleteController {
}
}

BatchDeleteController.$inject = ['$scope', '$state', '$translate', 'WriteQueries', 'progression', 'notification', 'view'];
BatchDeleteController.$inject = ['$scope', '$state', '$translate', 'WriteQueries', 'progression', 'notification', 'view', 'HttpErrorService'];
76 changes: 46 additions & 30 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, progression, notification, params, view, entry) {
constructor($scope, $window, $state, $q, $translate, WriteQueries, Configuration, progression, notification, params, view, entry, HttpErrorService) {
this.$scope = $scope;
this.$window = $window;
this.$state = $state;
Expand All @@ -17,6 +17,7 @@ export default class DeleteController {
this.notification = notification;
this.$scope.entry = entry;
this.$scope.view = view;
this.HttpErrorService = HttpErrorService;

$scope.$on('$destroy', this.destroy.bind(this));

Expand All @@ -26,34 +27,49 @@ export default class DeleteController {
});
}

deleteOne() {
const entityName = this.entity.name();
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) {
return this.$state.go(this.$state.get('list'), angular.extend({
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 => {
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) : {}
})
.catch(angular.identity) // See https://github.com/angular-translate/angular-translate/issues/1516
.then(text => notification.log(text, { addnCls: 'humane-flatty-error' }));
});
deleteOne($event) {
return new Promise((resolve, reject) => {
const entityName = this.entity.name();
const { $translate, notification, progression } = this;
progression.start();

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.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' }))
.then(() => {
resolve();
})
.catch(error => {
progression.done();
this.HttpErrorService.handleError($event, toState, toParams, fromState, fromParams, error);
reject();
});
});
});
}

back() {
Expand All @@ -73,4 +89,4 @@ export default class DeleteController {
}
}

DeleteController.$inject = ['$scope', '$window', '$state', '$q', '$translate', 'WriteQueries', 'NgAdminConfiguration', 'progression', 'notification', 'params', 'view', 'entry'];
DeleteController.$inject = ['$scope', '$window', '$state', '$q', '$translate', 'WriteQueries', 'NgAdminConfiguration', 'progression', 'notification', 'params', 'view', 'entry', 'HttpErrorService'];
2 changes: 1 addition & 1 deletion src/javascripts/ng-admin/Crud/delete/batchDelete.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ <h1 compile="::batchDeleteController.title">
<div class="row">
<div class="col-lg-12">
<p translate="ARE_YOU_SURE"></p>
<button class="btn btn-danger" ng-click="batchDeleteController.batchDelete()" translate="YES"></button>
<button class="btn btn-danger" ng-click="batchDeleteController.batchDelete($event)" translate="YES"></button>
<button class="btn btn-default" ng-click="batchDeleteController.back()" translate="NO"></button>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/javascripts/ng-admin/Crud/delete/delete.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ <h1 compile="::deleteController.title">
<div class="row" id="delete-view">
<div class="col-lg-12">
<p translate="ARE_YOU_SURE"></p>
<button class="btn btn-danger" ng-click="deleteController.deleteOne()" translate="YES"></button>
<button class="btn btn-danger" ng-click="deleteController.deleteOne($event)" translate="YES"></button>
<button class="btn btn-default" ng-click="deleteController.back()" translate="NO"></button>
</div>
</div>
152 changes: 83 additions & 69 deletions src/javascripts/ng-admin/Crud/form/FormController.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export default class FormController {
constructor($scope, $state, $injector, $translate, previousState, WriteQueries, Configuration, progression, notification, view, dataStore) {
constructor($scope, $state, $injector, $translate, previousState, WriteQueries, Configuration, progression, notification, view, dataStore, HttpErrorService) {
this.$scope = $scope;
this.$state = $state;
this.$injector = $injector;
Expand All @@ -19,6 +19,7 @@ export default class FormController {
this.$scope.entry = dataStore.getFirstEntry(this.entity.uniqueId);
this.$scope.view = view;
this.$scope.entity = this.entity;
this.HttpErrorService = HttpErrorService;

// in case of entity identifier being modified
this.originEntityId = this.$scope.entry.values[this.entity.identifier().name()];
Expand All @@ -45,47 +46,55 @@ export default class FormController {
submitCreation($event) {
$event.preventDefault();
if (!this.validateEntry()) {
return;
return Promise.reject();
}
const { entity, view, $state, progression, notification, $translate } = this;
var route = entity.showView().enabled ? 'show' : 'list';
var restEntry = this.$scope.entry.transformToRest(view.fields());
const fromState = $state.current.name;
const fromParams = $state.current.params;
const toState = $state.get(route);
var entry = null;
const toParams = () => ({
entity: entity.name(),
id: entry ? entry.identifierValue : null,
});
progression.start();
this.WriteQueries
.createOne(view, restEntry)
.then(rawEntry => {
entry = view.mapEntry(rawEntry);
return entry;
})
.then(entry => view.onSubmitSuccess() && this.$injector.invoke(
view.onSubmitSuccess(),
view,
{ $event, entity, entry, route, controller: this, form: this.form, progression, notification }
))
.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' }))
.catch(error => {
const errorMessage = this.config.getErrorMessageFor(this.view, error) || 'ERROR_MESSAGE';
const customHandlerReturnValue = view.onSubmitError() && this.$injector.invoke(
view.onSubmitError(),

return new Promise((resolve, reject) => {
return this.WriteQueries
.createOne(view, restEntry)
.then(rawEntry => {
entry = view.mapEntry(rawEntry);
return entry;
})
.then(entry => view.onSubmitSuccess() && this.$injector.invoke(
view.onSubmitSuccess(),
view,
{ $event, error, errorMessage, entity, entry, route, controller: this, form: this.form, progression, notification }
);
if (customHandlerReturnValue === false) return;
progression.done();
$translate(errorMessage, {
status: error && error.status,
details: error && error.data && typeof error.data === 'object' ? JSON.stringify(error.data) : {}
{ $event, entity, entry, route, controller: this, form: this.form, progression, notification }
))
.then(customHandlerReturnValue => (customHandlerReturnValue === false) ?
new Promise(innerResolve => innerResolve()) :
$state.go(toState, toParams())
)
.then(() => progression.done())
.then(() => $translate('CREATION_SUCCESS'))
.then(text => notification.log(text, { addnCls: 'humane-flatty-success' }))
.then(() => {
resolve();
})
.catch(angular.identity) // See https://github.com/angular-translate/angular-translate/issues/1516
.then(text => notification.log(text, { addnCls: 'humane-flatty-error' }));
});
.catch(error => {
const customHandlerReturnValue = view.onSubmitError() && this.$injector.invoke(
view.onSubmitError(),
view,
{ $event, error, entity, entry, route, controller: this, form: this.form, progression, notification }
);
if (customHandlerReturnValue === false) return;
progression.done();
this.HttpErrorService.handleError($event, toState, toParams(), fromState, fromParams, error);
reject();
});
});
}

submitEdition($event) {
Expand All @@ -94,43 +103,48 @@ export default class FormController {
return;
}
const { view, $state, previousState, progression, notification, $translate } = this;
var restEntry = this.$scope.entry.transformToRest(view.fields());
var entry = null;
const restEntry = this.$scope.entry.transformToRest(view.fields());
const fromState = $state.current.name;
const fromParams = $state.current.params;
const toState =previousState.name;
let entry = null;
const toParams = previousState.params;
progression.start();
this.WriteQueries
.updateOne(view, restEntry, this.originEntityId)
.then(rawEntry => {
entry = view.mapEntry(rawEntry);
return entry;
})
.then(entry => 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;
$state.go(previousState.name, previousState.params)
.then(() => progression.done())
.then(() => $translate('EDITION_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(
view.onSubmitError(),
return new Promise((resolve, reject) => {
return this.WriteQueries
.updateOne(view, restEntry, this.originEntityId)
.then(rawEntry => {
entry = view.mapEntry(rawEntry);
return entry;
})
.then(entry => view.onSubmitSuccess() && this.$injector.invoke(
view.onSubmitSuccess(),
view,
{ $event, error, errorMessage, entity: this.entity, entry, controller: this, form: this.form, progression, notification }
);
if (customHandlerReturnValue === false) return;
progression.done();
$translate(errorMessage, {
status: error && error.status,
details: error && error.data && typeof error.data === 'object' ? JSON.stringify(error.data) : {}
{ $event, entity: this.entity, entry, controller: this, form: this.form, progression, notification }
))
.then(customHandlerReturnValue => {
if (customHandlerReturnValue === false) return;
$state.go(toState, toParams)
.then(() => progression.done())
.then(() => $translate('EDITION_SUCCESS'))
.then(text => notification.log(text, { addnCls: 'humane-flatty-success' }));
})
.catch(angular.identity) // See https://github.com/angular-translate/angular-translate/issues/1516
.then(text => notification.log(text, { addnCls: 'humane-flatty-error' }));
});
.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();
});
});

}

destroy() {
Expand All @@ -146,4 +160,4 @@ export default class FormController {
}
}

FormController.$inject = ['$scope', '$state', '$injector', '$translate', 'previousState', 'WriteQueries', 'NgAdminConfiguration', 'progression', 'notification', 'view', 'dataStore'];
FormController.$inject = ['$scope', '$state', '$injector', '$translate', 'previousState', 'WriteQueries', 'NgAdminConfiguration', 'progression', 'notification', 'view', 'dataStore', 'HttpErrorService'];
Loading