chore(conditions): champs_conditions and routing_rule components inherit from conditions_component

This commit is contained in:
Eric Leroy-Terquem 2023-10-13 11:30:22 +02:00
parent c995a06434
commit c8396f92f5
9 changed files with 127 additions and 69 deletions

View file

@ -0,0 +1,44 @@
class Conditions::ChampsConditionsComponent < Conditions::ConditionsComponent
def initialize(tdc:, upper_tdcs:, procedure_id:)
@tdc, @condition, @tdcs = tdc, tdc.condition, upper_tdcs
@procedure_id = procedure_id
end
private
def logic_conditionnel_button
html_class = 'fr-btn fr-btn--tertiary fr-btn--sm'
if @condition.nil?
submit_tag(
t('.enable_conditionnel'),
formaction: add_condition_path,
class: html_class
)
else
submit_tag(
t('.disable_conditionnel'),
formmethod: 'delete',
formnovalidate: true,
data: { confirm: t('.disable_conditionnel_alert') },
class: html_class
)
end
end
def add_condition_path
add_row_admin_procedure_condition_path(@procedure_id, @tdc.stable_id)
end
def delete_condition_path(row_index)
delete_row_admin_procedure_condition_path(@procedure_id, @tdc.stable_id, row_index: row_index)
end
def input_id_for(name, row_index)
"#{@tdc.stable_id}-#{name}-#{row_index}"
end
def input_prefix
'type_de_champ[condition_form]'
end
end

View file

@ -6,7 +6,7 @@
Logique conditionnelle
= logic_conditionnel_button
= render TypesDeChampEditor::ConditionsErrorsComponent.new(conditions: condition_per_row, upper_tdcs: @upper_tdcs)
= render TypesDeChampEditor::ConditionsErrorsComponent.new(conditions: condition_per_row, upper_tdcs: @tdcs)
- if @condition.present?
%table.condition-table.mt-2.width-100

View file

@ -1,11 +1,6 @@
class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
class Conditions::ConditionsComponent < ApplicationComponent
include Logic
def initialize(tdc:, upper_tdcs:, procedure_id:)
@tdc, @condition, @upper_tdcs = tdc, tdc.condition, upper_tdcs
@procedure_id = procedure_id
end
private
def rows
@ -20,26 +15,6 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
end
end
def logic_conditionnel_button
html_class = 'fr-btn fr-btn--tertiary fr-btn--sm'
if @condition.nil?
submit_tag(
t('.enable_conditionnel'),
formaction: add_row_admin_procedure_condition_path(@procedure_id, @tdc.stable_id),
class: html_class
)
else
submit_tag(
t('.disable_conditionnel'),
formmethod: 'delete',
formnovalidate: true,
data: { confirm: t('.disable_conditionnel_alert') },
class: html_class
)
end
end
def far_left_tag(row_number)
if row_number == 0
t('.display_if')
@ -85,7 +60,7 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
end
def available_targets_for_select
@upper_tdcs
@tdcs
.filter { |tdc| ChampValue::MANAGED_TYPE_DE_CHAMP.values.include?(tdc.type_champ) }
.map { |tdc| [tdc.libelle, champ_value(tdc.stable_id).to_json] }
end
@ -108,7 +83,7 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
end
def compatibles_operators_for_select(left)
case left.type(@upper_tdcs)
case left.type(@tdcs)
when ChampValue::CHAMP_VALUE_TYPE.fetch(:boolean)
[
[t('is', scope: 'logic'), Eq.name]
@ -138,7 +113,7 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
def right_operand_tag(left, right, row_index)
right_invalid = !current_right_valid?(left, right)
case left.type(@upper_tdcs)
case left.type(@tdcs)
when :boolean
booleans_for_select = [[t('utils.yes'), constant(true).to_json], [t('utils.no'), constant(false).to_json]]
@ -160,7 +135,7 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
class: 'fr-select'
)
when :enum, :enums
enums_for_select = left.options(@upper_tdcs)
enums_for_select = left.options(@tdcs)
if right_invalid
enums_for_select = empty_target_for_select + enums_for_select
@ -186,13 +161,13 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
end
def current_right_valid?(left, right)
Logic.compatible_type?(left, right, @upper_tdcs)
Logic.compatible_type?(left, right, @tdcs)
end
def add_condition_tag
tag.button(
t('.add_condition'),
formaction: add_row_admin_procedure_condition_path(@procedure_id, @tdc.stable_id),
formaction: add_condition_path,
formnovalidate: true,
class: 'fr-btn fr-btn--secondary fr-btn--sm fr-icon-add-circle-line fr-btn--icon-left'
)
@ -201,7 +176,7 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
def delete_condition_tag(row_index)
tag.button(
tag.span('', class: 'icon delete') + tag.span(t('.remove_a_row'), class: 'sr-only'),
formaction: delete_row_admin_procedure_condition_path(@procedure_id, @tdc.stable_id, row_index: row_index),
formaction: delete_condition_path(row_index),
formmethod: 'delete',
formnovalidate: true
)
@ -214,12 +189,4 @@ class TypesDeChampEditor::ConditionsComponent < ApplicationComponent
def input_name_for(name)
"#{input_prefix}[rows][][#{name}]"
end
def input_id_for(name, row_index)
"#{@tdc.stable_id}-#{name}-#{row_index}"
end
def input_prefix
'type_de_champ[condition_form]'
end
end

View file

