demarches-normaliennes/app/components/editable_champ/editable_champ_component.rb

125 lines
2.7 KiB
Ruby
Raw Normal View History

2022-08-12 10:45:04 +02:00
class EditableChamp::EditableChampComponent < ApplicationComponent
2023-05-05 09:13:05 +02:00
include Dsfr::InputErrorable
def initialize(form:, champ:, seen_at: nil)
@form, @champ, @seen_at = form, champ, seen_at
2023-05-05 09:13:05 +02:00
@attribute = :value
end
private
def has_label?(champ)
types_without_label = [
TypeDeChamp.type_champs.fetch(:header_section),
TypeDeChamp.type_champs.fetch(:explication),
TypeDeChamp.type_champs.fetch(:repetition),
TypeDeChamp.type_champs.fetch(:linked_drop_down_list)
]
!types_without_label.include?(@champ.type_champ)
end
def component_class
"EditableChamp::#{@champ.type_champ.camelcase}Component".constantize
end
2023-06-28 12:14:04 +02:00
def select_group
[
'departements',
'drop_down_list',
'epci',
'pays',
'regions'
]
end
def input_group
[
'annuaire_education',
'date',
'datetime',
'decimal_number',
'dgfip',
'dossier_link',
'email',
'iban',
'integer_number',
'mesri',
'number',
'phone',
'piece_justificative',
'pole_emploi',
'rna',
'siret',
'text',
'textarea',
'titre_identite'
]
end
2023-07-03 11:16:49 +02:00
def radio_group
[
'boolean',
'yes_no'
]
end
def html_options
{
class: class_names(
2023-05-05 09:13:05 +02:00
{
'editable-champ': true,
"editable-champ-#{@champ.type_champ}": true,
"hidden": !@champ.visible?,
2023-06-28 12:14:04 +02:00
"fr-input-group": input_group.include?(@champ.type_champ),
2023-07-03 11:16:49 +02:00
"fr-select-group": select_group.include?(@champ.type_champ),
"fr-radio-group": radio_group.include?(@champ.type_champ),
"fr-fieldset": @champ.legend_label?
2023-05-05 09:13:05 +02:00
}.merge(input_group_error_class_names)
),
id: @champ.input_group_id,
2023-05-30 11:39:53 +02:00
data: { controller: stimulus_controller, **data_dependent_conditions, **stimulus_values }
}
end
2023-05-30 11:39:53 +02:00
def stimulus_values
if @champ.fetch_external_data_pending?
{ turbo_poll_url_value: }
else
{}
end
end
def turbo_poll_url_value
if @champ.private?
annotation_instructeur_dossier_path(@champ.dossier.procedure, @champ.dossier, @champ)
else
champ_dossier_path(@champ.dossier, @champ)
end
end
def stimulus_controller
2023-06-06 15:54:03 +02:00
if autosave_enabled?
# This is an editable champ. Lets find what controllers it might need.
2023-03-01 18:30:10 +01:00
controllers = ['autosave']
2023-05-30 11:39:53 +02:00
if @champ.fetch_external_data_pending?
controllers << 'turbo-poll'
end
controllers.join(' ')
end
2022-08-12 10:45:04 +02:00
end
def data_dependent_conditions
if @champ.dependent_conditions?
{ "dependent-conditions": "true" }
else
{}
end
end
2023-06-06 15:54:03 +02:00
def autosave_enabled?
!@champ.carte? && !@champ.block? && @champ.fillable?
end
2022-08-12 10:45:04 +02:00
end