feat(email_check): change strategy to check email, dropping email_buttler package and using a custom EmailChecker

This commit is contained in:
mfo 2024-06-07 10:06:40 +02:00
parent c813c02975
commit 66eb3dc821
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
7 changed files with 72 additions and 7 deletions

View file

@ -32,7 +32,7 @@ class Dsfr::InputComponent < ApplicationComponent
}.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

View file

@ -0,0 +1,5 @@
class EmailCheckerController < ApplicationController
def show
render json: EmailChecker.check(email: params[:email])
end
end

View file

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

BIN
bun.lockb

Binary file not shown.

View file

@ -161,6 +161,7 @@ Rails.application.routes.draw do
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]

View file

@ -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",

View file

@ -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: 'martin@orange.fr' } }
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: 'martin@orane.fr' } }
it do
expect(response).to have_http_status(:success)
expect(body).to eq({ success: true, email_suggestions: ['martin@orange.fr'] })
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