refactor(announces): note form as view component

This commit is contained in:
Colin Darie 2023-10-11 19:06:25 +02:00
parent 8587696f33
commit ad671092c0
No known key found for this signature in database
GPG key ID: 8C76CADD40253590
5 changed files with 57 additions and 4 deletions

View file

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

View file

@ -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)

View file

@ -0,0 +1 @@
= turbo_stream.append 'release_notes_fieldset', render(ReleaseNote::NoteFormComponent.new(note: @release_note))

View file

@ -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)

View file

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