refactor(section): update section components
This commit is contained in:
parent
283ee72d51
commit
02591da16b
7 changed files with 59 additions and 37 deletions
|
@ -5,8 +5,8 @@
|
|||
|
||||
|
||||
.repetition{ id: dom_id(@champ, :rows) }
|
||||
- @champ.rows.each do |champs|
|
||||
= render EditableChamp::RepetitionRowComponent.new(form: @form, champ: @champ, row: champs, seen_at: @seen_at)
|
||||
- @champ.row_ids.each do |row_id|
|
||||
= render EditableChamp::RepetitionRowComponent.new(form: @form, champ: @champ, row_id:, seen_at: @seen_at)
|
||||
|
||||
.actions
|
||||
= render NestedForms::OwnedButtonComponent.new(formaction: champs_repetition_path(@champ.id), http_method: :create, opt: { class: "fr-btn fr-btn--secondary fr-btn--icon-left fr-icon-add-circle-line fr-mb-3w", title: t(".add_title", libelle: @champ.libelle), id: dom_id(@champ, :create_repetition)}) do
|
||||
|
|
|
@ -1,5 +1,17 @@
|
|||
class EditableChamp::RepetitionRowComponent < ApplicationComponent
|
||||
def initialize(form:, champ:, row:, seen_at: nil)
|
||||
@form, @champ, @row, @seen_at = form, champ, row, seen_at
|
||||
def initialize(form:, champ:, row_id:, seen_at: nil)
|
||||
@form, @champ, @row_id, @seen_at = form, champ, row_id, seen_at
|
||||
|
||||
@types_de_champ = champ.dossier.revision.children_of(champ.type_de_champ)
|
||||
@champs_by_stable_id_with_row = champ.dossier.champs_by_stable_id_with_row
|
||||
@row_number = champ.row_ids.find_index(row_id)
|
||||
end
|
||||
|
||||
attr_reader :row_id, :row_number
|
||||
|
||||
private
|
||||
|
||||
def section_component
|
||||
EditableChamp::SectionComponent.new(types_de_champ: @types_de_champ, champs_by_stable_id_with_row: @champs_by_stable_id_with_row, row_id:)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,12 +1,11 @@
|
|||
- row_id = "safe-row-selector-#{@row.first.row_id}"
|
||||
.row{ id: row_id }
|
||||
- if @row.size > 1
|
||||
.row{ id: "safe-row-selector-#{row_id}" }
|
||||
- if @types_de_champ.size > 1
|
||||
%fieldset
|
||||
%legend.block-id= "#{@champ.libelle} "
|
||||
= render EditableChamp::SectionComponent.new(champs: @row)
|
||||
= render section_component
|
||||
- else
|
||||
= render EditableChamp::SectionComponent.new(champs: @row)
|
||||
= render section_component
|
||||
|
||||
.flex.row-reverse
|
||||
= render NestedForms::OwnedButtonComponent.new(formaction: champs_repetition_path(@champ.id, row_id: @row.first.row_id), http_method: :delete, opt: { class: "fr-btn fr-btn--sm fr-btn--tertiary fr-text-action-high--red-marianne", title: t(".delete_title", row_number: @champ.rows.find_index(@row))}) do
|
||||
= render NestedForms::OwnedButtonComponent.new(formaction: champs_repetition_path(@champ.id, row_id:), http_method: :delete, opt: { class: "fr-btn fr-btn--sm fr-btn--tertiary fr-text-action-high--red-marianne", title: t(".delete_title", row_number:)}) do
|
||||
= t(".delete")
|
||||
|
|
|
@ -2,8 +2,10 @@ class EditableChamp::SectionComponent < ApplicationComponent
|
|||
include ApplicationHelper
|
||||
include TreeableConcern
|
||||
|
||||
def initialize(nodes: nil, champs: nil)
|
||||
nodes ||= to_tree(champs:)
|
||||
def initialize(nodes: nil, types_de_champ: nil, row_id: nil, champs_by_stable_id_with_row:)
|
||||
nodes ||= to_tree(types_de_champ:)
|
||||
@champs_by_stable_id_with_row = champs_by_stable_id_with_row
|
||||
@row_id = row_id
|
||||
@nodes = to_fieldset(nodes:)
|
||||
end
|
||||
|
||||
|
@ -12,7 +14,8 @@ class EditableChamp::SectionComponent < ApplicationComponent
|
|||
end
|
||||
|
||||
def header_section
|
||||
@nodes.first if @nodes.first.is_a?(Champs::HeaderSectionChamp)
|
||||
node = @nodes.first
|
||||
champ_for_type_de_champ(node) if node.is_a?(TypeDeChamp) && node.header_section?
|
||||
end
|
||||
|
||||
def splitted_tail
|
||||
|
@ -35,17 +38,21 @@ class EditableChamp::SectionComponent < ApplicationComponent
|
|||
when EditableChamp::SectionComponent
|
||||
[node, nil]
|
||||
else
|
||||
[nil, node]
|
||||
[nil, champ_for_type_de_champ(node)]
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def to_fieldset(nodes:)
|
||||
nodes.map { _1.is_a?(Array) ? EditableChamp::SectionComponent.new(nodes: _1) : _1 }
|
||||
nodes.map { _1.is_a?(Array) ? EditableChamp::SectionComponent.new(nodes: _1, row_id: @row_id, champs_by_stable_id_with_row: @champs_by_stable_id_with_row) : _1 }
|
||||
end
|
||||
|
||||
def first_champ_is_an_header_section?
|
||||
header_section.present?
|
||||
end
|
||||
|
||||
def champ_for_type_de_champ(type_de_champ)
|
||||
@champs_by_stable_id_with_row[[@row_id, type_de_champ.stable_id].compact]
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue