feat(dossier): toggle conditional champs visibility

This commit is contained in:
Paul Chavard 2022-06-23 12:12:17 +02:00
parent 5b19aa9fe5
commit 120b593015
5 changed files with 45 additions and 2 deletions

View file

@ -172,7 +172,7 @@ module Users
respond_to do |format|
format.html { render :brouillon }
format.turbo_stream
format.turbo_stream { render layout: false }
end
end

View file

@ -180,8 +180,24 @@ class Champ < ApplicationRecord
raise NotImplemented.new(:fetch_external_data)
end
def conditional?
type_de_champ.condition.present?
end
def visible?
if conditional?
type_de_champ.condition.compute(champs_for_condition)
else
true
end
end
private
def champs_for_condition
private? ? dossier.champs_private : dossier.champs
end
def html_id
"#{stable_id}-#{id}"
end

View file

@ -1,4 +1,4 @@
.editable-champ{ class: "editable-champ-#{champ.type_champ}", id: champ.input_group_id, data: autosave_available?(champ) ? { controller: 'autosave' } : {} }
.editable-champ{ class: "editable-champ-#{champ.type_champ} #{champ.visible? ? '' : 'hidden'}", id: champ.input_group_id, data: autosave_available?(champ) ? { controller: 'autosave' } : {} }
- if champ.repetition?
%h3.header-subsection= champ.libelle
- if champ.description.present?

View file

@ -0,0 +1,5 @@
- @dossier.champs.filter(&:conditional?).each do |champ|
- if champ.visible?
= turbo_stream.show champ.input_group_id
- else
= turbo_stream.hide champ.input_group_id

View file

@ -271,6 +271,28 @@ describe 'The user' do
expect(page).to have_text('file.pdf')
end
context 'with condition' do
include Logic
let(:procedure) { create(:procedure, :published, :for_individual, :with_yes_no, :with_type_de_champ) }
let(:revision) { procedure.published_revision }
let(:yes_no_type_de_champ) { revision.types_de_champ.first }
let(:type_de_champ) { revision.types_de_champ.last }
let(:condition) { ds_eq(champ_value(yes_no_type_de_champ.stable_id), constant(true)) }
before { type_de_champ.update(condition: condition) }
scenario 'fill a dossier', js: true do
log_in(user, procedure)
fill_individual
expect(page).to have_field(type_de_champ.libelle, with: '', visible: false)
choose('Oui')
expect(page).to have_field(type_de_champ.libelle, with: '', visible: true)
end
end
context 'draft autosave' do
scenario 'autosave a draft', js: true do
log_in(user, simple_procedure)