blob: 3076406e679c42614d0ad328dc09c4c2a67c78e0 [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandolod7552052016-03-11 13:47:27 -080019'use strict';
20
21angular.module('xos.truckroll', [
22 'ngResource',
23 'ngCookies',
Matteo Scandolod7552052016-03-11 13:47:27 -080024 'ui.router',
25 'xos.helpers'
26])
27.config(($stateProvider) => {
28 $stateProvider
29 .state('user-list', {
30 url: '/',
31 template: '<truckroll></truckroll>'
32 });
33})
34.config(function($httpProvider){
35 $httpProvider.interceptors.push('NoHyperlinks');
36})
Matteo Scandolod7552052016-03-11 13:47:27 -080037.directive('truckroll', function(){
38 return {
39 restrict: 'E',
40 scope: {},
41 bindToController: true,
42 controllerAs: 'vm',
43 templateUrl: 'templates/truckroll.tpl.html',
Matteo Scandolo280dcd32016-05-16 09:59:38 -070044 controller: function($timeout, $log, Subscribers, Truckroll){
Matteo Scandolod7552052016-03-11 13:47:27 -080045 Subscribers.query().$promise
46 .then((subscribers) => {
47 this.subscribers = subscribers;
48 });
49
50 this.loader = false;
51
52 this.runTest = () => {
53
54 // clean previous tests
Matteo Scandolo8c518c62016-08-03 13:31:13 -070055 delete this.truckroll.id;
Matteo Scandolod7552052016-03-11 13:47:27 -080056 delete this.truckroll.result;
Matteo Scandolo30746462016-03-11 14:41:14 -080057 delete this.truckroll.is_synced;
Matteo Scandolocc9930e2016-03-11 15:01:01 -080058 delete this.truckroll.result_code;
Matteo Scandolobc389672016-03-11 16:22:29 -080059 delete this.truckroll.backend_status;
Matteo Scandolod7552052016-03-11 13:47:27 -080060
61 const test = new Truckroll(this.truckroll);
62 this.loader = true;
63 test.$save()
64 .then((res) => {
65 this.waitForTest(res.id);
66 })
67 };
68
69 this.waitForTest = (id) => {
Matteo Scandolo8c518c62016-08-03 13:31:13 -070070
Matteo Scandolod7552052016-03-11 13:47:27 -080071 Truckroll.get({id: id}).$promise
72 .then((testResult) => {
Matteo Scandolo30746462016-03-11 14:41:14 -080073 // if error
Matteo Scandolo6970a812016-03-12 09:17:14 -080074 // or
Matteo Scandolo30746462016-03-11 14:41:14 -080075 // if is synced
Matteo Scandolo6970a812016-03-12 09:17:14 -080076 if(
77 testResult.backend_status.indexOf('2') >= 0 ||
78 (testResult.result_code && testResult.result_code.indexOf('2') >= 0) ||
79 testResult.is_synced
80 ){
Matteo Scandolod7552052016-03-11 13:47:27 -080081 this.truckroll = angular.copy(testResult);
Matteo Scandolod7552052016-03-11 13:47:27 -080082 this.loader = false;
Matteo Scandolo6970a812016-03-12 09:17:14 -080083 Truckroll.delete({id: id});
Matteo Scandolod7552052016-03-11 13:47:27 -080084 }
Matteo Scandolo30746462016-03-11 14:41:14 -080085 // else keep polling
Matteo Scandolod7552052016-03-11 13:47:27 -080086 else{
87 $timeout(() => {
88 this.waitForTest(id);
89 }, 2000)
90 }
91 })
92 };
93 }
94 };
95});