-
Notifications
You must be signed in to change notification settings - Fork 1
/
favorites.js
560 lines (432 loc) · 17.3 KB
/
favorites.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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
import { GoogleAuthProvider, OAuthProvider, updatePassword, signInWithCredential } from "firebase/auth";
const {auth} = require("./src/firebase/accounts.js")
import { Initialize_UI, Display_Templates, injectDefaultStyles, createFirebaseBindings } from "ui-for-firebase-authentication"
injectDefaultStyles()
let googleProvider = new GoogleAuthProvider()
let appleProvider = new OAuthProvider("apple.com")
//Add email scope.
googleProvider.addScope("https://www.googleapis.com/auth/userinfo.email")
appleProvider.addScope("email")
let providers = [
{
provider: googleProvider,
display: Display_Templates.Google_Light
},
{
provider: appleProvider,
display: Display_Templates.Apple_Dark
},
{
provider: "email",
display: Display_Templates.Email_Dark
},
]
let signInOptionsContainer = document.getElementById("signInOptionsContainer")
let authenticationBindings = createFirebaseBindings(auth)
if (window.isNative) {
//We will sign in on the web API, but will get credentials using the native flow when on native.
authenticationBindings.signInWithProvider = function(provider) {
let str = ""
if (provider === googleProvider) {
str = "google"
}
else if (provider === appleProvider) {
str = "apple"
}
return window.signInWithProvider(str, {skipNativeAuth: true}).then((signInResult) => {
let cred;
if (provider instanceof GoogleAuthProvider) {
cred = provider.constructor.credential(signInResult.credential.idToken)
}
else {
cred = provider.credential({
idToken: signInResult.credential.idToken,
rawNonce: signInResult.credential.nonce
})
}
return signInWithCredential(auth, cred)
})
}
}
function ui_reinit() {
Initialize_UI(authenticationBindings, providers, signInOptionsContainer)
}
ui_reinit()
auth.onAuthStateChanged(function(newAuth) {
console.log(newAuth)
})
const accounts = require("./src/firebase/accounts.js")
let signedInStatusText = document.getElementById("signedInStatusText")
let signOutButton = document.getElementById("signOutButton")
let manageAccountButton = document.getElementById("manageAccountButton")
const {loadFavorites, getFavoritesLastModified, writeFavorites, mergeFavoritesObjects} = require("./src/addToFavorites.js")
const getSearchLink = require("./src/getSearchLink.js")
let notificationsState = document.getElementById("notificationsState")
function updateSignInStatus() {
let user = accounts.getCurrentUser()
console.warn(user)
let text;
if (user) {
//Remove the sign in options
while (signInOptionsContainer.lastChild) {signInOptionsContainer.lastChild.remove()}
//Show account management.
manageAccountButton.style.display = signOutButton.style.display = ""
text = `Signed in as ${accounts.getCurrentUserDetails().email}. `
}
else {
//Display the sign in options
ui_reinit()
//Hide account management
manageAccountButton.style.display = signOutButton.style.display = "none"
text = `Sign in to Sync Favorites and Receive Notifications! `
notificationsState.style.display = "none"
}
signedInStatusText.innerHTML = text
}
auth.onAuthStateChanged(updateSignInStatus)
signOutButton.addEventListener("click", async function() {
await auth.signOut()
if (window.isNative) {
//We need to sign out of the native app as well - otherwise we may just be automatically signed back in.
window.signOut()
}
})
let deleteAccountButton = document.createElement("button")
deleteAccountButton.innerHTML = "Delete Account"
deleteAccountButton.addEventListener("click", async function() {
if (confirm("Delete your account?")) {
await auth.currentUser.delete()
}
})
let passwordEntryField = document.createElement("input")
passwordEntryField.placeholder = "Enter New Password..."
passwordEntryField.type = "password"
let setPassword = document.createElement("button")
let togglePasswordVisibility = document.createElement("button")
togglePasswordVisibility.innerHTML = "Show"
let manageAccountWindow = document.createElement("div")
manageAccountWindow.style.textAlign = "center"
manageAccountWindow.innerHTML = "<h2>Manage Account</h2><p>Passwords are optional if you sign in with Google, Apple, or another service. Setting a password allows you to log in with your email and password as well. </p>"
manageAccountWindow.appendChild(passwordEntryField)
manageAccountWindow.appendChild(togglePasswordVisibility)
manageAccountWindow.appendChild(document.createElement("br"))
manageAccountWindow.appendChild(setPassword)
let deleteAccountWarning = document.createElement("p")
deleteAccountWarning.innerHTML = "Deleting your account will prevent syncing of your favorites data between devices. You can always create a new account later. "
manageAccountWindow.appendChild(deleteAccountWarning)
manageAccountWindow.appendChild(deleteAccountButton)
manageAccountButton.addEventListener("click", function() {
createModal(manageAccountWindow)
})
setPassword.addEventListener("click", async function() {
if (passwordEntryField.value.length === 0) {return} //Firebase accepts a blank string as a password - though it then throws internal auth errors, so it may be equivalent to disabling passwords.
await updatePassword(auth.currentUser, passwordEntryField.value)
})
togglePasswordVisibility.addEventListener("click", function() {
if (passwordEntryField.type === "password") {
togglePasswordVisibility.innerHTML = "Hide"
passwordEntryField.type = "text"
}
else {
togglePasswordVisibility.innerHTML = "Show"
passwordEntryField.type = "password"
}
})
function setPasswordInfo() {
let hasPassword = accounts.getCurrentUser()?.providerData?.some((provider) => {return provider.providerId === "password"})
if (hasPassword) {
setPassword.innerHTML = "Update Password"
}
else {
setPassword.innerHTML = "Set Password"
}
}
setPasswordInfo()
auth.onAuthStateChanged(setPasswordInfo)
//accounts.setData merges unless false is passed as a second argument.
//Therefore, we can indescriminately setData with our new favorites - it won't destroy anything already up there.
//But when things are updated individually on the client, we either need to overwrite or figure out how to delete specific map keys.
let notificationStatus = document.getElementById("notificationStatus")
let enableButton = document.getElementById("enable")
let disableButton = document.getElementById("disable")
let timeInput = document.getElementById("timeOfDay")
let disabledDate = document.getElementById("disabledDate")
let timeInputLabel = document.querySelector("label[for='timeOfDay']")
let disabledDateLabel = document.querySelector("label[for='disabledDate']")
enableButton.addEventListener("click", async function() {
await accounts.setNotificationsConfig({
enabled: true,
noneUntil: 0,
})
updateNotificationsUI() //TODO: We don't actually need to issue a read here - we know the config, and what we changed.
})
disableButton.addEventListener("click", async function() {
await accounts.setNotificationsConfig({
enabled: false
})
updateNotificationsUI() //TODO: We don't actually need to issue a read here - we know the config, and what we changed.
})
timeInput.addEventListener("change", async function() {
if (timeInput.value === "") {
return
}
let [hours, minutes] = timeInput.value.split(":")
let simDate = new Date().setHours(hours, minutes, 0, 0)
simDate = new Date(simDate)
let utcHours = String(simDate.getUTCHours()).padStart(2, 0)
let utcMinutes = String(simDate.getUTCMinutes()).padStart(2, 0)
await accounts.setNotificationsConfig({
timeOfDay: utcHours + ":" + utcMinutes
})
})
disabledDate.addEventListener("change", async function() {
let noneUntil;
if (disabledDate.value === "") {
noneUntil = 0
}
else {
noneUntil = new Date(timeInput.value + " " + disabledDate.value).getTime()
}
await accounts.setNotificationsConfig({noneUntil})
updateNotificationsUI()
})
function getDisabledUntilPhrase(DateObj) {
if (!(DateObj instanceof Date)) {DateObj = new Date(DateObj)} //Allow time strings or milliseconds since epoch.
return "Email Notifications disabled until " + DateObj.toLocaleTimeString() + " on " + DateObj.toLocaleDateString()
}
async function updateNotificationsUI(data) {
let notifications = await accounts.getNotificationsConfig(data)
notificationsState.style.display = ""
if (notifications.enabled) {
enableButton.style.display = "none"
disableButton.style.display = ""
timeInputLabel.style.display = timeInput.style.display = ""
disabledDateLabel.style.display = disabledDate.style.display = ""
}
else {
notificationStatus.innerHTML = "Email Notifications Disabled"
enableButton.style.display = ""
disableButton.style.display = "none"
timeInputLabel.style.display = timeInput.style.display = "none"
disabledDateLabel.style.display = disabledDate.style.display = "none"
return
}
let timeZoneOffset = new Date().getTimezoneOffset();
let todaysDate = new Date(Date.now() - timeZoneOffset).toJSON().split("T")[0];
disabledDate.min = todaysDate
if (notifications.noneUntil > Date.now()) {
notificationStatus.innerHTML = getDisabledUntilPhrase(notifications.noneUntil)
disabledDate.value = new Date(notifications.noneUntil).toISOString().split("T")[0]
}
else {
notificationStatus.innerHTML = "Email Notifications Enabled"
}
}
//Manage favorites.
//alwaysOverwrite will be used for edits performed on this page.
async function syncFavorites(alwaysOverwrite = false) {
if (!accounts.getCurrentUser()) {return}
let localFavorites = loadFavorites()
if (alwaysOverwrite) {
accounts.setFavorites(localFavorites, false)
return
}
let localLastModified = getFavoritesLastModified()
let data = await accounts.getData()
let netFavorites = await accounts.getFavorites(data)
let netLastModified = await accounts.getFavoritesLastModified(data)
await updateNotificationsUI(data)
//If net modified more recently than local, overwrite local with net.
//If local modified more recently than net:
// - It is possible that this should be the new favorites entirely.
// - It is possible that we just added a favorite, but are otherwise out of date and need to remove everything not in the sync.
// - Without a change history, this is hard to determine. Therefore, we will take the non-destructive approach and merge.
//The !== null check can be removed eventually.
//favoritesLastModified is a recent addition, and therefore genuine favorites lists might not have it set.
//Once favoritesLastModified has been out for long enough, it can be assumed that all users have it set.
if (netLastModified > localLastModified && localLastModified !== null) {
writeFavorites(netFavorites)
}
else {
let newFavorites = mergeFavoritesObjects(localFavorites, netFavorites)
console.log(localFavorites, netFavorites, newFavorites)
writeFavorites(newFavorites)
accounts.setFavorites(newFavorites)
}
dataChanged()
}
auth.onAuthStateChanged(function() {
syncFavorites()
})
//Format: [{id, name, minimum, maximum, units}]
let currentSelections = document.getElementById("currentSelections")
//Add the header
//This was HTML, but a space was added in between the spans, unless the spans had no whitespace between them.
currentSelections.innerHTML += '<div class="row"></div>'
currentSelections.firstChild.innerHTML += '<span class="gaugeColumn">Gauge</span>'
currentSelections.firstChild.innerHTML += '<span class="nameColumn">Name</span>'
currentSelections.firstChild.innerHTML += '<span class="minColumn">Min</span>'
currentSelections.firstChild.innerHTML += '<span class="maxColumn">Max</span>'
currentSelections.firstChild.innerHTML += '<span class="unitsColumn">Unit</span>'
//Rendering code.
async function redrawRows() {
//Clear the current list.
let rows = currentSelections.children
//The first child element is the header. Don't delete the firstChild, but the nextSibling.
while (currentSelections.firstChild.nextSibling) {
currentSelections.firstChild.nextSibling.remove()
}
let selections = loadFavorites()
//We'll sort favorites by name.
let ids = []
let rowElems = [] //Objects. {name, row}. Stored here to sort.
for (let gauge in selections) {
for (let id in selections[gauge]) {
ids.push(id)
let row = document.createElement("div")
row.className = "row"
let gaugeColumn = document.createElement("span")
gaugeColumn.className = "gaugeColumn"
gaugeColumn.innerHTML = gauge.split(":").join(":<wbr>")
if (gauge === "" || gauge === "undefined") {
gaugeColumn.innerHTML = "None"
}
row.appendChild(gaugeColumn)
let nameAndSection = selections[gauge][id].name + ((selections[gauge][id].section)?(" (" + selections[gauge][id].section) + ")":"")
let nameColumn = document.createElement("span")
nameColumn.className = "nameColumn"
nameColumn.innerHTML = `<a href="${getSearchLink([id], window.root)}">${nameAndSection}</a>`
row.appendChild(nameColumn)
let minColumn = document.createElement("span")
minColumn.className = "minColumn"
row.appendChild(minColumn)
let maxColumn = document.createElement("span")
maxColumn.className = "maxColumn"
row.appendChild(maxColumn)
let unitsColumn = document.createElement("span")
unitsColumn.className = "unitsColumn"
row.appendChild(unitsColumn)
function createUnitsSelector() {
let units = document.createElement("select")
let blank = document.createElement("option")
blank.value = "-"
blank.innerHTML = "-"
units.appendChild(blank)
let feet = document.createElement("option")
feet.value = "feet"
feet.innerHTML = "Feet"
units.appendChild(feet)
let cfs = document.createElement("option")
cfs.value = "cfs"
cfs.innerHTML = "CFS"
units.appendChild(cfs)
let meters = document.createElement("option")
meters.value = "meters"
meters.innerHTML = "Meters"
units.appendChild(meters)
let cms = document.createElement("option")
cms.value = "cms"
cms.innerHTML = "CMS"
units.appendChild(cms)
return units
}
if (gaugeColumn.innerHTML === "None") {
unitsColumn.innerHTML = maxColumn.innerHTML = minColumn.innerHTML = "N/A"
;[minColumn, maxColumn].forEach((col) => {
col.addEventListener("click", function() {
alert("This favorite has no gauge. If rivers.run has a gauge for it, try deleting this favorite and creating it again. ")
})
})
}
else {
minColumn.innerHTML = `<span class="underlineText">${selections[gauge][id].minimum ?? "---"}</span>`
maxColumn.innerHTML = `<span class="underlineText">${selections[gauge][id].maximum ?? "---"}</span>`
minColumn.addEventListener("click", function(e) {
//e.stopPropagation()
let val = Number(prompt("What would you like the minimum to be?"))
if (!val || isNaN(val)) {
return alert("Please enter a number with no units. Ex, 425 or 8000")
}
selections[gauge][id].minimum = val
writeFavorites(selections)
accounts.setFavorites(selections, false)
dataChanged()
})
maxColumn.addEventListener("click", function(e) {
//e.stopPropagation()
let val = Number(prompt("What would you like the maximum to be?"))
if (!val || isNaN(val)) {
return alert("Please enter a number with no units. Ex, 425 or 8000")
}
selections[gauge][id].maximum = val
writeFavorites(selections)
accounts.setFavorites(selections, false)
dataChanged()
})
let selector = createUnitsSelector()
unitsColumn.appendChild(selector)
selector.value = selections[gauge][id].units ?? "-"
selector.addEventListener("input", function() {
selections[gauge][id].units = selector.value
writeFavorites(selections)
accounts.setFavorites(selections, false)
dataChanged()
})
}
let deleteButton = document.createElement("span")
deleteButton.className = "deleteButton"
deleteButton.innerHTML = "✖"
row.appendChild(deleteButton)
deleteButton.addEventListener("click", function(e) {
//e.stopPropagation();
if (confirm("Remove " + selections[gauge][id].name + " at gauge " + gauge + "?")) {
delete selections[gauge][id]
//Trim out the gauge object if it is empty.
if (Object.keys(selections[gauge]).length === 0) {
delete selections[gauge]
}
writeFavorites(selections)
accounts.setFavorites(selections, false)
dataChanged()
}
})
rowElems.push({row, name: nameAndSection})
}
}
rowElems.sort((a, b) => {
if (a.name > b.name) {return 1}
else if (a.name < b.name) {return -1}
return 0
})
rowElems.forEach((obj) => {
currentSelections.appendChild(obj.row)
})
let favoritesLinks = document.getElementById("favoritesLinks")
favoritesLinks.innerHTML = `${ids.length} rivers in favorites - `
//On iOS, we want the search link to point to rivers.run, but go to the local server when clicked.
let a = document.createElement("a")
a.href = getSearchLink(ids)
a.innerHTML = "View all favorites on rivers.run"
a.addEventListener("click", function(e) {
window.location.href = getSearchLink(ids, window.root)
e.preventDefault()
})
favoritesLinks.appendChild(a)
}
async function dataChanged() {
redrawRows()
}
redrawRows()
//Utility buttons at top - delete all favorites and scroll to bottom.
let deleteAllFavorites = document.getElementById("deleteAllFavorites")
deleteAllFavorites.addEventListener("click", function() {
if (confirm("Delete all favorites?")) {
writeFavorites({})
accounts.setFavorites({}, false)
dataChanged()
}
})
let scrollToBottom = document.getElementById("scrollToBottom")
scrollToBottom.addEventListener("click", function() {
window.scrollTo(0, 1e10)
})