Skip to content

Commit

Permalink
validating email faster after typing
Browse files Browse the repository at this point in the history
  • Loading branch information
enoodle committed Feb 9, 2019
1 parent b519860 commit b83a13b
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 4 deletions.
7 changes: 7 additions & 0 deletions app/assets/stylesheets/forms.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
@extend %textOverflowEllipsis;
}

.email_valid {
background-color: lighten($c-green, 30%);
}
.email_invalid {
background-color: lighten($c-red, 30%);
}

.menu-heading {
height: 79px;
margin: 28px 0 56px 0;
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/persons_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ class PersonsController < ApplicationController

def export
end

def verify_email
render :json => { verified: Person.email_registered?(params[:email]) }
end
end
31 changes: 30 additions & 1 deletion app/views/camps/_person_fields.haml
Original file line number Diff line number Diff line change
@@ -1,3 +1,32 @@
:javascript

function verify_person_email(event) {
const email_field = event.target;
if (email_field.value != '') {
verify_email(email_field.value, function (valid) {
console.log(`email is valid: ${valid}`);
if (!valid) {
email_field.classList.remove("email_valid");
email_field.classList.add("email_invalid");
} else {
email_field.classList.remove("email_invalid");
email_field.classList.add("email_valid");
}
});
}
}

function verify_email(email, callback) {
var request = new XMLHttpRequest();
request.open("POST", '/verify_email', true);
request.setRequestHeader('Content-Type', 'application/json');
request.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));
request.onload = function() {
callback(JSON.parse(request.response)['verified']);
};
request.send(JSON.stringify({ "email": email }));
}

- dream_admin ||= false

.nested-fields.panel.panel-default.col-md-7{ style: 'float:' + I18n.t('lang_direction')}
Expand All @@ -13,7 +42,7 @@
= f.text_field :name, class: 'form-control', readonly: true
= f.label :email
- if (@can_edit)
= f.text_field :email, class: 'form-control'
= f.text_field :email, class: 'form-control person_email', onfocusout: 'verify_person_email(event)'
- else
= f.text_field :email, class: 'form-control', readonly: true
= f.label :phone_number
Expand Down
2 changes: 1 addition & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ en:
modified_at: Modified at
new_dream_is_disabled: Creating new dreams is disabled at the moment. If you think
this is a mistake please contact bureau of dreams
email_not_in_spark: This email is not registered in Spark
email_not_in_spark: Sorry, but we couldn't find a Midburn profile associated with
dont_miss_out:
banner: Don't miss out! Only %{time} left to %{action}
actions:
Expand Down
2 changes: 1 addition & 1 deletion config/locales/he.yml
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ he:

# Errors
new_dream_is_disabled: "יצירת חלומות חדשים מנוטרלת כרגע. אם אתה חושב שזאת שגיאה אנא צור קשר עם לשכת החלומות"
email_not_in_spark: "האימייל לא רשום בספארק:"
email_not_in_spark: "אופס! לא מצאנו פרופיל מידברן מקושר למייל: "

time:
am: am
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
get '/howcanihelp' => 'howcanihelp#index'

get '/people/export' => 'people#export_csv'

post '/verify_email' => 'persons#verify_email'

get '*unmatched_route' => 'application#not_found'
end
2 changes: 1 addition & 1 deletion spec/controllers/camps_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
recycling: 'recycling plan',
budgetplan: 'budgetplan plan',
cocreation: 'cocreation plan',
people_attributes: {'0' => {name: Faker::Name.name}}
people_attributes: {'0' => {name: Faker::Name.name, email: Faker::Internet.email}}
}
}

Expand Down

0 comments on commit b83a13b

Please sign in to comment.