From ad671092c002f500ad02df802016c221599417a6 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Wed, 11 Oct 2023 19:06:25 +0200 Subject: [PATCH] refactor(announces): note form as view component --- app/components/release_note/form_component.rb | 31 +++++++++++++++++++ .../form_component/form_component.html.haml} | 19 ++++++++++-- .../release_notes/add_note.turbo_stream.haml | 1 + .../super_admins/release_notes/edit.html.haml | 2 +- spec/factories/release_notes.rb | 8 +++++ 5 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 app/components/release_note/form_component.rb rename app/{views/super_admins/release_notes/_form.html.haml => components/release_note/form_component/form_component.html.haml} (57%) create mode 100644 app/views/super_admins/release_notes/add_note.turbo_stream.haml create mode 100644 spec/factories/release_notes.rb diff --git a/app/components/release_note/form_component.rb b/app/components/release_note/form_component.rb new file mode 100644 index 000000000..b6becd279 --- /dev/null +++ b/app/components/release_note/form_component.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +class ReleaseNote::FormComponent < ApplicationComponent + attr_reader :release_note + + def initialize(release_note:) + @release_note = release_note + end + + private + + def categories_fieldset_class + class_names( + "fr-fieldset--error": categories_error? + ) + end + + def categories_error? + release_note.errors.key?(:categories) + end + + def categories_errors_describedby_id + return nil if !categories_error? + + dom_id(release_note, "categories_errors") + end + + def categories_full_messages_errors + release_note.errors.full_messages_for(:categories) + end +end diff --git a/app/views/super_admins/release_notes/_form.html.haml b/app/components/release_note/form_component/form_component.html.haml similarity index 57% rename from app/views/super_admins/release_notes/_form.html.haml rename to app/components/release_note/form_component/form_component.html.haml index b541cab81..41e225768 100644 --- a/app/views/super_admins/release_notes/_form.html.haml +++ b/app/components/release_note/form_component/form_component.html.haml @@ -1,7 +1,8 @@ = form_for [:super_admins, release_note] do |f| %fieldset#release_notes_fieldset.fr-fieldset{ 'data-controller': 'trix' } .fr-fieldset__element - = render Dsfr::InputComponent.new(form: f, attribute: :released_on, input_type: :date_field) + = render Dsfr::InputComponent.new(form: f, attribute: :released_on, input_type: :date_field) do |c| + - c.with_label { ReleaseNote.human_attribute_name(:released_on) } .fr-fieldset__element .fr-toggle @@ -10,9 +11,10 @@ Publier .fr-fieldset__element - %fieldset.fr-fieldset{ "aria-labelledby": dom_id(release_note, :category_legend) } + %fieldset.fr-fieldset{ "aria-labelledby": token_list(dom_id(release_note, "category_legend"), categories_errors_describedby_id), class: categories_fieldset_class, role: categories_error? ? "group" : nil } %legend.fr-fieldset__legend.fr-fieldset__legend--regular{ id: dom_id(release_note, :category_legend) } - Catégories + = ReleaseNote.human_attribute_name(:categories) + = render EditableChamp::AsteriskMandatoryComponent.new - ReleaseNote::CATEGORIES.each do |category| .fr-fieldset__element.fr-fieldset__element--inline @@ -21,6 +23,17 @@ %label.fr-label{ for: dom_id(release_note, "category_#{category}") } = category.humanize + - if categories_error? + .fr-messages-group{ id: "checkboxes-error-messages", aria_live: "assertive" } + - if categories_full_messages_errors.one? + %p.fr-message.fr-message--error{ id: categories_errors_describedby_id }= categories_full_messages_errors.first + - else + .fr-error-text{ id: categories_errors_describedby_id } + %ul.list-style-type-none.fr-pl-0 + - categories_full_messages_errors.map do |error_message| + %li= error_message + + .fr-fieldset__element .fr-input-group = render Dsfr::InputComponent.new(form: f, attribute: :body, input_type: :rich_text_area) diff --git a/app/views/super_admins/release_notes/add_note.turbo_stream.haml b/app/views/super_admins/release_notes/add_note.turbo_stream.haml new file mode 100644 index 000000000..4f3292a6c --- /dev/null +++ b/app/views/super_admins/release_notes/add_note.turbo_stream.haml @@ -0,0 +1 @@ += turbo_stream.append 'release_notes_fieldset', render(ReleaseNote::NoteFormComponent.new(note: @release_note)) diff --git a/app/views/super_admins/release_notes/edit.html.haml b/app/views/super_admins/release_notes/edit.html.haml index 6d156bdaf..8b5c40054 100644 --- a/app/views/super_admins/release_notes/edit.html.haml +++ b/app/views/super_admins/release_notes/edit.html.haml @@ -2,5 +2,5 @@ .fr-grid-row.fr-grid-row--center .fr-col-lg-10 %h1.fr-h2 Annonce - = render partial: 'form', object: @release_note, as: :release_note + = render ReleaseNote::FormComponent.new(release_note: @release_note) diff --git a/spec/factories/release_notes.rb b/spec/factories/release_notes.rb new file mode 100644 index 000000000..720a76753 --- /dev/null +++ b/spec/factories/release_notes.rb @@ -0,0 +1,8 @@ +FactoryBot.define do + factory :release_note do + body { "Sample release note body" } + categories { ReleaseNote::CATEGORIES.sample(1) } + published { true } + released_on { 1.day.ago.to_date } + end +end