perf(InvitationWithTypoComponent): check email once from controller, forward actuel/suggestion email from ctrl to view

This commit is contained in:
mfo 2024-06-25 14:37:54 +02:00
parent 4771c45bce
commit afa9821edb
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
3 changed files with 12 additions and 14 deletions

View file

@ -8,13 +8,4 @@ class Procedure::InvitationWithTypoComponent < ApplicationComponent
def render? def render?
@maybe_typo.present? @maybe_typo.present?
end end
def maybe_typos
email_checker = EmailChecker.new
@maybe_typo.map do |actual_email|
suggested_email = email_checker.check(email: actual_email)[:email_suggestions].first
[actual_email, suggested_email]
end
end
end end

View file

@ -2,7 +2,7 @@
- c.with_body do - c.with_body do
%p= @title %p= @title
%ul %ul
- maybe_typos.each do |(actual_email, suggested_email)| - @maybe_typo.each do |(actual_email, suggested_email)|
%li %li
= "Je confirme " = "Je confirme "
= button_to "#{actual_email}", @url, method: :POST, params: { maybe_typo: actual_email }, class: 'fr-btn fr-btn--tertiary fr-btn--sm', form: {class: 'inline'} = button_to "#{actual_email}", @url, method: :POST, params: { maybe_typo: actual_email }, class: 'fr-btn fr-btn--tertiary fr-btn--sm', form: {class: 'inline'}

View file

@ -9,12 +9,19 @@ module Administrateurs
end end
def create def create
email_checker = EmailChecker.new
emails = params['emails'].presence || [].to_json emails = params['emails'].presence || [].to_json
@maybe_typo, emails = JSON.parse(emails) emails = JSON.parse(emails).map { EmailSanitizer.sanitize(_1) }
.map { EmailSanitizer.sanitize(_1) } @maybe_typo, emails = emails.map do |email|
.partition { EmailChecker.new.check(email: _1)[:email_suggestions].present? } result = email_checker.check(email: email)
if result[:email_suggestions].present?
[email, result[:email_suggestions].first]
else
[email, nil]
end
end.partition { _1[1].present? }
errors = if !@maybe_typo.empty? errors = if !@maybe_typo.empty?
["Attention, nous pensons avoir identifié une faute de frappe dans les invitations : #{@maybe_typo.join(', ')}"] ["Attention, nous pensons avoir identifié une faute de frappe dans les invitations : #{@maybe_typo.map(&:first).join(', ')}"]
else else
[] []
end end