-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathmessage-form.js
executable file
·198 lines (167 loc) · 5.36 KB
/
message-form.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
*
*/
var filter = require('lodash.filter');
var isEmpty = require('lodash.isempty');
var isUndefined = require('lodash.isundefined');
var map = require('lodash.map');
var SubscriptionRequest = require('../../../models').SubscriptionRequest;
var MessageSender = require('../../../models').MessageSender;
var helpers = require('../helpers/message-form');
var MessageFormController = /*@ngInject*/ function($scope, $location, $timeout, dioData, dioAPI) {
// TODO(leah): Nitpicks:
// * wire up the error fields to use better show conditions, so the phone err msg etc doesn't immediately show on typing
// * The "Dear [Rep / Senator] text scrolls over other text entered in that box
$scope.loadingDelay = true;
$scope.submitted = false;
$scope.joinEmailList = false;
$scope.sending = false;
$scope.legislatorList = 'Dear [Representative/Senator],';
$timeout(function() {
$scope.loadingDelay = false;
}, 350);
$scope.goBack = function() {
if (!dioData.hasCanonicalAddress()) {
$location.path('/');
} else {
$location.path('/location');
}
};
/**
*
* @type {{}}
*/
$scope.formData = {
prefix: 'Ms'
};
/**
*
* @type {{}}
*/
$scope.countyData = {};
/**
*
* @type {Array}
*/
$scope.topicOptions = {};
/**
* Fetch LegislatorFormElements models from the server for the supplied bioguideIds.
* @param {Array} bioguideIds
*/
$scope.fetchlegislatorFormElems = function(bioguideIds) {
var cb = function(err, legislatorsFormElements) {
var lfeFound = !isEmpty(legislatorsFormElements);
var serverErr = !isEmpty(err);
if (lfeFound) {
dioData.setLegislatorsFormElements(legislatorsFormElements);
$scope.setLocalData();
} else {
if (serverErr) {
// TODO(sina): Show a server err
} else {
// TODO(all): Figure out what to do here
}
}
};
dioAPI.legislatorFormElemsByBioguideIds(bioguideIds, cb);
};
/**
* Check whether or not any reps require captchas.
* @returns {boolean}
*/
$scope.repsUseCaptchas = function(messageResponses) {
for (var i = 0; i < messageResponses.length; ++i) {
if (messageResponses[i].status === 'captcha_needed') {
return true;
}
}
return false;
};
$scope.send = function() {
// create JSON form submission object
$scope.submitted = true;
var messages = map($scope.legislators, function(legislator) {
return helpers.makeMessage(
legislator,
$scope.formData,
$scope.messageForm.phone.$viewValue,
$scope.topicOptions,
$scope.address
);
});
var cb = function(err, messageResponses) {
var serverErr = !isEmpty(err);
if (!serverErr) {
var hasCaptcha = $scope.repsUseCaptchas(messageResponses);
dioData.setMessageResponses(messageResponses);
if (hasCaptcha) {
$location.path('/captcha');
} else {
$location.path('/thanks');
}
} else {
if (err.code === 429) {
// TODO(sina): show a "too many messages" err
} else if ((err.code !== 400 && err.code !== 500) && !hasCaptcha) {
$location.path('/thanks');
} else {
// TODO(sina): do something here
$scope.sending = false;
}
}
};
$scope.sending = true;
dioAPI.submitMessageToReps(messages, cb);
if ($scope.joinEmailList) {
var messageSender = new MessageSender({
firstName: $scope.formData.firstName,
lastName: $scope.formData.lastName,
email: $scope.formData.email
});
var subRequest = new SubscriptionRequest({
canonicalAddress: $scope.address,
sender: messageSender
});
dioAPI.subscribeToEFFList(subRequest, function() {
// no-op as EFF subscription is best-effort only
});
}
};
/**
* Set local scope values for values fetched from the session store.
*/
$scope.setLocalData = function() {
$scope.legislators = dioData.getSelectedLegislators();
var legFormElements = dioData.getLegislatorsFormElements();
var bioguideIdsBySelection = dioData.getBioguideIdsBySelection();
$scope.legislatorsFormElements = filter(legFormElements, function(lfe) {
return bioguideIdsBySelection[lfe.bioguideId];
});
$scope.address = dioData.getCanonicalAddress();
var formFieldData = helpers.createFormFields(
$scope.legislatorsFormElements, $scope.legislators, $scope.address);
$scope.countyData = formFieldData.countyData;
$scope.topicOptions = formFieldData.topicOptions;
$scope.formData = formFieldData.formData;
$scope.legislatorList = map($scope.legislators, function(legislator){
return ' ' + legislator.title + '. ' + legislator.firstName + ' ' + legislator.lastName;
}).join();
};
/**
* Fetch data from the session store.
*/
$scope.fetchDataFromStore = function() {
var validData = dioData.hasBioguideIdsBySelection() && dioData.hasCanonicalAddress();
var selectedBioguideIds = dioData.getSelectedBioguideIds();
if (!validData || isEmpty(selectedBioguideIds)) {
$scope.goBack();
}
if (!dioData.hasLegislatorsFormElements()) {
$scope.fetchlegislatorFormElems(selectedBioguideIds);
} else {
$scope.setLocalData();
}
};
$scope.fetchDataFromStore();
};
module.exports = MessageFormController;