Skip to content

Commit

Permalink
Added email checks
Browse files Browse the repository at this point in the history
  • Loading branch information
dhochbaum-dcp committed Dec 9, 2024
1 parent e2303fa commit 6cd004b
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 2 deletions.
71 changes: 71 additions & 0 deletions client/app/controllers/subscribe.js
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

View workflow job for this annotation

GitHub Actions / Tests

Strings must use singlequote

Check failure on line 6 in client/app/controllers/subscribe.js

View workflow job for this annotation

GitHub Actions / Tests

Strings must use singlequote

Check failure on line 6 in client/app/controllers/subscribe.js

View workflow job for this annotation

GitHub Actions / Tests

Strings must use singlequote

Check failure on line 6 in client/app/controllers/subscribe.js

View workflow job for this annotation

GitHub Actions / Tests

Strings must use singlequote

Check failure on line 6 in client/app/controllers/subscribe.js

View workflow job for this annotation

GitHub Actions / Tests

Strings must use singlequote

export default class SubscribeController extends Controller {
lastEmailChecked = "";

Check failure on line 9 in client/app/controllers/subscribe.js

View workflow job for this annotation

GitHub Actions / Tests

Strings must use singlequote
emailAlreadyExists = false;

Check failure on line 10 in client/app/controllers/subscribe.js

View workflow job for this annotation

GitHub Actions / Tests

Expected blank line between class members
emailNeedsConfirmation = false;

Check failure on line 11 in client/app/controllers/subscribe.js

View workflow job for this annotation

GitHub Actions / Tests

Expected blank line between class members
startContinuouslyChecking = false;

Check failure on line 12 in client/app/controllers/subscribe.js

View workflow job for this annotation

GitHub Actions / Tests

Expected blank line between class members
emailSent = false;

Check failure on line 13 in client/app/controllers/subscribe.js

View workflow job for this annotation

GitHub Actions / Tests

Expected blank line between class members


@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);
}
}

}
1 change: 1 addition & 0 deletions client/app/styles/app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
13 changes: 13 additions & 0 deletions client/app/styles/modules/_m-subscribe.scss
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;
}
1 change: 0 additions & 1 deletion client/app/templates/components/subscription-form.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<ul>
<li>
<div class="update-type-item">
{{log "isCityWide" isCityWide}}
<Input @id="city-wide-checkbox" @type="checkbox" @checked={{isCityWide}} {{on "change" this.toggleCitywide}}/>
<div class="update-type-label">
<label for="city-wide-checkbox">Citywide Updates</label>
Expand Down
11 changes: 10 additions & 1 deletion client/app/templates/subscribe.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,17 @@
<div class="subscribe-section">
<div class="subscribe-input-group">
<label class="email-label">Email Address</label>
<Input class="input-group-field" type="text" @value={{this.model.email}}/>
<Input class="input-group-field" type="text" @value={{this.model.email}} onkeyup={{action 'continuouslyCheckEmail'}} onchange={{action 'checkExistingEmail'}} />
</div>
{{#if this.emailAlreadyExists}}
<p class="email-exists">This email already is subscribed to ZAP Updates. <a onclick={{action 'sendEmail'}}>Click here to receive an email to modify subscriptions.</a></p>
{{/if}}
{{#if this.emailNeedsConfirmation}}
<p class="email-needs-confirmation">This email address has not confirmed their subscription to ZAP Updates. <a onclick={{action 'sendEmail'}}>Click here to re-send the confirmation email.</a></p>
{{/if}}
{{#if this.emailSent}}
<p class="email-sent">Email sent.</p>
{{/if}}
</div>
</SubscriptionForm>
<div class="subscribe-footer">
Expand Down

0 comments on commit 6cd004b

Please sign in to comment.