|
167 | 167 | </div> |
168 | 168 | </body> |
169 | 169 | <script type="module"> |
170 | | - import $ from "jquery"; |
171 | 170 | import { |
172 | 171 | applyActionCode, |
173 | 172 | verifyPasswordResetCode, |
|
179 | 178 | } from "firebase/auth"; |
180 | 179 | import { initializeApp } from "firebase/app"; |
181 | 180 | import { firebaseConfig } from "./ts/constants/firebase-config"; |
| 181 | + import { qs, onDOMReady } from "./ts/utils/dom"; |
182 | 182 |
|
183 | 183 | const app = initializeApp(firebaseConfig); |
184 | 184 | const Auth = getAuth(app); |
|
199 | 199 | .then((resp) => { |
200 | 200 | // Email address has been verified. |
201 | 201 |
|
202 | | - $("main .preloader .icon").html(`<i class="fas fa-fw fa-check"></i>`); |
203 | | - $("main .preloader .text").text( |
| 202 | + qs("main .preloader .icon")?.setHtml(`<i class="fas fa-fw fa-check"></i>`); |
| 203 | + qs("main .preloader .text")?.setText( |
204 | 204 | `Your email address has been verified`, |
205 | 205 | ); |
206 | | - $("main .preloader .subText").text(`You can now close this tab`); |
| 206 | + qs("main .preloader .subText")?.setText(`You can now close this tab`); |
207 | 207 | }) |
208 | 208 | .catch((error) => { |
209 | 209 | console.error(error); |
210 | | - $("main .preloader .icon").html(`<i class="fas fa-fw fa-times"></i>`); |
211 | | - $("main .preloader .text").text( |
| 210 | + qs("main .preloader .icon")?.setHtml(`<i class="fas fa-fw fa-times"></i>`); |
| 211 | + qs("main .preloader .text")?.setText( |
212 | 212 | `Fatal error: ${error.message}. If this issue persists, please report it.`, |
213 | 213 | ); |
214 | 214 | // Code is invalid or expired. Ask the user to verify their email address |
|
217 | 217 | } |
218 | 218 |
|
219 | 219 | function showResetPassword() { |
220 | | - $("main .preloader").addClass("hidden"); |
221 | | - $("main .resetPassword").removeClass("hidden"); |
222 | | - $("main .resetPassword input").trigger("focus"); |
| 220 | + qs("main .preloader")?.hide(); |
| 221 | + qs("main .resetPassword")?.show(); |
| 222 | + qs("main .resetPassword input")?.focus(); |
223 | 223 | } |
224 | 224 |
|
225 | 225 | function hideResetPassword() { |
226 | | - $("main .preloader").removeClass("hidden"); |
227 | | - $("main .resetPassword").addClass("hidden"); |
| 226 | + qs("main .preloader")?.show(); |
| 227 | + qs("main .resetPassword")?.hide(); |
228 | 228 | } |
229 | 229 |
|
230 | 230 | function handleResetPassword(actionCode, continueUrl) { |
|
234 | 234 | .then((email) => { |
235 | 235 | var accountEmail = email; |
236 | 236 |
|
237 | | - var newPassword = $("main .resetPassword .pwd").val(); |
238 | | - var newPasswordConfirm = $("main .resetPassword .pwd-confirm").val(); |
| 237 | + var newPassword = qs<HTMLInputElement>("main .resetPassword .pwd")?.getValue(); |
| 238 | + var newPasswordConfirm = qs<HTMLInputElement>("main .resetPassword .pwd-confirm")?.getValue(); |
239 | 239 |
|
240 | 240 | if (newPassword !== newPasswordConfirm) { |
241 | 241 | alert("Passwords do not match"); |
|
255 | 255 | confirmPasswordReset(Auth, actionCode, newPassword) |
256 | 256 | .then((resp) => { |
257 | 257 | // Password reset has been confirmed and new password updated. |
258 | | - $("main .preloader .icon").html( |
| 258 | + qs("main .preloader .icon")?.setHtml( |
259 | 259 | `<i class="fas fa-fw fa-check"></i>`, |
260 | 260 | ); |
261 | | - $("main .preloader .text").text(`Your password has been changed`); |
262 | | - $("main .preloader .subText").text(`You can now close this tab`); |
| 261 | + qs("main .preloader .text")?.setText(`Your password has been changed`); |
| 262 | + qs("main .preloader .subText")?.setText(`You can now close this tab`); |
263 | 263 |
|
264 | 264 | signInWithEmailAndPassword(Auth, accountEmail, newPassword); |
265 | 265 | }) |
266 | 266 | .catch((error) => { |
267 | 267 | console.error(error); |
268 | | - $("main .preloader .icon").html( |
| 268 | + qs("main .preloader .icon")?.setHtml( |
269 | 269 | `<i class="fas fa-fw fa-times"></i>`, |
270 | 270 | ); |
271 | | - $("main .preloader .text").text( |
| 271 | + qs("main .preloader .text")?.setText( |
272 | 272 | `Fatal error: ${error.message}. If this issue persists, please report it.`, |
273 | 273 | ); |
274 | 274 | // Error occurred during confirmation. The code might have expired or the |
|
277 | 277 | }) |
278 | 278 | .catch((error) => { |
279 | 279 | console.error(error); |
280 | | - $("main .preloader .icon").html(`<i class="fas fa-fw fa-times"></i>`); |
281 | | - $("main .preloader .text").text( |
| 280 | + qs("main .preloader .icon")?.setHtml(`<i class="fas fa-fw fa-times"></i>`); |
| 281 | + qs("main .preloader .text")?.setText( |
282 | 282 | `Fatal error: ${error.message}. If this issue persists, please report it.`, |
283 | 283 | ); |
284 | 284 |
|
285 | | - // $("main .preloader .subText").text(error); |
| 285 | + // qs("main .preloader .subText")?.setText(error); |
286 | 286 |
|
287 | 287 | // Invalid or expired action code. Ask user to try to reset the password |
288 | 288 | // again. |
|
303 | 303 | return applyActionCode(Auth, actionCode); |
304 | 304 | }) |
305 | 305 | .then(() => { |
306 | | - $("main .preloader .icon").html(`<i class="fas fa-fw fa-check"></i>`); |
307 | | - $("main .preloader .text").text(`Your account email was reverted.`); |
308 | | - $("main .preloader .subText").text(``); |
| 306 | + qs("main .preloader .icon")?.setHtml(`<i class="fas fa-fw fa-check"></i>`); |
| 307 | + qs("main .preloader .text")?.setText(`Your account email was reverted.`); |
| 308 | + qs("main .preloader .subText")?.setText(``); |
309 | 309 |
|
310 | | - $("main .preloader").append(` |
| 310 | + qs("main .preloader")?.appendHtml(` |
311 | 311 | <br> |
312 | 312 | In case you believe your account was compromised, please request a password reset email: |
313 | 313 | `); |
314 | | - $("main .preloader").append(` |
| 314 | + qs("main .preloader")?.appendHtml(` |
315 | 315 | <br> |
316 | 316 | <div class="button" onclick="sendPasswordReset('${restoredEmail}')">Send Password Reset Email</div> |
317 | 317 | `); |
|
325 | 325 | }) |
326 | 326 | .catch((error) => { |
327 | 327 | console.error(error); |
328 | | - $("main .preloader .icon").html(`<i class="fas fa-fw fa-times"></i>`); |
329 | | - $("main .preloader .text").text(error.message); |
| 328 | + qs("main .preloader .icon")?.setHtml(`<i class="fas fa-fw fa-times"></i>`); |
| 329 | + qs("main .preloader .text")?.setText(error.message); |
330 | 330 | }); |
331 | 331 | } |
332 | 332 |
|
333 | 333 | function sendPasswordReset(email) { |
334 | 334 | sendPasswordResetEmail(Auth, email) |
335 | 335 | .then(() => { |
336 | | - $("main .preloader .icon").html(`<i class="fas fa-fw fa-check"></i>`); |
337 | | - $("main .preloader .text").text(`Password reset email sent`); |
338 | | - $("main .preloader .subText").text(`Please check your inbox`); |
| 336 | + qs("main .preloader .icon")?.setHtml(`<i class="fas fa-fw fa-check"></i>`); |
| 337 | + qs("main .preloader .text")?.setText(`Password reset email sent`); |
| 338 | + qs("main .preloader .subText")?.setText(`Please check your inbox`); |
339 | 339 | }) |
340 | 340 | .catch((error) => { |
341 | 341 | console.error(error); |
342 | | - $("main .preloader .icon").html(`<i class="fas fa-fw fa-times"></i>`); |
343 | | - $("main .preloader .text").text(error.message); |
| 342 | + qs("main .preloader .icon")?.setHtml(`<i class="fas fa-fw fa-times"></i>`); |
| 343 | + qs("main .preloader .text")?.setText(error.message); |
344 | 344 | }); |
345 | 345 | } |
346 | 346 |
|
|
353 | 353 | return decodeURIComponent(results[2].replace(/\+/g, " ")); |
354 | 354 | } |
355 | 355 |
|
356 | | - document.addEventListener( |
357 | | - "DOMContentLoaded", |
358 | | - () => { |
| 356 | + onDOMReady(() => { |
359 | 357 | try { |
360 | 358 | // Get the action to complete. |
361 | 359 | var mode = getParameterByName("mode"); |
|
376 | 374 | // var auth = firebase.auth(); |
377 | 375 |
|
378 | 376 | if (!mode) { |
379 | | - $("main .preloader .icon").html( |
| 377 | + qs("main .preloader .icon")?.setHtml( |
380 | 378 | `<i class="fas fa-fw fa-times"></i>`, |
381 | 379 | ); |
382 | | - $("main .preloader .text").text(`Mode parameter not found`); |
| 380 | + qs("main .preloader .text")?.setText(`Mode parameter not found`); |
383 | 381 | return; |
384 | 382 | } |
385 | 383 |
|
386 | 384 | if (!actionCode) { |
387 | | - $("main .preloader .icon").html( |
| 385 | + qs("main .preloader .icon")?.setHtml( |
388 | 386 | `<i class="fas fa-fw fa-times"></i>`, |
389 | 387 | ); |
390 | | - $("main .preloader .text").text(`Action code parameter not found`); |
| 388 | + qs("main .preloader .text")?.setText(`Action code parameter not found`); |
391 | 389 | return; |
392 | 390 | } |
393 | 391 |
|
394 | 392 | // Handle the user management action. |
395 | 393 | switch (mode) { |
396 | 394 | case "resetPassword": |
397 | 395 | // Display reset password handler and UI. |
398 | | - $("#logo .text span").text("Reset Password"); |
| 396 | + qs("#logo .text span")?.setText("Reset Password"); |
399 | 397 | document.title = "Reset Password | Monkeytype"; |
400 | 398 | showResetPassword(); |
401 | 399 | break; |
|
404 | 402 | handleRecoverEmail(actionCode); |
405 | 403 | break; |
406 | 404 | case "verifyEmail": |
407 | | - $("#logo .text span").text("Verify Email"); |
| 405 | + qs("#logo .text span")?.setText("Verify Email"); |
408 | 406 | document.title = "Verify Email | Monkeytype"; |
409 | 407 | // Display email verification handler and UI. |
410 | 408 | handleVerifyEmail(actionCode, continueUrl); |
411 | 409 | break; |
412 | 410 | default: |
413 | | - $("main .preloader .icon").html( |
| 411 | + qs("main .preloader .icon")?.setHtml( |
414 | 412 | `<i class="fas fa-fw fa-times"></i>`, |
415 | 413 | ); |
416 | | - $("main .preloader .text").text(`Invalid mode`); |
| 414 | + qs("main .preloader .text")?.setText(`Invalid mode`); |
417 | 415 | console.error("no mode found"); |
418 | 416 | // Error: invalid mode. |
419 | 417 | } |
420 | 418 |
|
421 | | - $("main .resetPassword .button").on("click", () => { |
| 419 | + qs("main .resetPassword .button")?.on("click", () => { |
422 | 420 | handleResetPassword(actionCode, continueUrl); |
423 | 421 | }); |
424 | 422 |
|
425 | | - $("main .resetPassword input").on("keypress", (e) => { |
| 423 | + qs("main .resetPassword input")?.on("keypress", (e) => { |
426 | 424 | if (e.key === "Enter") handleResetPassword(actionCode, continueUrl); |
427 | 425 | }); |
428 | 426 | } catch (e) { |
429 | | - $("main .preloader .icon").html(`<i class="fas fa-fw fa-times"></i>`); |
430 | | - $("main .preloader .text").text( |
| 427 | + qs("main .preloader .icon")?.setHtml(`<i class="fas fa-fw fa-times"></i>`); |
| 428 | + qs("main .preloader .text")?.setText( |
431 | 429 | `Fatal error: ${e.message}. If this issue persists, please report it.`, |
432 | 430 | ); |
433 | 431 | } |
434 | | - }, |
435 | | - false, |
436 | | - ); |
| 432 | + }); |
437 | 433 |
|
438 | | - document.querySelector("header").addEventListener("click", () => { |
| 434 | + qs("header")?.on("click", () => { |
439 | 435 | window.location = "/"; |
440 | 436 | }); |
441 | 437 | </script> |
|
0 commit comments