From 8e58c58886d32a2fbe2099b629f8235ea0ac8fea Mon Sep 17 00:00:00 2001 From: David Hochbaum Date: Tue, 3 Dec 2024 16:40:05 -0500 Subject: [PATCH 1/8] Added email checks --- client/app/controllers/subscribe.js | 71 +++++++++++++++++++++ client/app/styles/app.scss | 1 + client/app/styles/modules/_m-subscribe.scss | 13 ++++ client/app/templates/subscribe.hbs | 11 +++- 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 client/app/controllers/subscribe.js create mode 100644 client/app/styles/modules/_m-subscribe.scss diff --git a/client/app/controllers/subscribe.js b/client/app/controllers/subscribe.js new file mode 100644 index 00000000..a964f8e5 --- /dev/null +++ b/client/app/controllers/subscribe.js @@ -0,0 +1,71 @@ +import Controller from '@ember/controller'; +import { action } from '@ember/object'; +import fetch from 'fetch'; +import ENV from 'labs-zap-search/config/environment'; + +const tlds = [".com", ".gov", ".edu", ".net", ".org"]; + +export default class SubscribeController extends Controller { + lastEmailChecked = ""; + emailAlreadyExists = false; + emailNeedsConfirmation = false; + startContinuouslyChecking = false; + emailSent = false; + + + @action + async checkExistingEmail(event) { + const email = event.target.value; + if(email === this.lastEmailChecked) { return; } + this.set('lastEmailChecked', email); + this.set('startContinuouslyChecking', true); + + try { + const response = await fetch(`${ENV.host}/subscribers/email/${email}`); + const userData = await response.json(); + + if (userData.error) { + this.set('emailAlreadyExists', false); + this.set('emailNeedsConfirmation', false); + this.set('emailSent', false); + return; + } + + if (userData.message === "User already subscribed.") { + this.set('emailAlreadyExists', true); + this.set('emailNeedsConfirmation', false); + } else if (userData.message === "User is subscribed but must confirm.") { + this.set('emailNeedsConfirmation', true); + this.set('emailAlreadyExists', false); + } + return; + } catch (error) { + // We will receive an error if: + // a) the user does not exist in Sendgrid, or + // b) their confirmed field is null. + // Either way, we don't need to log to console + this.set('emailAlreadyExists', false); + this.set('emailNeedsConfirmation', false); + this.set('emailSent', false); + } + } + + @action + continuouslyCheckEmail(event) { + if((this.startContinuouslyChecking) || (tlds.includes(event.target.value.slice(-4)))) { this.checkExistingEmail(event) } + } + + @action + sendEmail() { + if (this.emailAlreadyExists) { + // Run the script to update the email + // Endpoint does not yet exist + this.set('emailSent', true); + } else if (this.emailNeedsConfirmation) { + // Run the script to confirm the email + // Endpoint does not yet exist + this.set('emailSent', true); + } + } + +} diff --git a/client/app/styles/app.scss b/client/app/styles/app.scss index 0ab8f05f..5b15b623 100644 --- a/client/app/styles/app.scss +++ b/client/app/styles/app.scss @@ -69,6 +69,7 @@ $completed-color: #a6cee3; @import 'modules/_m-search'; @import 'modules/_m-site-header'; @import 'modules/_m-statuses'; +@import 'modules/_m-subscribe'; @import 'modules/_m-subscribed'; @import 'modules/_m-subscribers-confirm'; @import 'modules/_m-subscription-form'; diff --git a/client/app/styles/modules/_m-subscribe.scss b/client/app/styles/modules/_m-subscribe.scss new file mode 100644 index 00000000..ca67ac83 --- /dev/null +++ b/client/app/styles/modules/_m-subscribe.scss @@ -0,0 +1,13 @@ +// -------------------------------------------------- +// Module: Subscribe Page +// -------------------------------------------------- + +.email-exists, .email-needs-confirmation { + padding-top: 1.5rem; + font-weight: 700; +} + +.email-sent { + color: $success-color; + font-weight: 700; +} \ No newline at end of file diff --git a/client/app/templates/subscribe.hbs b/client/app/templates/subscribe.hbs index e54240fe..2cb66a9c 100644 --- a/client/app/templates/subscribe.hbs +++ b/client/app/templates/subscribe.hbs @@ -11,8 +11,17 @@
+ {{#if this.emailAlreadyExists}} + + {{/if}} + {{#if this.emailNeedsConfirmation}} + + {{/if}} + {{#if this.emailSent}} + + {{/if}}
- +