@ -0,0 +1,26 @@
class Conditions::RoutingRulesComponent < Conditions::ConditionsComponent
def initialize(groupe_instructeur:)
@groupe_instructeur = groupe_instructeur
@condition = groupe_instructeur.routing_rule
@procedure_id = groupe_instructeur.procedure_id
@tdcs = groupe_instructeur.procedure.active_revision.types_de_champ
end
private
def add_condition_path
add_row_admin_procedure_routing_rule_path(@procedure_id, @groupe_instructeur.id)
end
def delete_condition_path(row_index)
delete_row_admin_procedure_routing_rule_path(@procedure_id, @groupe_instructeur.id, row_index: row_index)
end
def input_id_for(name, row_index)
"#{@groupe_instructeur.id}-#{name}-#{row_index}"
end
def input_prefix
'groupe_instructeur[condition_form]'
end
end

View file

@ -0,0 +1,9 @@
---
fr:
display_if: Router si
enable_conditionnel: cliquer pour activer
disable_conditionnel: cliquer pour désactiver
disable_conditionnel_alert: "La logique conditionnelle appliquée à ce champ sera désactivée.\nVoulez-vous continuer ?"
select: Sélectionner
add_condition: Ajouter une règle de routage
remove_a_row: Supprimer la ligne

View file

@ -0,0 +1,32 @@
.flex.justify-start.section{ id: dom_id(@groupe_instructeur, :routing_rule) }
= form_tag admin_procedure_routing_rule_path(@procedure_id, @groupe_instructeur.id),
method: :patch,
data: { turbo: true, controller: 'autosave' },
class: 'form width-100' do
.conditionnel.mt-2.width-100
%table.condition-table.mt-2
%thead
%tr
%th.far-left
%th.target Champ Cible
%th.operator Opérateur
%th.value Valeur
%th.delete-column
%tbody
- if rows.blank?
%tr
%td.far-left= far_left_tag(0)
%td.target= left_operand_tag(empty, 0)
%td.operator= operator_tag(EmptyOperator.name, empty, 0)
%td.value= right_operand_tag(empty, empty, 0)
%td.delete-column= delete_condition_tag(0)
- rows.each.with_index do |(targeted_champ, operator_name, value), row_index|
%tr
%td.far-left= far_left_tag(row_index)
%td.target= left_operand_tag(targeted_champ, row_index)
%td.operator= operator_tag(operator_name, targeted_champ, row_index)
%td.value= right_operand_tag(targeted_champ, value, row_index)
%td.delete-column= delete_condition_tag(row_index)
.flex.justify-end.mt-2= add_condition_tag

View file

@ -22,34 +22,14 @@
Groupe inactif
%span.fr-hint-text Si cette option est activée, les usagers ne pourront plus sélectionner ce groupe dinstructeurs
= form_tag admin_procedure_routing_rules_path(@procedure),
method: :post,
data: { controller: 'autosave' },
class: 'fr-mb-3w' do
= render Conditions::RoutingRulesComponent.new(groupe_instructeur: @groupe_instructeur)
= hidden_field_tag('groupe_instructeur_id', @groupe_instructeur.id)
.fr-hint-text.mt-2.mb-2
%span Si vous ne trouvez pas l'option correspondant à votre groupe, veuillez l'ajouter dans le
%span
= link_to 'champ concerné', champs_admin_procedure_path(@procedure)
.flex
%p.fr-mb-1w.fr-mr-2w Routage
- if @groupe_instructeur.invalid_rule?
%p.fr-mb-1w.fr-badge.fr-badge--warning.fr-badge--sm règle invalide
- elsif @groupe_instructeur.non_unique_rule?
%p.fr-mb-1w.fr-badge.fr-badge--warning.fr-badge--sm règle déjà attribuée à #{@groupe_instructeur.groups_with_same_rule}
.flex.align-baseline.fr-mb-1w
.fr-mr-2w.no-wrap si le champ
.target.fr-mr-2w
= targeted_champ_tag
.operator.fr-mr-2w.no-wrap
= operator_tag
.value
= value_tag
.fr-hint-text
%span Si vous ne trouvez pas l'option correspondant à votre groupe, veuillez l'ajouter dans le champ dédié au
%span
= link_to 'routage', champs_admin_procedure_path(@procedure)
.flex.fr-btns-group--sm.fr-btns-group--inline.fr-btns-group--icon-right
.flex.fr-btns-group--sm.fr-btns-group--inline.fr-btns-group--icon-right.mb-2
- if @groupe_instructeur.can_delete?
%p= t('.delete')
= button_to admin_procedure_groupe_instructeur_path(@procedure, @groupe_instructeur),

View file

@ -116,7 +116,7 @@
.type-de-champ-add-button{ id: dom_id(coordinate, :type_de_champ_add_button), class: class_names(hidden: !coordinate.empty?) }
= render TypesDeChampEditor::AddChampButtonComponent.new(revision: coordinate.revision, parent: coordinate, is_annotation: coordinate.private?)
= render(TypesDeChampEditor::ConditionsComponent.new(tdc: type_de_champ, upper_tdcs: @upper_coordinates.map(&:type_de_champ), procedure_id: procedure.id))
= render(Conditions::ChampsConditionsComponent.new(tdc: type_de_champ, upper_tdcs: @upper_coordinates.map(&:type_de_champ), procedure_id: procedure.id))
.type-de-champ-add-button{ class: class_names(root: !coordinate.child?) }
= render TypesDeChampEditor::AddChampButtonComponent.new(revision: coordinate.revision, parent: coordinate&.parent, is_annotation: coordinate.private?, after_stable_id: type_de_champ.stable_id)