-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaster.js
315 lines (296 loc) · 14.3 KB
/
master.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
var putData = {
"firstName": "User",
"oldScore": 50,
"updateUser": true
};
var currentUserData = {
"currentScore": 50,
"currentTotalVisits": 0,
"currentEBillVisits": 0
};
var donationAmount=0;
$(document).ready(function() {
$.ajax({
cache: false,
});
$("#scan-qr-button").click(function() {
$("#scanner-div").removeClass("d-none");
$("#home-page-options").addClass("d-none");
$('#reader').html5_qrcode(function(data) {
let userEmail = data;
$.ajax({
url: "https://hackduke2018serverbackup.appspot.com/userDetails",
type: "get",
data: {
userEmail
},
success: function(response) {
console.log(response);
if (response) {
$("#scanner-div").addClass("d-none");
$( "#scan-tab" ).removeClass("active");
$( "#scan-tab" ).addClass("disabled");
$( "#select-tab" ).removeClass("disabled");
$('#select-tab a[href="#select"]').tab('show');
$('#select-tab').click();
putData.userEmail = userEmail;
putData.firstName = response.firstName;
putData.oldScore = response.goGreenScore;
currentUserData = {
"currentScore": response.goGreenScore,
"currentTotalVisits": response.totalVisits,
"currentEBillVisits": response.eBillVisits
}
let displayName = `User: ${response.firstName} ${response.lastName}`;
let goGreenScoreDisplayText = `Go Green Score: ${response.goGreenScore}`;
let totalVisitsDisplayText = `Total Visits till date: ${response.totalVisits}`;
let visitsWithEBill = `Total eco-friendly visits: ${response.eBillVisits}`;
console.log("User information retrieved succesfully");
setTimeout(function () {
$("#loading-div").addClass("d-none");
$("#user-info-div" ).removeClass("d-none");
$('#common-ngo-info').removeClass('d-none');
$("#user-info-header").text(displayName);
$("#user-info-details1").text(goGreenScoreDisplayText);
$("#user-info-details2").text(totalVisitsDisplayText);
$("#user-info-details3").text(visitsWithEBill);
}, 3000);
} else {
alert("Invalid QR Code, please try email option and proceed ahead");
location.reload();
}
},
error: function(xhr) {
console.log("failed to get user information");
}
});
},
function(error) {
// $('#error').html("Scanning...");
},
function(videoError) {
$('#error').html("Camera error.");
}
);
});
$("#enter-email-button").click(function() {
$("#email-div").removeClass("d-none");
$("#home-page-options").addClass("d-none");
});
$("#email-submit").click(function() {
var testEmail = /^[A-Z0-9._%+-]+@([A-Z0-9-]+\.)+[A-Z]{2,4}$/i;
if($('#exampleInputEmail').length && $('#exampleInputEmail').val().length && testEmail.test($('#exampleInputEmail').val())) {
let givenEmail = $("#exampleInputEmail").val();
$("#email-div").addClass("d-none");
$( "#scan-tab" ).removeClass("active");
$( "#scan-tab" ).addClass("disabled");
$( "#select-tab" ).removeClass("disabled");
$('#select-tab a[href="#select"]').tab('show');
$('#select-tab').click();
putData.userEmail = givenEmail;
let URL = "https://hackduke2018serverbackup.appspot.com/userDetails"
console.log(URL);
$.ajax({
url: URL,
type: "get",
data: {
userEmail: givenEmail
},
success: function(response) {
if (response) {
putData.email = response.email;
putData.firstName = response.firstName;
putData.oldScore = response.goGreenScore;
currentUserData = {
"currentScore": response.goGreenScore,
"currentTotalVisits": response.totalVisits,
"currentEBillVisits": response.eBillVisits
}
$('#select-email-div').removeClass('d-none');
$("#email-loading-div").addClass("d-none");
let displayName = `User: ${response.firstName} ${response.lastName}`;
let goGreenScoreDisplayText = `Go Green Score: ${response.goGreenScore}`;
let totalVisitsDisplayText = `Total Visits till date: ${response.totalVisits}`;
let visitsWithEBill = `Total eco-friendly visits: ${response.eBillVisits}`;
console.log("User information retrieved succesfully");
setTimeout(function () {
$("#loading-div").addClass("d-none");
$("#user-info-div" ).removeClass("d-none");
$('#common-ngo-info').removeClass('d-none');
$("#user-info-header").text(displayName);
$("#user-info-details1").text(goGreenScoreDisplayText);
$("#user-info-details2").text(totalVisitsDisplayText);
$("#user-info-details3").text(visitsWithEBill);
}, 3000);
} else {
putData.updateUser = false;
$('#select-scan-div').addClass('d-none');
$('#select-email-div').removeClass('d-none');
setTimeout(function () {
$("#email-loading-div").addClass("d-none");
$('#user-email-entered-info-div').removeClass('d-none');
$('#common-ngo-info').removeClass('d-none');
}, 3000);
}
},
error: function(xhr) {
console.log("failed to get user information");
}
});
}
});
$('#email-receipt-button').click(function() {
let totalVisits = currentUserData.currentTotalVisits + 1;
let totalEBillVisits = currentUserData.currentTotalVisits + 1
let paperBillVisits = totalVisits - totalEBillVisits;
let newScore = 50 + (Math.log(totalVisits) / Math.log(2)) + 3 * (Math.log(totalEBillVisits) / Math.log(2)) - (paperBillVisits/2);
putData.newScore = Math.round(newScore * 100) / 100;
putData.eBill = true;
$('#checkout-info').addClass("d-none");
$('#sending-email-div').removeClass("d-none");
$('#finish-paragraph-text-message').text('Sending you an e-copy of your bill');
$.ajax({
url: 'https://hackduke2018serverbackup.appspot.com/updateUser',
data: putData,
type: 'POST',
success: function(response) {
if (response) {
setTimeout(function () {
$('#sending-email-div').addClass("d-none");
$('#success-modal').removeClass('d-none');
$('#thank-you-message').text('Thank you for shopping with us and for protecting mother nature!');
}, 2000);
setTimeout(function() {
location.reload();
}, 5500);
}
}
});
});
$('#print-receipt-button').click(function() {
let totalVisits = currentUserData.currentTotalVisits + 1;
let totalEBillVisits = currentUserData.currentTotalVisits;
let paperBillVisits = totalVisits - totalEBillVisits;
let newScore = 50 + (Math.log(totalVisits) / Math.log(2)) + 3 * (Math.log(totalEBillVisits) / Math.log(2)) - 3 * (paperBillVisits/2);
putData.newScore = Math.round(newScore * 100) / 100;
putData.eBill = false;
console.log(putData);
$('#checkout-info').addClass("d-none");
$('#printing-bill-div').removeClass("d-none");
$('#finish-paragraph-text-message-printing').text('Printing your receipt, please wait!');
$.ajax({
url: 'https://hackduke2018serverbackup.appspot.com/updateUser',
data: putData,
type: 'POST',
success: function(response) {
if (response) {
setTimeout(function () {
$('#printing-bill-div').addClass("d-none");
$('#success-modal').removeClass('d-none');
$('#thank-you-message').text('Thank you for shopping with us.');
}, 2000);
setTimeout(function() {
location.reload();
}, 5500);
}
}
});
});
$('#ngo-list').change(function(e) {
let selectedNgoCategory = $('#ngo-list option:selected').text();
let ngoApiURL = "https://api.data.charitynavigator.org/v2/Organizations?app_id=11c35b83&app_key=f000f0d35d35033f3a9c7e15ed57f15b&pageSize=4&search=".concat(selectedNgoCategory).concat("&rated=true&minRating=3");
$.ajax({
url: ngoApiURL,
type: 'get',
success: function(response) {
$('#ngo-checkbox-list').removeClass('d-none');
// $('#common-ngo-section').removeClass('d-none');
response.forEach((charity, index) => {
let ngoSelected = `ngo${index}`;
$(`#${ngoSelected}`).text(charity.charityName);
$(`#${ngoSelected}`).attr("href", charity.websiteURL);
})
},
error: function(xhr) {
console.log("failed to get ngo information");
}
});
});
$('#donate-and-proceed').click(function() {
if ($("#ngo-list").val() === null) {
alert("Please select a charity or continue using skip button");
} else {
donationAmount = 80;
$('#user-email-entered-info-div').addClass('d-none');
$('#user-info-div').addClass('d-none');
$('#common-ngo-info').addClass('d-none');
$( "#select-tab" ).removeClass("active");
$('#checkout-info').removeClass("d-none");
$("#select-tab" ).addClass("disabled");
$("#checkout-tab" ).removeClass("disabled");
$('#donation-amount-display').text(`Amount Donated: $0.${donationAmount}`);
if (currentUserData.currentTotalVisits === 0) {
// new user so score will not increase but can decrease
let totalVisits = currentUserData.currentTotalVisits + 1;
let eBillVisitsGoingNotGreen = currentUserData.currentEBillVisits;
let paperBillVisitsGoingNotGreen = totalVisits - eBillVisitsGoingNotGreen;
let newScoreNotGoingGreen = 50 + (Math.log(totalVisits) / Math.log(2)) - 3 * (paperBillVisitsGoingNotGreen/2);
$('#current-score-display').text(`Your Current Go Green score is: ${currentUserData.currentScore}`);
$('#non-green-affects-text').text(`Selecting a printed copy will decrease your Go Green score to: ${newScoreNotGoingGreen}`);
} else {
let totalVisits = currentUserData.currentTotalVisits + 1;
let eBillVisitsGoingGreen = currentUserData.currentEBillVisits + 1
let eBillVisitsGoingNotGreen = currentUserData.currentEBillVisits;
let paperBillVisitsGoingGreen = totalVisits - eBillVisitsGoingGreen;
let paperBillVisitsGoingNotGreen = totalVisits - eBillVisitsGoingNotGreen;
$('#current-score-display').text(`Your Current Go Green score is: ${currentUserData.currentScore}`);
let newScoreGoingGreen = 50 + (Math.log(totalVisits) / Math.log(2)) + 3 * (Math.log(eBillVisitsGoingGreen) / Math.log(2)) - 3 * (paperBillVisitsGoingGreen/2);
let newScoreNotGoingGreen = 50 + (Math.log(totalVisits) / Math.log(2)) + 3 * (Math.log(eBillVisitsGoingNotGreen) / Math.log(2)) - 3 * (paperBillVisitsGoingNotGreen/2);
$('#go-green-benefit-text').text(`Selecting an e-copy will increase your Go Green score to: ${newScoreGoingGreen}`);
$('#non-green-affects-text').text(`Selecting a printed copy will decrease your Go Green score to: ${newScoreNotGoingGreen}`);
}
$('#checkout-tab a[href="#checkout"]').tab('show');
$('#checkout-tab').click();
}
});
$('#skip-and-proceed').click(function() {
$('#user-email-entered-info-div').addClass('d-none');
$('#user-info-div').addClass('d-none');
$('#common-ngo-info').addClass('d-none');
$( "#select-tab" ).removeClass("active");
$('#checkout-info').removeClass("d-none");
$("#select-tab" ).addClass("disabled");
$("#checkout-tab" ).removeClass("disabled");
if (currentUserData.currentTotalVisits === 0) {
// new user so score will not increase but can decrease
let totalVisits = currentUserData.currentTotalVisits + 1;
let eBillVisitsGoingNotGreen = currentUserData.currentEBillVisits;
let paperBillVisitsGoingNotGreen = totalVisits - eBillVisitsGoingNotGreen;
let newScoreNotGoingGreen = 50 + (Math.log(totalVisits) / Math.log(2)) - 3 * (paperBillVisitsGoingNotGreen/2);
$('#current-score-display').text(`Your Current Go Green score is: ${currentUserData.currentScore}`);
$('#non-green-affects-text').text(`Selecting a printed copy will decrease your Go Green score to: ${newScoreNotGoingGreen}`);
} else {
let totalVisits = currentUserData.currentTotalVisits + 1;
let eBillVisitsGoingGreen = currentUserData.currentEBillVisits + 1
let eBillVisitsGoingNotGreen = currentUserData.currentEBillVisits;
let paperBillVisitsGoingGreen = totalVisits - eBillVisitsGoingGreen;
let paperBillVisitsGoingNotGreen = totalVisits - eBillVisitsGoingNotGreen;
$('#current-score-display').text(`Your Current Go Green score is: ${currentUserData.currentScore}`);
let newScoreGoingGreen = 50 + (Math.log(totalVisits) / Math.log(2)) + 3 * (Math.log(eBillVisitsGoingGreen) / Math.log(2)) - 3 * (paperBillVisitsGoingGreen/2);
let newScoreNotGoingGreen = 50 + (Math.log(totalVisits) / Math.log(2)) + 3 * (Math.log(eBillVisitsGoingNotGreen) / Math.log(2)) - 3 * (paperBillVisitsGoingNotGreen/2);
$('#go-green-benefit-text').text(`Selecting an e-copy will increase your Go Green score to: ${newScoreGoingGreen}`);
$('#non-green-affects-text').text(`Selecting a printed copy will decrease your Go Green score to: ${newScoreNotGoingGreen}`);
}
$('#checkout-tab a[href="#checkout"]').tab('show');
$('#checkout-tab').click();
});
});
//
// $.ajax({
// url: 'http://localhost:3000/updateUser',
// data: putData,
// type: 'POST',
// success: function(response) {
// alert('Load was performed.');
// }
// });