Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 1 | 'use strict'; |
| 2 | |
| 3 | angular.module('xos.truckroll', [ |
| 4 | 'ngResource', |
| 5 | 'ngCookies', |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 6 | 'ui.router', |
| 7 | 'xos.helpers' |
| 8 | ]) |
| 9 | .config(($stateProvider) => { |
| 10 | $stateProvider |
| 11 | .state('user-list', { |
| 12 | url: '/', |
| 13 | template: '<truckroll></truckroll>' |
| 14 | }); |
| 15 | }) |
| 16 | .config(function($httpProvider){ |
| 17 | $httpProvider.interceptors.push('NoHyperlinks'); |
| 18 | }) |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 19 | .directive('truckroll', function(){ |
| 20 | return { |
| 21 | restrict: 'E', |
| 22 | scope: {}, |
| 23 | bindToController: true, |
| 24 | controllerAs: 'vm', |
| 25 | templateUrl: 'templates/truckroll.tpl.html', |
Matteo Scandolo | 280dcd3 | 2016-05-16 09:59:38 -0700 | [diff] [blame] | 26 | controller: function($timeout, $log, Subscribers, Truckroll){ |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 27 | Subscribers.query().$promise |
| 28 | .then((subscribers) => { |
| 29 | this.subscribers = subscribers; |
| 30 | }); |
| 31 | |
Matteo Scandolo | 280dcd3 | 2016-05-16 09:59:38 -0700 | [diff] [blame] | 32 | $log.log('Truckorll Component!'); |
| 33 | $log.info('Truckorll Component!'); |
| 34 | $log.warn('Truckorll Component!'); |
| 35 | $log.error('Truckorll Component!'); |
| 36 | $log.debug('Truckorll Component!'); |
| 37 | |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 38 | this.loader = false; |
| 39 | |
| 40 | this.runTest = () => { |
| 41 | |
| 42 | // clean previous tests |
| 43 | delete this.truckroll.result; |
Matteo Scandolo | 3074646 | 2016-03-11 14:41:14 -0800 | [diff] [blame] | 44 | delete this.truckroll.is_synced; |
Matteo Scandolo | cc9930e | 2016-03-11 15:01:01 -0800 | [diff] [blame] | 45 | delete this.truckroll.result_code; |
Matteo Scandolo | bc38967 | 2016-03-11 16:22:29 -0800 | [diff] [blame] | 46 | delete this.truckroll.backend_status; |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 47 | |
| 48 | const test = new Truckroll(this.truckroll); |
| 49 | this.loader = true; |
| 50 | test.$save() |
| 51 | .then((res) => { |
| 52 | this.waitForTest(res.id); |
| 53 | }) |
| 54 | }; |
| 55 | |
| 56 | this.waitForTest = (id) => { |
| 57 | Truckroll.get({id: id}).$promise |
| 58 | .then((testResult) => { |
Matteo Scandolo | 3074646 | 2016-03-11 14:41:14 -0800 | [diff] [blame] | 59 | // if error |
Matteo Scandolo | 6970a81 | 2016-03-12 09:17:14 -0800 | [diff] [blame] | 60 | // or |
Matteo Scandolo | 3074646 | 2016-03-11 14:41:14 -0800 | [diff] [blame] | 61 | // if is synced |
Matteo Scandolo | 6970a81 | 2016-03-12 09:17:14 -0800 | [diff] [blame] | 62 | if( |
| 63 | testResult.backend_status.indexOf('2') >= 0 || |
| 64 | (testResult.result_code && testResult.result_code.indexOf('2') >= 0) || |
| 65 | testResult.is_synced |
| 66 | ){ |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 67 | this.truckroll = angular.copy(testResult); |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 68 | this.loader = false; |
Matteo Scandolo | 6970a81 | 2016-03-12 09:17:14 -0800 | [diff] [blame] | 69 | Truckroll.delete({id: id}); |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 70 | } |
Matteo Scandolo | 3074646 | 2016-03-11 14:41:14 -0800 | [diff] [blame] | 71 | // else keep polling |
Matteo Scandolo | d755205 | 2016-03-11 13:47:27 -0800 | [diff] [blame] | 72 | else{ |
| 73 | $timeout(() => { |
| 74 | this.waitForTest(id); |
| 75 | }, 2000) |
| 76 | } |
| 77 | }) |
| 78 | }; |
| 79 | } |
| 80 | }; |
| 81 | }); |