-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathcaptcha.js
90 lines (72 loc) · 2 KB
/
captcha.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
*
*/
var filter = require('lodash.filter');
var isEmpty = require('lodash.isempty');
var map = require('lodash.map');
var CaptchaSolution = require('../../../models').CaptchaSolution;
var CaptchaController = /*@ngInject*/ function($scope, $location, $timeout, dioData, dioAPI) {
/**
*
* @type {number}
*/
$scope.captchasRemaining = 0;
/**
*
* @type {Array}
*/
$scope.captchasReceived = [];
$scope.goBack = function() {
if (!dioData.hasCanonicalAddress()) {
$location.path('/');
} else {
$location.path('/location');
}
};
$scope.submit = function(captcha) {
captcha.waitingForResponse = true;
var cb = function(err, response) {
var serverErr = !isEmpty(err);
if (!serverErr) {
var status = response.status;
if (status === 'success') {
captcha.success = true;
$scope.captchasRemaining -= 1;
} else {
// TODO(sina): Show a "solution" failed, plz retry message by the captcha
}
if ($scope.captchasRemaining === 0) {
$location.path('/thanks');
}
} else {
// TODO(sina): Throw a try again later err
}
};
dioAPI.submitCaptchaResponse(new CaptchaSolution(captcha), cb);
};
/**
* Fetch data from the session store.
*/
$scope.fetchDataFromStore = function() {
var messageResponses = dioData.getMessageResponses();
if (isEmpty(messageResponses)) {
$scope.goBack();
}
var captchasReceived = map(messageResponses, function(messageResponse) {
$scope.captchasRemaining += 1;
return {
link: messageResponse.url,
uid: messageResponse.uid,
answer: '',
success: false,
waitingForResponse: false,
bioguideId: messageResponse.bioguideId
};
});
$scope.captchasReceived = filter(captchasReceived, function(captcha) {
return !angular.isUndefined(captcha.link);
});
};
$scope.fetchDataFromStore();
};
module.exports = CaptchaController;