diff --git a/spring-petclinic-api-gateway/src/main/resources/static/index.html b/spring-petclinic-api-gateway/src/main/resources/static/index.html index 221feadb6..c947c3e3e 100644 --- a/spring-petclinic-api-gateway/src/main/resources/static/index.html +++ b/spring-petclinic-api-gateway/src/main/resources/static/index.html @@ -46,6 +46,9 @@ + + + diff --git a/spring-petclinic-api-gateway/src/main/resources/static/scripts/app.js b/spring-petclinic-api-gateway/src/main/resources/static/scripts/app.js index 870aeb209..66db62a27 100644 --- a/spring-petclinic-api-gateway/src/main/resources/static/scripts/app.js +++ b/spring-petclinic-api-gateway/src/main/resources/static/scripts/app.js @@ -1,7 +1,7 @@ 'use strict'; /* App Module */ var petClinicApp = angular.module('petClinicApp', [ - 'ui.router', 'layoutNav', 'layoutFooter', 'layoutWelcome', + 'ui.router', 'infrastructure', 'layoutNav', 'layoutFooter', 'layoutWelcome', 'ownerList', 'ownerDetails', 'ownerForm', 'petForm', 'visits', 'vetList']); petClinicApp.config(['$stateProvider', '$urlRouterProvider', '$locationProvider', '$httpProvider', function( @@ -9,6 +9,7 @@ petClinicApp.config(['$stateProvider', '$urlRouterProvider', '$locationProvider' // safari turns to be lazy sending the Cache-Control header $httpProvider.defaults.headers.common["Cache-Control"] = 'no-cache'; + $httpProvider.interceptors.push('HttpErrorHandlingInterceptor'); $locationProvider.hashPrefix('!'); @@ -32,4 +33,4 @@ petClinicApp.config(['$stateProvider', '$urlRouterProvider', '$locationProvider' angular.module(mod).component(mod, { templateUrl: "scripts/fragments/" + c + ".html" }); -}); \ No newline at end of file +}); diff --git a/spring-petclinic-api-gateway/src/main/resources/static/scripts/infrastructure/httpErrorHandlingInterceptor.js b/spring-petclinic-api-gateway/src/main/resources/static/scripts/infrastructure/httpErrorHandlingInterceptor.js new file mode 100644 index 000000000..f65ab687d --- /dev/null +++ b/spring-petclinic-api-gateway/src/main/resources/static/scripts/infrastructure/httpErrorHandlingInterceptor.js @@ -0,0 +1,17 @@ +'use strict'; + +/** + * Global HTTP errors handler. + */ +angular.module('infrastructure') + .factory('HttpErrorHandlingInterceptor', function () { + return { + responseError: function (response) { + var error = response.data; + alert(error.error + "\r\n" + error.errors.map(function (e) { + return e.field + ": " + e.defaultMessage; + }).join("\r\n")); + return response; + } + } + }); diff --git a/spring-petclinic-api-gateway/src/main/resources/static/scripts/infrastructure/infrastructure.js b/spring-petclinic-api-gateway/src/main/resources/static/scripts/infrastructure/infrastructure.js new file mode 100644 index 000000000..58dd767c4 --- /dev/null +++ b/spring-petclinic-api-gateway/src/main/resources/static/scripts/infrastructure/infrastructure.js @@ -0,0 +1,3 @@ +'use strict'; + +angular.module('infrastructure', []); diff --git a/spring-petclinic-api-gateway/src/main/resources/static/scripts/owner-form/owner-form.controller.js b/spring-petclinic-api-gateway/src/main/resources/static/scripts/owner-form/owner-form.controller.js index f3e0e057c..d25f7a569 100644 --- a/spring-petclinic-api-gateway/src/main/resources/static/scripts/owner-form/owner-form.controller.js +++ b/spring-petclinic-api-gateway/src/main/resources/static/scripts/owner-form/owner-form.controller.js @@ -16,20 +16,15 @@ angular.module('ownerForm') self.submitOwnerForm = function () { var id = self.owner.id; - var req; + if (id) { - req = $http.put("api/customer/owners/" + id, self.owner); + $http.put('api/customer/owners/' + id, self.owner).then(function () { + $state.go('ownerDetails', {ownerId: ownerId}); + }); } else { - req = $http.post("api/customer/owners", self.owner); + $http.post('api/customer/owners', self.owner).then(function () { + $state.go('owners'); + }); } - - req.then(function () { - $state.go('owners'); - }, function (response) { - var error = response.data; - alert(error.error + "\r\n" + error.errors.map(function (e) { - return e.field + ": " + e.defaultMessage; - }).join("\r\n")); - }); }; }]); diff --git a/spring-petclinic-api-gateway/src/main/resources/static/scripts/pet-form/pet-form.controller.js b/spring-petclinic-api-gateway/src/main/resources/static/scripts/pet-form/pet-form.controller.js index 9bf24f190..93bbb6c8b 100644 --- a/spring-petclinic-api-gateway/src/main/resources/static/scripts/pet-form/pet-form.controller.js +++ b/spring-petclinic-api-gateway/src/main/resources/static/scripts/pet-form/pet-form.controller.js @@ -46,13 +46,7 @@ angular.module('petForm') } req.then(function () { - $state.go("owners", {ownerId: ownerId}); - }, function (response) { - var error = response.data; - error.errors = error.errors || []; - alert(error.error + "\r\n" + error.errors.map(function (e) { - return e.field + ": " + e.defaultMessage; - }).join("\r\n")); + $state.go('ownerDetails', {ownerId: ownerId}); }); }; }]); diff --git a/spring-petclinic-api-gateway/src/main/resources/static/scripts/visits/visits.controller.js b/spring-petclinic-api-gateway/src/main/resources/static/scripts/visits/visits.controller.js index 992765831..d68798b39 100644 --- a/spring-petclinic-api-gateway/src/main/resources/static/scripts/visits/visits.controller.js +++ b/spring-petclinic-api-gateway/src/main/resources/static/scripts/visits/visits.controller.js @@ -19,12 +19,7 @@ angular.module('visits') }; $http.post(url, data).then(function () { - $state.go("owners", { ownerId: $stateParams.ownerId }); - }, function (response) { - var error = response.data; - alert(error.error + "\r\n" + error.errors.map(function (e) { - return e.field + ": " + e.defaultMessage; - }).join("\r\n")); + $state.go('ownerDetails', { ownerId: $stateParams.ownerId }); }); }; }]);