working on job polling in angular.

This commit is contained in:
Stefan Midjich 2016-12-20 10:44:53 +01:00
parent 81aaf663c6
commit 4b541fd1cf
2 changed files with 97 additions and 13 deletions

View file

@ -3,30 +3,111 @@
//
//
var debug = true;
var app = angular.module("rsPortalApp", []);
app.config(function($routeProvider) {
app.constant('config', {
debug: true
});
app.config(function($routeProvider, $interpolateProvider) {
$routeProvider
.when('/Swedish', {templateUrl: "swedish.html" })
.when('/English', {templateUrl: "english.html" })
.when('/FAQ-Swedish', {templateUrl: "faq-swe.html" })
.otherwise({redirectTo: '/Swedish'});
$interpolateProvider.startSymbol('[%');
$interpolateProvider.endSymbol('%]');
});
app.controller('RSMainCtrl', function($scope, $http) {
app.controller('RSMainCtrl', function($scope, $http, $q, config) {
$scope.approved = {};
$scope.apiProcessing = false;
$scope.apiErrors = [];
function poll_jobs(job_id) {
var defer = $q.defer();
var api_url = '/job/'+job_id;
// TODO: Restore maxRun before going live.
var maxRun = plugin_timeout/10;
var timesRun = 0;
var do_poll = function () {
$http.get(api_url).success(function(data) {
if (config.debug) {
console.log(data);
}
if (data.is_finished) {
if (config.debug) {
console.log('Resolving job: '+job_id);
}
defer.resolve(job_id, data.result);
}
if (data.is_failed) {
console.log('Job failed: '+job_id);
defer.reject(job_id, data.result);
}
});
// TODO: Add fail callback to log failed GET API requests.
console.log('Runs: '+timesRun+'/'+maxRun);
if (++timesRun == maxRun) {
clearTimeout(timer);
if (config.debug) {
console.log('Polling timed out');
}
defer.reject("Job polling timed out");
return;
} else {
timer = setTimeout(do_poll, 2000);
}
};
var timer = setTimeout(do_poll, 500);
}
$scope.submit = function() {
$scope.apiProcessing = true;
console.log('hej');
var promises = [];
if ($scope.approved.answer == true) {
$http({
method: 'POST',
url: '/approve',
}).then(function success(response) {
poll_jobs(response.data);
if (config.debug) {
console.log('/approve => '+response);
}
for (var job in response.data) {
var job_id = response.data[job].id;
var promise = poll_jobs(job_id);
promises.push(promise);
}
$q.all(promises).then(function (response) {
if (config.debug) {
console.log('Resolved: '+response);
}
$scope.apiProcessing = false;
}, function (reason) {
if (config.debug) {
console.log(reason);
}
$scope.apiProcessing = false;
});
}, function error(response) {
// Failure
if (config.debug) {
console.log(response);
}
$scope.apiErrors.push(response);
$scope.apiProcessing = false;
});
}
};

View file

@ -10,7 +10,7 @@
<meta charset='utf-8' />
<title>Gästportal - Region Skåne</title>
<meta content='Gästportal' name='description' />
<meta content='' name='author' />
<meta content='Cygate AB' name='author' />
<meta content='width=device-width, initial-scale=1.0' name='viewport' />
<meta content="no-index,no-follow" name="robots">
<link rel="icon" type="image/ico" href="/static/rs/images/favicon.ico">
@ -56,13 +56,13 @@
</ul>
<ul id="mobile-nav-list">
<li><a title="Hem" href="/">Hem</a></li>
<li><a title="Hem" href="/#/">Hem</a></li>
<li><a href="/#/FAQ-Swedish">Vanliga Frågor</a></li>
<li><a href="/#/English">English</a></li>
</ul>
<ul id="main-nav-list">
<li><a href="/">Hem</a></li>
<li><a href="/#/">Hem</a></li>
<li><a href="/#/FAQ-Swedish">Vanliga Frågor</a></li>
<li><a href="/#/English">English</a></li>
</ul>
@ -126,14 +126,18 @@
<form ng-submit="submit()" id="approveForm" method="post">
<div class="static-form-block">
<div id="error-box" class="msg-container success-msg hide">
<div id="error-box" class="msg-container ng-hide" ng-show="apiErrors.length > 0">
<p ng-repeat="error in apiErrors">[%error%]</p>
</div>
<div class="input-container">
<label>
<input ng-model="approved.answer" type="checkbox" id="approveCheckbox" required> Jag godkänner avtalet
</label>
<button type="submit" class="button" id="approveButton" value="Godkänn">Godkänn</button>
<button ng-disabled="apiProcessing" type="submit" class="button" id="approveButton">
Godkänn
</button>
<img ng-show="apiProcessing" height="35" width="35" src="/static/rs/images/loading.gif">
</div>
</div>
</form>
@ -169,7 +173,8 @@
<form id="approveForm" method="post">
<div class="static-form-block">
<div id="error-box" class="msg-container success-msg hide">
<div id="error-box" class="msg-container" ng-show="apiErrors.length > 0">
<p ng-repeat="error in apiErrors">[%error%]</p>
</div>
<div class="input-container">
@ -257,8 +262,6 @@
<script src="/static/rs/js/angular.min.js"></script>
<script src="/static/rs/js/rsapp.js"></script>
<script src="/static/js/jquery-1.12.2.min.js"></script>
<script src="/static/js/captiveportal.js"></script>
</body>
</html>