blob: 20692c8e69efb215b024c68697462c8578ca996c [file] [log] [blame]
Matteo Scandolof0c32262016-03-11 13:47:27 -08001'use strict';
2
3angular.module('xos.truckroll', [
4 'ngResource',
5 'ngCookies',
Matteo Scandolof0c32262016-03-11 13:47:27 -08006 '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 Scandolof0c32262016-03-11 13:47:27 -080019.directive('truckroll', function(){
20 return {
21 restrict: 'E',
22 scope: {},
23 bindToController: true,
24 controllerAs: 'vm',
25 templateUrl: 'templates/truckroll.tpl.html',
Matteo Scandolob3b03172016-05-16 09:59:38 -070026 controller: function($timeout, $log, Subscribers, Truckroll){
Matteo Scandolof0c32262016-03-11 13:47:27 -080027 Subscribers.query().$promise
28 .then((subscribers) => {
29 this.subscribers = subscribers;
30 });
31
Matteo Scandolob3b03172016-05-16 09:59:38 -070032 $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 Scandolof0c32262016-03-11 13:47:27 -080038 this.loader = false;
39
40 this.runTest = () => {
41
42 // clean previous tests
43 delete this.truckroll.result;
Matteo Scandolo47342ba2016-03-11 14:41:14 -080044 delete this.truckroll.is_synced;
Matteo Scandolo31473172016-03-11 15:01:01 -080045 delete this.truckroll.result_code;
Matteo Scandoloe9bfa802016-03-11 16:22:29 -080046 delete this.truckroll.backend_status;
Matteo Scandolof0c32262016-03-11 13:47:27 -080047
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 Scandolo47342ba2016-03-11 14:41:14 -080059 // if error
Matteo Scandoloc105c1c2016-03-12 09:17:14 -080060 // or
Matteo Scandolo47342ba2016-03-11 14:41:14 -080061 // if is synced
Matteo Scandoloc105c1c2016-03-12 09:17:14 -080062 if(
63 testResult.backend_status.indexOf('2') >= 0 ||
64 (testResult.result_code && testResult.result_code.indexOf('2') >= 0) ||
65 testResult.is_synced
66 ){
Matteo Scandolof0c32262016-03-11 13:47:27 -080067 this.truckroll = angular.copy(testResult);
Matteo Scandolof0c32262016-03-11 13:47:27 -080068 this.loader = false;
Matteo Scandoloc105c1c2016-03-12 09:17:14 -080069 Truckroll.delete({id: id});
Matteo Scandolof0c32262016-03-11 13:47:27 -080070 }
Matteo Scandolo47342ba2016-03-11 14:41:14 -080071 // else keep polling
Matteo Scandolof0c32262016-03-11 13:47:27 -080072 else{
73 $timeout(() => {
74 this.waitForTest(id);
75 }, 2000)
76 }
77 })
78 };
79 }
80 };
81});