diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index 4d6d6821b..72e2fb8f3 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -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 diff --git a/app/models/champ.rb b/app/models/champ.rb index ec769dac8..d2127d27a 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -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 diff --git a/app/views/shared/dossiers/editable_champs/_editable_champ.html.haml b/app/views/shared/dossiers/editable_champs/_editable_champ.html.haml index d33042f15..534e654b9 100644 --- a/app/views/shared/dossiers/editable_champs/_editable_champ.html.haml +++ b/app/views/shared/dossiers/editable_champs/_editable_champ.html.haml @@ -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? diff --git a/app/views/users/dossiers/update_brouillon.turbo_stream.haml b/app/views/users/dossiers/update_brouillon.turbo_stream.haml new file mode 100644 index 000000000..59eb62452 --- /dev/null +++ b/app/views/users/dossiers/update_brouillon.turbo_stream.haml @@ -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 diff --git a/spec/system/users/brouillon_spec.rb b/spec/system/users/brouillon_spec.rb index aeb3ecc25..da99b2deb 100644 --- a/spec/system/users/brouillon_spec.rb +++ b/spec/system/users/brouillon_spec.rb @@ -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)