0

I've a jHipster project with the 4.5.1 version (Angular 1) and it does a really nice job. However, I'm already modifying the generated frontend code, which is CRUD focused for all entities, and want to unify many of them.

That said, I would like to be able to choose which entities to alert about using UI-Bootstrap. Now, when I save EntityA which manages EntityB in the same view, I get two alerts for each of the entities. I just want to get the message about the first one.

Is there any way to do it? Or just better, to disable the automatic entity messaging and doing it by hand in the controllers?

Aritz
  • 30,971
  • 16
  • 136
  • 217

1 Answers1

0

The interpectors for the angular http requests are kept in app/blocks/interceptor. There, there's a file called notification.interceptor.js and there's no filter for the entities being displayed, so we need to configure it in some way:

(function() {
    'use strict';

    angular
        .module('myApp')
        .factory('notificationInterceptor', notificationInterceptor);

    notificationInterceptor.$inject = ['$q', 'AlertService'];

    function notificationInterceptor ($q, AlertService) {
        var service = {
            response: response
        };

        return service;

        function response (response) {
            var headers = Object.keys(response.headers()).filter(function (header) {
                return header.indexOf('app-alert', header.length - 'app-alert'.length) !== -1 || header.indexOf('app-params', header.length - 'app-params'.length) !== -1;
            }).sort();
            var alertKey = response.headers(headers[0]);
            if (angular.isString(alertKey) && alertKey.indexOf('myEntityToBeDisplayed') !== -1) {
                AlertService.success(alertKey, { param : response.headers(headers[1])});
            }
            return response;
        }
    }
})();

Then, if we also want to show alerts or log the error responses from the server, the errorhandler.interceptor.js intercepts each of the error responses happening. Tuning it a bit, there's the chance to show alerts for all of them:

(function() {
    'use strict';

    angular
        .module('myApp')
        .factory('errorHandlerInterceptor', errorHandlerInterceptor);

    errorHandlerInterceptor.$inject = ['$q', '$rootScope'];

    function errorHandlerInterceptor ($q, $rootScope) {
        var service = {
            responseError: responseError
        };

        return service;

        function responseError (response) {
            if (!(response.status === 401)) {
                $rootScope.$emit('myApp.httpError', response);
            }
            return $q.reject(response);
        }
    }
})();

See also:

Aritz
  • 30,971
  • 16
  • 136
  • 217