-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e2303fa
commit 6cd004b
Showing
5 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"]; | ||
Check failure on line 6 in client/app/controllers/subscribe.js GitHub Actions / Tests
Check failure on line 6 in client/app/controllers/subscribe.js GitHub Actions / Tests
Check failure on line 6 in client/app/controllers/subscribe.js GitHub Actions / Tests
Check failure on line 6 in client/app/controllers/subscribe.js GitHub Actions / Tests
|
||
|
||
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters