-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
508 lines (449 loc) · 22.3 KB
/
app.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
import { getParameters, validateToken, userLoggedIn, getMyData, hasUserData, getMyCollections, showAnimation, hideAnimation, storeResponse, isBrowserCompatible, inactivityTime, urls, appState, processAuthWithFirebaseAdmin, successResponse, logDDRumError, translateHTML, translateText, languageAcronyms } from "./js/shared.js";
import { userNavBar, homeNavBar, languageSelector } from "./js/components/navbar.js";
import { homePage, joinNowBtn, whereAmIInDashboard, renderHomeAboutPage, renderHomeExpectationsPage, renderHomePrivacyPage } from "./js/pages/homePage.js";
import { addEventPinAutoUpperCase, addEventRequestPINForm, addEventRetrieveNotifications, toggleCurrentPage, toggleCurrentPageNoUser, addEventToggleSubmit, addEventLanguageSelection } from "./js/event.js";
import { requestPINTemplate, duplicateAccountReminderRender } from "./js/pages/healthCareProvider.js";
import { myToDoList } from "./js/pages/myToDoList.js";
import {renderNotificationsPage} from "./js/pages/notifications.js"
import { renderAgreements } from "./js/pages/agreements.js";
import { renderSettingsPage } from "./js/pages/settings.js";
import { renderSupportPage } from "./js/pages/support.js";
import { renderPaymentPage } from "./js/pages/payment.js";
import { renderSamplesPage } from "./js/pages/samples.js";
import { footerTemplate } from "./js/pages/footer.js";
import { renderVerifiedPage } from "./js/pages/verifiedPage.js";
import { firebaseConfig as devFirebaseConfig } from "./dev/config.js";
import { firebaseConfig as stageFirebaseConfig } from "./stage/config.js";
import { firebaseConfig as prodFirebaseConfig } from "./prod/config.js";
// When doing local development, uncomment this.
// Get the API key file from Box or the DevOps team
// Do not accept PRs with the localDevFirebaseConfig import uncommented
// import { firebaseConfig as localDevFirebaseConfig} from "./local-dev/config.js";
import conceptIdMap from "./js/fieldToConceptIdMapping.js";
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("./serviceWorker.js").catch((error) => {
console.error("Service worker registration failed.", error);
return;
});
navigator.serviceWorker.ready.then(() => {
if (navigator.serviceWorker.controller) {
navigator.serviceWorker.controller.postMessage({ action: "getAppVersion" });
}
});
navigator.serviceWorker.addEventListener("message", (event) => {
if (event.data.action === "sendAppVersion") {
document.getElementById("appVersion").textContent = event.data.payload;
}
});
}
let auth = '';
// DataDog session management -> tie Connect_ID to DataDog sessions
let isDataDogUserSessionSet = false;
const datadogConfig = {
clientToken: 'pubcb2a7770dcbc09aaf1da459c45ecff65',
applicationId: '02ee9ee2-2197-4d6d-aff1-045d46fafa2c',
site: 'ddog-gov.com',
service: 'pwa',
sessionSampleRate: 100,
sessionReplaySampleRate: 20,
trackUserInteractions: true,
trackResources: true,
trackLongTasks: true,
defaultPrivacyLevel: 'mask-user-input'
}
const isLocalDev = location.hostname === 'localhost' || location.hostname === '127.0.0.1';
window.onload = async () => {
const isCompatible = isBrowserCompatible();
if(!isCompatible) {
const mainContent = document.getElementById('root');
mainContent.innerHTML = `<span class="not-compatible">MyConnect is not supported on your browser. Please use Chrome, Edge, Safari or Firefox.</span>`;
return;
}
//Check for language storage
let preferredLanguage = window.localStorage.getItem('preferredLanguage');
if (!preferredLanguage) {
preferredLanguage = conceptIdMap.language.en;
}
document.documentElement.setAttribute('lang', languageAcronyms()[parseInt(preferredLanguage, 10)]);
appState.setState({"language": parseInt(preferredLanguage, 10)});
translateHTML(document.body);
const script = document.createElement('script');
if(location.host === urls.prod) {
script.src = `https://maps.googleapis.com/maps/api/js?key=${prodFirebaseConfig.apiKey}&libraries=places&callback=Function.prototype`
!firebase.apps.length ? firebase.initializeApp(prodFirebaseConfig) : firebase.app();
window.DD_RUM && window.DD_RUM.init({ ...datadogConfig, env: 'prod' });
}
else if(location.host === urls.stage) {
script.src = `https://maps.googleapis.com/maps/api/js?key=${stageFirebaseConfig.apiKey}&libraries=places&callback=Function.prototype`
!firebase.apps.length ? firebase.initializeApp(stageFirebaseConfig) : firebase.app();
window.DD_RUM && window.DD_RUM.init({ ...datadogConfig, env: 'stage' });
}
else if (isLocalDev) {
if (typeof localDevFirebaseConfig === 'undefined') {
console.error('Local development requires a localDevFirebaseConfig function to be defined in ./local-dev/config.js.');
return;
}
!firebase.apps.length ? firebase.initializeApp(localDevFirebaseConfig) : firebase.app();
} else {
script.src = `https://maps.googleapis.com/maps/api/js?key=${devFirebaseConfig.apiKey}&libraries=places&callback=Function.prototype`
!firebase.apps.length ? firebase.initializeApp(devFirebaseConfig) : firebase.app();
!isLocalDev && window.DD_RUM && window.DD_RUM.init({ ...datadogConfig, env: 'dev' });
}
!isLocalDev && window.DD_RUM && window.DD_RUM.startSessionReplayRecording();
document.body.appendChild(script)
auth = firebase.auth();
auth.onAuthStateChanged(async (user) => {
let idToken = '';
if (user) {
idToken = await user.getIdToken();
if (!user.isAnonymous) {
localforage.clear();
inactivityTime();
const firstSignInTime = new Date(user.metadata.creationTime).toISOString();
appState.setState({ participantData: { firstSignInTime } });
}
}
appState.setState({ idToken });
});
const footer = document.getElementById('footer');
footer.innerHTML = footerTemplate();
// googleTranslateElementInit();
router();
}
// TODO: 'google is not defined' datadog error - inspect loading sequence/timing.
// const googleTranslateElementInit = () => {
// if(google) new google.translate.TranslateElement({pageLanguage: 'en'}, 'google_translate_element');
// }
const handleVerifyEmail = (auth, actionCode) => {
auth.applyActionCode(actionCode).then(function(resp) {
window.location.hash = '#verified';
location.reload();
}).catch(function(error) {
console.log(error);
});
}
const handleResetPassword = (auth, actionCode) => {
auth.verifyPasswordResetCode(actionCode).then(function(email) {
document.getElementById('root').innerHTML = `
<h2>Reset password</h2> for <strong>${email}</strong>
<form id="resetPasswordForm" method="POST">
<div class="form-group row">
<label class="col-sm-3 col-form-label">Enter new password</label>
<input type="password" id="resetPassword" pattern="[A-Za-z0-9@_]{6,}" title="Strong passwords have at least 6 characters and a mix of letters and numbers" class="form-control col-sm-4">
<i class="fas fa-eye show-text" id="showPassword" title="Show password"></i>
</div>
</br>
<button type="submit" class="btn btn-primary mb-3">Update password</button>
</form>
`;
const form = document.getElementById('resetPasswordForm');
const show = document.getElementById('showPassword');
show.addEventListener('click', () => {
const element = document.getElementById('resetPassword');
if(element.type === 'password') {
element.type = 'text';
show.classList = ['fas fa-eye-slash show-text'];
show.title = "Hide password";
}
else {
element.type = 'password';
show.classList = ['fas fa-eye show-text'];
show.title = "Show password";
}
});
form.addEventListener('submit', e => {
e.preventDefault();
const newPassword = document.getElementById('resetPassword').value;
if(!newPassword) return;
if(newPassword.trim() === '') return;
// Save the new password.
auth.confirmPasswordReset(actionCode, newPassword).then(function(resp) {
document.getElementById('root').innerHTML = `
Password reset successfully! Please <a href="#sign_in">sign in</a> again to continue.
`;
auth.signInWithEmailAndPassword(accountEmail, newPassword);
}).catch(function(error) {
// Error occurred during confirmation. The code might have expired or the
// password is too weak.
});
})
}).catch(function(error) {
// Invalid or expired action code. Ask user to try to reset the password
// again.
});
}
window.onhashchange = () => {
router();
}
const router = async () => {
const parameters = getParameters(window.location.href);
if(parameters && parameters['mode']){
const mode = parameters['mode'];
const actionCode = parameters['oobCode'];
switch (mode) {
case 'resetPassword':
handleResetPassword(auth, actionCode);
break;
// case 'recoverEmail':
// Display email recovery handler and UI.
// handleRecoverEmail(auth, actionCode, lang);
// break;
case 'verifyEmail':
handleVerifyEmail(auth, actionCode);
break;
default:
// Error: invalid mode.
}
if(['resetPassword', 'verifyEmail'].includes(parameters['mode'])) return;
}
let loggedIn = await userLoggedIn();
const route = window.location.hash || '#';
let exceptions = ['#joining-connect','#after-you-join','#long-term-study-activities','#what-connect-will-do','#how-your-information-will-help-prevent-cancer','#why-connect-is-important','#what-to-expect-if-you-decide-to-join','#where-this-study-takes-place','#about-our-researchers','#a-resource-for-science']
if (loggedIn === false) {
toggleNavBar(route, {}); // If not logged in, pass no data to toggleNavBar
renderLanguageSelector();
if (route === '#') {
homePage();
} else if (route === '#about') {
renderHomeAboutPage();
} else if (route === '#expectations') {
renderHomeExpectationsPage();
} else if(route === '#privacy') {
renderHomePrivacyPage();
} else if(route === '#support'){
location.href = "https://norcfedramp.servicenowservices.com/participant";
} else if (exceptions.includes(route)){
if(!document.getElementById(route.substring(1))){
window.location.hash = '#'
}
} else {
window.location.hash = '#';
}
}
else{
const data = await getMyData();
renderLanguageSelector()
if(successResponse(data)) {
const firebaseAuthUser = firebase.auth().currentUser;
await checkAuthDataConsistency(firebaseAuthUser.email ?? '', firebaseAuthUser.phoneNumber ?? '', data.data[conceptIdMap.firebaseAuthEmail] ?? '', data.data[conceptIdMap.firebaseAuthPhone] ?? '');
// Set Connect_ID for the current DataDog session
if (!isLocalDev && window.DD_RUM && data?.data?.['Connect_ID'] && !isDataDogUserSessionSet) {
window.DD_RUM.setUser({id: data.data['Connect_ID']});
isDataDogUserSessionSet = true;
}
toggleNavBar(route, data); // If logged in, pass data to toggleNavBar
if (route === '#') userProfile();
else if (route === '#dashboard') userProfile();
else if (route === '#messages') renderNotificationsPage();
else if (route === '#sign_out') signOut();
else if (route === '#forms') renderAgreements();
else if (route === '#myprofile') renderSettingsPage();
else if (route === '#support') renderSupportPage();
else if (route === '#samples') renderSamplesPage();
else if (route === '#payment') renderPaymentPage();
else if (route === '#verified') renderVerifiedPage();
else window.location.hash = '#';
}
}
}
const renderLanguageSelector = () => {
let languageSelectorContainer = document.getElementById('languageSelectorContainer');
if (!languageSelectorContainer) {
//Add the language Selector Container
languageSelectorContainer = document.createElement('div');
languageSelectorContainer.id = 'languageSelectorContainer';
let navBarAlt = document.getElementById('navbarNavAltMarkup')
navBarAlt.parentNode.insertBefore(languageSelectorContainer, navBarAlt);
}
languageSelectorContainer.innerHTML = languageSelector();
translateHTML(languageSelectorContainer);
addEventLanguageSelection();
}
const userProfile = () => {
auth.onAuthStateChanged(async user => {
if (user && !user.isAnonymous){
document.title = translateText('shared.dashboardTitle');
const mainContent = document.getElementById('root');
let href = location.href;
const specialParameter = 'continueUrl=';
if(href.includes(specialParameter)) href = href.substr(href.indexOf(specialParameter) + specialParameter.length, href.length);
const parameters = getParameters(href);
showAnimation();
if (parameters?.token) {
const response = await validateToken(parameters.token); // Add uid and sign in flag if token is valid
if (response.code === 200 || response.code === 202) {
const firstSignInTime = new Date(user.metadata.creationTime).toISOString();
if (!firstSignInTime) {
let myErrorData = await getMyData();
logDDRumError(new Error(`Invalid firstSignInTime`), 'InvalidFirstSignInTimeError', {
userAction: 'PWA sign in',
timestamp: new Date().toISOString(),
connectID: myErrorData.data['Connect_ID'],
function: 'addEventRequestPINForm'
});
}
await storeResponse({[conceptIdMap.firstSignInTime]: firstSignInTime});
}
if (response.code === 202) {
let myErrorData = await getMyData();
logDDRumError(new Error(`Duplicate Account Found`), 'duplicateAccountError', {
userAction: 'PWA sign in',
timestamp: new Date().toISOString(),
connectID: myErrorData.data['Connect_ID'],
function: 'addEventRequestPINForm'
});
duplicateAccountReminderRender();
hideAnimation();
return;
}
}
const userData = await getMyData();
window.history.replaceState({},'Dashboard', './#dashboard');
if(user.email && !user.emailVerified && !user.email.startsWith('noreply')) {
const mainContent = document.getElementById('root');
mainContent.innerHTML = `
<br>
<div class="row">
<div class="col-md-2">
</div>
<div class="col-md-8">
<div class="verifyEmailText">Please verify your email by clicking <a id="verifyEmail">
<br>
<br>
<button class="btn btn-primary consentNextButton" style="font-weight:bold;">Verify Email</button></a></div>
</div>
<div class="col-md-2">
</div>
</div>
`
document.getElementById('verifyEmail').addEventListener('click', () => {
mainContent.innerHTML = `
<br>
<div class="row">
<div class="col-md-2">
</div>
<div class="col-md-8">
<div class="verifyEmailText">Please click the link we sent to your email to verify your contact information.<br>Be sure to check your spam folder.</div>
</div>
<div class="col-md-2">
</div>
</div>`
});
hideAnimation();
document.getElementById('verifyEmail').addEventListener('click', () => {
user.sendEmailVerification().then(function() {
}).catch(function(error) {
});
});
return;
}
if (hasUserData(userData)) {
const myData = userData.data;
const myCollections = await getMyCollections();
myToDoList(myData, false, myCollections.data);
}
else {
mainContent.innerHTML = requestPINTemplate();
addEventPinAutoUpperCase();
addEventRequestPINForm();
addEventToggleSubmit();
hideAnimation();
}
}
else{
document.title = translateText('shared.homeTitle');
window.location.hash = '#';
}
});
}
const signOut = () => {
// Record a logout action and stop the DataDog session. This or 15 mins of inactivity will create a new session when the next action is taken.
if (!isLocalDev && window.DD_RUM) {
window.DD_RUM.addAction('user_logout', {
timestamp: new Date().toISOString()
});
window.DD_RUM.stopSession();
isDataDogUserSessionSet = false;
}
firebase.auth().signOut();
window.location.hash = '#';
document.title = translateText('shared.homeTitle');
}
/**
* Render navbar based on user login status
* @param {string} route The route to be rendered
* @param {*} data User data
*/
const toggleNavBar = (route, data) => {
auth.onAuthStateChanged(async user => {
if (user && !user.isAnonymous){
showAnimation();
document.getElementById('navbarNavAltMarkup').innerHTML = userNavBar(data);
document.getElementById('joinNow') ? document.getElementById('joinNow').innerHTML = joinNowBtn(false) : ``;
document.getElementById('signInWrapperDiv') ? document.getElementById('signInWrapperDiv').style.display = "none" :'';
document.getElementById('nextStepWarning') ? document.getElementById('nextStepWarning').innerHTML = await whereAmIInDashboard() : '';
document.getElementById('nextStepWarning') ? document.getElementById('nextStepWarning').style.display="block": '';
addEventRetrieveNotifications();
toggleCurrentPage(route);
hideAnimation();
}
else{
showAnimation();
document.getElementById('navbarNavAltMarkup').innerHTML = homeNavBar();
document.getElementById('joinNow') ? document.getElementById('joinNow').innerHTML = joinNowBtn(true) : ``;
document.getElementById('nextStepWarning') ? document.getElementById('nextStepWarning').style.display="none": '';
toggleCurrentPageNoUser(route);
hideAnimation();
}
});
}
/**
* confirm the user's Firebase Auth email and phone data match the user's Firestore email and phone data.
* There's a 'gotcha' with magic links -the firebase auth profile is stripped of the phone number auth when a magic link is used for email login.
* The 'if' case below handles this. We check the firebase auth && firestore participant profiles for a phone match. If no match, and a phone number exists in the firestore participant profile, we write update the auth phone number to the firebase auth profile.
* The 'else if' case handles the following: We write firebase auth and firestore participant data separately, one after another. There's a chance the first write (Firebase Auth) succeeds and the second write (Firestore) fails.
* This only costs an API call if the data is inconsistent since we hava access to both datapoints from app init. It otherwise only costs the time to run the check.
*/
const checkAuthDataConsistency = async (firebaseAuthEmail, firebaseAuthPhoneNumber, firestoreParticipantEmail, firestoreParticipantPhoneNumber) => {
const isAuthEmailConsistent = firebaseAuthEmail === firestoreParticipantEmail;
const isAuthPhoneConsistent = firebaseAuthPhoneNumber === firestoreParticipantPhoneNumber;
if (firestoreParticipantPhoneNumber && !firebaseAuthPhoneNumber) {
await updateFirebaseAuthPhoneTrigger(firestoreParticipantPhoneNumber);
return false;
} else if (!isAuthEmailConsistent || !isAuthPhoneConsistent) {
const authDataToSync = {
[conceptIdMap.firebaseAuthEmail]: firebaseAuthEmail,
};
if (firebaseAuthPhoneNumber || firestoreParticipantPhoneNumber) {
authDataToSync[conceptIdMap.firebaseAuthPhone] = firebaseAuthPhoneNumber ?? firestoreParticipantPhoneNumber;
}
try {
await storeResponse(authDataToSync);
} catch (error) {
console.error('Error updating document (storeResponse): ', error);
return false;
}
return false;
}
return true;
};
const updateFirebaseAuthPhoneTrigger = async (phone) => {
showAnimation();
const uid = firebase.auth().currentUser.uid;
if (phone && phone.startsWith('+1')) phone = phone.substring(2);
let newAuthData = {};
newAuthData['uid'] = uid;
newAuthData['flag'] = 'replaceSignin';
newAuthData['phone'] = phone;
try {
await processAuthWithFirebaseAdmin(newAuthData);
hideAnimation();
const firebaseAuthUser = firebase.auth().currentUser;
await firebaseAuthUser.reload();
return;
} catch (error) {
console.error('An error occurred:', error);
hideAnimation();
throw error;
}
};