Iftekharul Islam | 99d199f | 2017-03-24 15:28:25 -0500 | [diff] [blame^] | 1 | /** |
| 2 | * api Interceptor |
| 3 | * |
| 4 | * @module app/common/services/apiInterceptor |
| 5 | * @exports apiInterceptor |
| 6 | * @name apiInterceptor |
| 7 | |
| 8 | * @version 0.0.1 |
| 9 | */ |
| 10 | |
| 11 | window.angular && (function (angular) { |
| 12 | 'use strict'; |
| 13 | |
| 14 | angular |
| 15 | .module('app.common.services') |
| 16 | .service('apiInterceptor', ['$q', '$rootScope', 'dataService', function($q, $rootScope, dataService){ |
| 17 | return { |
| 18 | 'request': function(config){ |
| 19 | dataService.server_unreachable = false; |
| 20 | dataService.loading = true; |
| 21 | return config; |
| 22 | }, |
| 23 | 'response': function(response){ |
| 24 | dataService.loading = false; |
| 25 | dataService.last_updated = new Date(); |
| 26 | |
| 27 | if(response == null){ |
| 28 | dataService.server_unreachable = true; |
| 29 | } |
| 30 | |
| 31 | if(response && response.status == 'error' && |
| 32 | dataService.path != '/login'){ |
| 33 | $rootScope.$emit('timedout-user', {}); |
| 34 | } |
| 35 | |
| 36 | return response; |
| 37 | }, |
| 38 | 'responseError': function(rejection){ |
| 39 | dataService.server_unreachable = true; |
| 40 | dataService.loading = false; |
| 41 | return $q.reject(rejection); |
| 42 | } |
| 43 | }; |
| 44 | }]); |
| 45 | |
| 46 | })(window.angular); |