-
Notifications
You must be signed in to change notification settings - Fork 0
/
osdi.js
261 lines (191 loc) · 5.21 KB
/
osdi.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
object osdiConnection
grab form, then pull fields
*/
/* test stuff */
function testFill() {
$("#osdi-email").val("joe@biden.com");
$("#osdi-given-name").val("Joe");
$("#osdi-family-name").val("Biden");
$("#osdi-mobile-phone").val("2125551213");
$("#osdi-address1").val("401 I St. SW");
$("#osdi-locality").val("Washington");
$("#osdi-region").val('DC');
$("#osdi-postal-code").val('20024');
}
/* config stuff */
/* localhost */
var osdiServers = [
{
"name" : "Salsa",
"signup_uri" : "http://localhost:3000/proxy/salsa/person_signup_helper"
},
{
"name" : "VAN",
"signup_uri" : "http://localhost:3000/api/v1/person_signup_helper"
},
{
"name" : "Trilogy",
"signup_uri" : "http://localhost:3000/api/v1/person_signup_helper"
}
];
/* live servers */
var osdiServers = [
{
"name" : "Salsa",
"signup_uri" : "http://salsa.demo.osdi.io/proxy/salsa/person_signup_helper"
},
{
"name" : "VAN",
"signup_uri" : "http://van.demo.osdi.io/proxy/van/person_signup_helper"
},
{
"name" : "Trilogy",
"signup_uri" : "http://dev-forms.trilogyinteractive.com/api/osdi/person_signup_helper"
}
];
var thankYouUri = "/thankyou.html";
/* App stuff */
$( document ).ready(function() {
console.log( "Document Ready!" );
$('#test-fill').click(testFill);
$('#osdi-signup').click(osdiSignup);
$('#donate-button').click(donateButton);
});
/* sequential Ajax hell */
/* UI stuff */
function osdiSignup() {
event.preventDefault(); /* dont actually submit */
$('#myModal').modal('show')
setTimeout(osdiSignupInner,300);
}
function osdiSignupInner() {
var person = processForm();
var count = osdiServers.length
for ( i=0; i< count ; i++) {
console.log("Sending to " + osdiServers[i]['name'])
uploadPerson(person,osdiServers[i]['signup_uri'],osdiServers[i]['name']);
}
}
function processForm() {
if ( ! present($('#osdi-given-name').val())
&& !present($('#osdi-family-name').val())
&& ! present($('#osdi-email').val())
) {
return null;
}
var q={};
q['data'] = {};
var p = q['data'];
p['add_tags'] = []
var tags = p['add_tags'];
p['given_name']=$('#osdi-given-name').val();
p['family_name']=$('#osdi-family-name').val();
p['email_addresses']= [ {
"address" : $('#osdi-email').val(),
"primary" : true
}];
p['phone_numbers'] = [];
p['phone_numbers'][0] = {
'number' : $('#osdi-mobile-phone').val(),
'sms_permission' : $('#sms_permission').val()
}
var a={
"address_lines": [ $('#osdi-address1').val(),$('#osdi-address2').val() ],
"locality": $('#osdi-locality').val(),
"region": $('#osdi-region').val(),
"postal_code": $('#osdi-postal-code').val()
}
p['postal_addresses'] = [ a ];
// // handle checkboxes for tags
// if ( $('#volunteer').prop('checked') ){
// tags.push('volunteer');
// }
$( ".tags" ).each(function() {
if ( $( this ).prop('checked') ) {
tags.push( $(this).val() );
}
});
console.log('Generated person ');
console.log(q);
return q;
}
/* osdi protocol stuff */
function uploadPerson(person, resourceUrl,serverName) {
// serialize to JSON
var json = JSON.stringify(person['data']);
var response;
var response_json;
// busy on
// do POST to server synchronous
// Settings to pass to jquery to fetch the data
osdiLog('Uploading person ' + personEmail(person));
response = exec_ajax(json,resourceUrl);
response_json = response.responseText;
person['save_response'] = response_json;
person['status_code'] = response.status;
person['request_id'] = response.getResponseHeader('OSDI-Request-ID');
if ( response.status > 299) {
osdiLog("Upload Error " + personEmail(person) + " status " + response.status);
osdiLog(getOSDIStatus(response.responseText));
} else {
osdiLog("Upload Success " + serverName + " status " + response.status);
showProgress("Upload Success " + serverName + " status " + response.status);
}
console.log(response);
// busy off
}
function exec_ajax(json,resourceUrl) {
var response;
var ajaxSettings = {
// The url to flickrs api
url: resourceUrl,
// The format we want it in
dataType: 'json',
async: false,
type: "POST",
data: json,
// The function to execute when the api has finished loading
//success: apiSuccessOSDI,
};
// Pass all the settings to being fetching the data
response = $.ajax(ajaxSettings);
console.log(response.status + ' POST ' + resourceUrl );
return response;
}
/* utility stuff */
function osdiLog(msg)
{
console.log(msg);
}
function getOSDIStatus(json) {
var obj;
var str;
try {
obj = JSON.parse(json);
str = obj['osdi:status']['status'] + ' ' + obj['osdi:status']['description'];
} catch (e) {
str = "Undetermined";
}
return str;
}
function present(obj) {
if ( obj != undefined && obj != null && obj != ""
&& obj != "undefined" && obj != "null"){
return true;
} else {
return false;
}
}
function personEmail(person) {
var pe = person['data']['email_addresses'][0]['address'];
return pe;
}
function showProgress(msg) {
var old = $('#progress').html();
var status = old + '<br/>' + msg;
$("#progress").html(status);
}
function donateButton() {
window.location = thankYouUri;
}