Skip to content

Commit

Permalink
feat(email_check): change strategy to check email, dropping email_but…
Browse files Browse the repository at this point in the history
…tler package and using a custom EmailChecker
  • Loading branch information
mfo committed Jun 7, 2024
1 parent c813c02 commit 66eb3dc
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/components/dsfr/input_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def input_group_opts
}.merge(input_group_error_class_names))
}
if email?
opts[:data] = { controller: 'email-input' }
opts[:data] = { controller: 'email-input', email_input_url_value: show_email_suggestions_path }
end
opts
end
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/email_checker_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class EmailCheckerController < ApplicationController
def show
render json: EmailChecker.check(email: params[:email])
end
end
31 changes: 26 additions & 5 deletions app/javascript/controllers/email_input_controller.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,39 @@
import { suggest } from 'email-butler';
import { httpRequest } from '@utils';
import { show, hide } from '@utils';
import { ApplicationController } from './application_controller';

type checkEmailResponse = {
success: boolean;
email_suggestions: string[];
};

export class EmailInputController extends ApplicationController {
static targets = ['ariaRegion', 'suggestion', 'input'];

static values = {
url: String
};

declare readonly urlValue: string;

declare readonly ariaRegionTarget: HTMLElement;
declare readonly suggestionTarget: HTMLElement;
declare readonly inputTarget: HTMLInputElement;

checkEmail() {
const suggestion = suggest(this.inputTarget.value);
if (suggestion && suggestion.full) {
this.suggestionTarget.innerHTML = suggestion.full;
async checkEmail() {
if (!this.inputTarget.value) {
return;
}

const url = new URL(this.urlValue, document.baseURI);
url.searchParams.append('email', this.inputTarget.value);

const data: checkEmailResponse | null = await httpRequest(
url.toString()
).json();

if (data && data.email_suggestions && data.email_suggestions.length > 0) {
this.suggestionTarget.innerHTML = data.email_suggestions[0];
show(this.ariaRegionTarget);
this.ariaRegionTarget.setAttribute('aria-live', 'assertive');
}
Expand Down
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@
end

get 'password_complexity' => 'password_complexity#show', as: 'show_password_complexity'
get 'check_email' => 'email_checker#show', as: 'show_email_suggestions'

resources :targeted_user_links, only: [:show]

Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
"core-js": "^3.37.1",
"date-fns": "^2.30.0",
"debounce": "^1.2.1",
"email-butler": "^1.0.13",
"geojson": "^0.5.0",
"graphiql": "^3.2.3",
"graphql": "^16.8.1",
Expand Down
39 changes: 39 additions & 0 deletions spec/controllers/email_checker_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
describe EmailCheckerController, type: :controller do
describe '#show' do
render_views
before { get :show, format: :json, params: params }
let(:body) { JSON.parse(response.body, symbolize_names: true) }

context 'valid email' do
let(:params) { { email: '[email protected]' } }
it do
expect(response).to have_http_status(:success)
expect(body).to eq({ success: true })
end
end

context 'email with typo' do
let(:params) { { email: '[email protected]' } }
it do
expect(response).to have_http_status(:success)
expect(body).to eq({ success: true, email_suggestions: ['[email protected]'] })
end
end

context 'empty' do
let(:params) { { email: '' } }
it do
expect(response).to have_http_status(:success)
expect(body).to eq({ success: false })
end
end

context 'notanemail' do
let(:params) { { email: 'clarkkent' } }
it do
expect(response).to have_http_status(:success)
expect(body).to eq({ success: false })
end
end
end
end

0 comments on commit 66eb3dc

Please sign in to comment.