From b519860c2c18ddddffe14de573a5642be89352fc Mon Sep 17 00:00:00 2001 From: Erez Freiberger Date: Sun, 10 Feb 2019 01:54:48 +0200 Subject: [PATCH 1/2] fixing person validation issues --- app/controllers/camps_controller.rb | 4 ++-- app/models/person.rb | 24 +++++++++++++++--------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/app/controllers/camps_controller.rb b/app/controllers/camps_controller.rb index 54e5236c..50a45ddb 100644 --- a/app/controllers/camps_controller.rb +++ b/app/controllers/camps_controller.rb @@ -195,8 +195,8 @@ def update end else respond_to do |format| - flash.now[:alert] = "#{t:errors_str}: #{@camp.errors.full_messages.uniq.join(', ')}" - format.html { render :action => "edit" } + flash[:alert] = "#{t:errors_str}: #{@camp.errors.full_messages.uniq.join(', ')}" + format.html { redirect_to edit_camp_path(id: @camp.id, step: params[:step].to_i) } format.json { respond_with_bip(@camp) } end end diff --git a/app/models/person.rb b/app/models/person.rb index f0822647..1b2e40f1 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -29,6 +29,7 @@ class Person < ActiveRecord::Base has_and_belongs_to_many :roles validates :name, presence: true + validates :email, presence: true validates_with EmailInSpark, :on => :create schema_validations whitelist: [:id, :created_at, :updated_at, :camp] @@ -50,14 +51,19 @@ def self.to_csv end def self.email_registered?(email) - r = HTTParty.post( - URI.join(ENV['SPARK_HOST'], 'volunteers/profiles').to_s, - body: {emails: [email]}.to_json, - headers: { - token: ENV['SPARK_TOKEN'], - 'Content-Type': 'application/json' - } - ) - JSON.parse(r.body)[0].key? 'user_data' + begin + r = HTTParty.post( + URI.join(ENV['SPARK_HOST'], 'volunteers/profiles').to_s, + body: {emails: [email]}.to_json, + headers: { + token: ENV['SPARK_TOKEN'], + 'Content-Type': 'application/json' + }, + timeout: 10 + ) + JSON.parse(r.body)[0].key? 'user_data' + rescue + false + end end end From b83a13be7d2069c035308ec58cb281e7d2f12482 Mon Sep 17 00:00:00 2001 From: Erez Freiberger Date: Sun, 10 Feb 2019 01:55:18 +0200 Subject: [PATCH 2/2] validating email faster after typing --- app/assets/stylesheets/forms.css.scss | 7 +++++ app/controllers/persons_controller.rb | 4 +++ app/views/camps/_person_fields.haml | 31 ++++++++++++++++++++++- config/locales/en.yml | 2 +- config/locales/he.yml | 2 +- config/routes.rb | 2 ++ spec/controllers/camps_controller_spec.rb | 2 +- 7 files changed, 46 insertions(+), 4 deletions(-) diff --git a/app/assets/stylesheets/forms.css.scss b/app/assets/stylesheets/forms.css.scss index 1775d75f..ae801b94 100644 --- a/app/assets/stylesheets/forms.css.scss +++ b/app/assets/stylesheets/forms.css.scss @@ -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; diff --git a/app/controllers/persons_controller.rb b/app/controllers/persons_controller.rb index d4dd2df2..a1639730 100644 --- a/app/controllers/persons_controller.rb +++ b/app/controllers/persons_controller.rb @@ -3,4 +3,8 @@ class PersonsController < ApplicationController def export end + + def verify_email + render :json => { verified: Person.email_registered?(params[:email]) } + end end diff --git a/app/views/camps/_person_fields.haml b/app/views/camps/_person_fields.haml index 50017f07..d1f070f9 100644 --- a/app/views/camps/_person_fields.haml +++ b/app/views/camps/_person_fields.haml @@ -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')} @@ -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 diff --git a/config/locales/en.yml b/config/locales/en.yml index ca5f99d4..19c5b23d 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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: diff --git a/config/locales/he.yml b/config/locales/he.yml index 993027ed..5ce33e79 100755 --- a/config/locales/he.yml +++ b/config/locales/he.yml @@ -564,7 +564,7 @@ he: # Errors new_dream_is_disabled: "יצירת חלומות חדשים מנוטרלת כרגע. אם אתה חושב שזאת שגיאה אנא צור קשר עם לשכת החלומות" - email_not_in_spark: "האימייל לא רשום בספארק:" + email_not_in_spark: "אופס! לא מצאנו פרופיל מידברן מקושר למייל: " time: am: am diff --git a/config/routes.rb b/config/routes.rb index a27e7027..cb48f58b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/spec/controllers/camps_controller_spec.rb b/spec/controllers/camps_controller_spec.rb index aa0cbb0b..8fe4ed41 100644 --- a/spec/controllers/camps_controller_spec.rb +++ b/spec/controllers/camps_controller_spec.rb @@ -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}} } }