From afa9821edb579c5ecf8e10567b51d8ceea5b97a7 Mon Sep 17 00:00:00 2001 From: mfo Date: Tue, 25 Jun 2024 14:37:54 +0200 Subject: [PATCH] perf(InvitationWithTypoComponent): check email once from controller, forward actuel/suggestion email from ctrl to view --- .../procedure/invitation_with_typo_component.rb | 9 --------- .../invitation_with_typo_component.html.haml | 2 +- .../experts_procedures_controller.rb | 15 +++++++++++---- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/app/components/procedure/invitation_with_typo_component.rb b/app/components/procedure/invitation_with_typo_component.rb index 53062a975..72b581514 100644 --- a/app/components/procedure/invitation_with_typo_component.rb +++ b/app/components/procedure/invitation_with_typo_component.rb @@ -8,13 +8,4 @@ class Procedure::InvitationWithTypoComponent < ApplicationComponent def render? @maybe_typo.present? 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 diff --git a/app/components/procedure/invitation_with_typo_component/invitation_with_typo_component.html.haml b/app/components/procedure/invitation_with_typo_component/invitation_with_typo_component.html.haml index c114f78da..8f63cb028 100644 --- a/app/components/procedure/invitation_with_typo_component/invitation_with_typo_component.html.haml +++ b/app/components/procedure/invitation_with_typo_component/invitation_with_typo_component.html.haml @@ -2,7 +2,7 @@ - c.with_body do %p= @title %ul - - maybe_typos.each do |(actual_email, suggested_email)| + - @maybe_typo.each do |(actual_email, suggested_email)| %li = "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'} diff --git a/app/controllers/administrateurs/experts_procedures_controller.rb b/app/controllers/administrateurs/experts_procedures_controller.rb index d606e0821..edd06a6c0 100644 --- a/app/controllers/administrateurs/experts_procedures_controller.rb +++ b/app/controllers/administrateurs/experts_procedures_controller.rb @@ -9,12 +9,19 @@ module Administrateurs end def create + email_checker = EmailChecker.new emails = params['emails'].presence || [].to_json - @maybe_typo, emails = JSON.parse(emails) - .map { EmailSanitizer.sanitize(_1) } - .partition { EmailChecker.new.check(email: _1)[:email_suggestions].present? } + emails = JSON.parse(emails).map { EmailSanitizer.sanitize(_1) } + @maybe_typo, emails = emails.map do |email| + 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? - ["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 [] end