tech(refactor): procedure::error_summary and dossier::ErrorsFullMessagesComponent use same behaviour to compact/expand errors
This commit is contained in:
parent
c480bc00c3
commit
e3a24d53ea
16 changed files with 115 additions and 78 deletions
|
@ -3,17 +3,15 @@
|
|||
class Dossiers::ErrorsFullMessagesComponent < ApplicationComponent
|
||||
ErrorDescriptor = Data.define(:anchor, :label, :error_message)
|
||||
|
||||
def initialize(dossier:, errors:)
|
||||
def initialize(dossier:)
|
||||
@dossier = dossier
|
||||
@errors = errors
|
||||
end
|
||||
|
||||
def dedup_and_partitioned_errors
|
||||
formated_errors = @errors.to_enum # ActiveModel::Errors.to_a is an alias to full_messages, we don't want that
|
||||
@dossier.errors.to_enum # ActiveModel::Errors.to_a is an alias to full_messages, we don't want that
|
||||
.to_a # but enum.to_a gives back an array
|
||||
.uniq { |error| [error.inner_error.base] } # dedup cumulated errors from dossier.champs, dossier.champs_public, dossier.champs_private which run the validator one time per association
|
||||
.map { |error| to_error_descriptor(error) }
|
||||
yield(Array(formated_errors[0..2]), Array(formated_errors[3..]))
|
||||
end
|
||||
|
||||
def to_error_descriptor(error)
|
||||
|
@ -27,6 +25,6 @@ class Dossiers::ErrorsFullMessagesComponent < ApplicationComponent
|
|||
end
|
||||
|
||||
def render?
|
||||
!@errors.empty?
|
||||
!@dossier.errors.empty?
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,4 +5,3 @@ en:
|
|||
Your file has 1 error. <a href="%{url}">Fix-it</a> to continue :
|
||||
other: |
|
||||
Your file has %{count} errors. <a href="%{url}">Fix-them</a> to continue :
|
||||
see_more: Show all errors
|
||||
|
|
|
@ -5,4 +5,3 @@ fr:
|
|||
Votre dossier contient 1 champ en erreur. <a href="%{url}">Corrigez-la</a> pour poursuivre :
|
||||
other: |
|
||||
Votre dossier contient %{count} champs en erreurs. <a href="%{url}">Corrigez-les</a> pour poursuivre :
|
||||
see_more: Afficher toutes les erreurs
|
||||
|
|
|
@ -1,15 +1,4 @@
|
|||
.fr-alert.fr-alert--error.fr-mb-3w{ role: "alertdialog" }
|
||||
- dedup_and_partitioned_errors do |head, tail|
|
||||
%p#sumup-errors= t('.sumup_html', count: head.size + tail.size, url: head.first.anchor)
|
||||
%ul.fr-mb-0#head-errors
|
||||
- head.each do |error_descriptor|
|
||||
%li
|
||||
= link_to error_descriptor.label, error_descriptor.anchor, class: 'error-anchor'
|
||||
= error_descriptor.error_message
|
||||
- if tail.size > 0
|
||||
%button{ type: "button", "aria-controls": 'tail-errors', "aria-expanded": "false", class: "fr-btn fr-btn--sm fr-btn--tertiary-no-outline" }= t('.see_more')
|
||||
%ul#tail-errors.fr-collapse.fr-mt-0
|
||||
- tail.each do |error_descriptor|
|
||||
%li
|
||||
= link_to error_descriptor.label, error_descriptor.anchor, class: 'error-anchor'
|
||||
= "(#{error_descriptor.error_message})"
|
||||
- if dedup_and_partitioned_errors.size > 0
|
||||
%p#sumup-errors= t('.sumup_html', count: dedup_and_partitioned_errors.size, url: dedup_and_partitioned_errors.first.anchor)
|
||||
= render ExpandableErrorList.new(errors: dedup_and_partitioned_errors)
|
||||
|
|
9
app/components/expandable_error_list.rb
Normal file
9
app/components/expandable_error_list.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
class ExpandableErrorList < ApplicationComponent
|
||||
def initialize(errors:)
|
||||
@errors = errors
|
||||
end
|
||||
|
||||
def splitted_errors
|
||||
yield(Array(@errors[0..2]), Array(@errors[3..]))
|
||||
end
|
||||
end
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
en:
|
||||
see_more: Show all errors
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
fr:
|
||||
see_more: Afficher toutes les erreurs
|
|
@ -0,0 +1,14 @@
|
|||
- splitted_errors do |head, tail|
|
||||
%ul#head-errors.fr-mb-0
|
||||
- head.each do |error_descriptor|
|
||||
%li
|
||||
= link_to error_descriptor.label, error_descriptor.anchor, class: 'error-anchor'
|
||||
= error_descriptor.error_message
|
||||
|
||||
- if tail.size > 0
|
||||
%button.fr-mt-0.fr-btn.fr-btn--sm.fr-btn--tertiary-no-outline{ type: "button", "aria-controls": 'tail-errors', "aria-expanded": "false", class: "" }= t('see_more')
|
||||
%ul#tail-errors.fr-collapse.fr-mt-0
|
||||
- tail.each do |error_descriptor|
|
||||
%li
|
||||
= link_to error_descriptor.label, error_descriptor.anchor, class: 'error-anchor'
|
||||
= error_descriptor.error_message
|
|
@ -1,4 +1,6 @@
|
|||
class Procedure::ErrorsSummary < ApplicationComponent
|
||||
ErrorDescriptor = Data.define(:anchor, :label, :error_message)
|
||||
|
||||
def initialize(procedure:, validation_context:)
|
||||
@procedure = procedure
|
||||
@validation_context = validation_context
|
||||
|
@ -24,10 +26,8 @@ class Procedure::ErrorsSummary < ApplicationComponent
|
|||
@procedure.errors.present?
|
||||
end
|
||||
|
||||
def error_messages
|
||||
@procedure.errors.map do |error|
|
||||
[error, error_correction_page(error)]
|
||||
end
|
||||
def errors
|
||||
@procedure.errors.map { to_error_descriptor(_1) }
|
||||
end
|
||||
|
||||
def error_correction_page(error)
|
||||
|
@ -45,4 +45,14 @@ class Procedure::ErrorsSummary < ApplicationComponent
|
|||
edit_admin_procedure_mail_template_path(@procedure, klass.const_get(:SLUG))
|
||||
end
|
||||
end
|
||||
|
||||
def to_error_descriptor(error)
|
||||
libelle = case error.attribute
|
||||
when :draft_types_de_champ_public, :draft_types_de_champ_private
|
||||
error.options[:type_de_champ].libelle.truncate(200)
|
||||
else
|
||||
error.base.class.human_attribute_name(error.attribute)
|
||||
end
|
||||
ErrorDescriptor.new(error_correction_page(error), libelle, error.message)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,8 +2,4 @@
|
|||
- if invalid?
|
||||
= render Dsfr::AlertComponent.new(state: :error, title: , extra_class_names: 'fr-mb-2w') do |c|
|
||||
- c.with_body do
|
||||
- error_messages.each do |(error, path)|
|
||||
%p.mt-2
|
||||
= error.full_message
|
||||
- if path.present?
|
||||
= "(#{link_to 'corriger', path, class: 'fr-link'})"
|
||||
= render ExpandableErrorList.new(errors:)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue