diff --git a/app/components/types_de_champ_editor/champ_component.rb b/app/components/types_de_champ_editor/champ_component.rb index 856d0d06a..d21c17a9a 100644 --- a/app/components/types_de_champ_editor/champ_component.rb +++ b/app/components/types_de_champ_editor/champ_component.rb @@ -60,6 +60,10 @@ class TypesDeChampEditor::ChampComponent < ApplicationComponent TypeDeChamp.type_champs .keys + # FIXME + # We can only refresh after update champs when autosave is enabled. And it is disabled for now in private forms. + # So for new we restrict champs that require refresh after update to public forms. + .filter { type_de_champ.public? || !TypeDeChamp.refresh_after_update?(_1) } .filter(&method(:filter_type_champ)) .filter(&method(:filter_featured_type_champ)) .filter(&method(:filter_block_type_champ)) diff --git a/app/controllers/users/dossiers_controller.rb b/app/controllers/users/dossiers_controller.rb index bb0b63b0b..c1e5ed2ed 100644 --- a/app/controllers/users/dossiers_controller.rb +++ b/app/controllers/users/dossiers_controller.rb @@ -201,10 +201,7 @@ module Users respond_to do |format| format.html { render :brouillon } format.turbo_stream do - @to_shows, @to_hides = @dossier.champs_public_all - .filter(&:conditional?) - .partition(&:visible?) - .map { |champs| champs_to_one_selector(champs) } + @to_show, @to_hide, @to_update = champs_to_turbo_update render(:update, layout: false) end @@ -222,10 +219,7 @@ module Users respond_to do |format| format.html { render :modifier } format.turbo_stream do - @to_shows, @to_hides = @dossier.champs_public_all - .filter(&:conditional?) - .partition(&:visible?) - .map { |champs| champs_to_one_selector(champs) } + @to_show, @to_hide, @to_update = champs_to_turbo_update end end end @@ -493,6 +487,24 @@ module Users errors end + def champs_to_turbo_update + champ_ids = champs_public_params + .fetch(:champs_public_all_attributes) + .keys + .map(&:to_i) + + to_update = dossier + .champs_public_all + .filter { _1.id.in?(champ_ids) && _1.refresh_after_update? } + to_show, to_hide = dossier + .champs_public_all + .filter(&:conditional?) + .partition(&:visible?) + .map { champs_to_one_selector(_1 - to_update) } + + return to_show, to_hide, to_update + end + def ensure_ownership! if !current_user.owns?(dossier) forbidden! diff --git a/app/models/champ.rb b/app/models/champ.rb index 7f9d02e08..fcfd84863 100644 --- a/app/models/champ.rb +++ b/app/models/champ.rb @@ -67,6 +67,7 @@ class Champ < ApplicationRecord :stable_id, :mandatory?, :prefillable?, + :refresh_after_update?, to: :type_de_champ scope :updated_since?, -> (date) { where('champs.updated_at > ?', date) } diff --git a/app/models/type_de_champ.rb b/app/models/type_de_champ.rb index 8a8e1c036..8b876f400 100644 --- a/app/models/type_de_champ.rb +++ b/app/models/type_de_champ.rb @@ -469,6 +469,14 @@ class TypeDeChamp < ApplicationRecord model_name: OpenStruct.new(param_key: model_name.param_key)) end + def refresh_after_update? + self.class.refresh_after_update?(type_champ) + end + + def self.refresh_after_update?(_type_champ) + false + end + private DEFAULT_EMPTY = [''] diff --git a/app/views/users/dossiers/update.turbo_stream.haml b/app/views/users/dossiers/update.turbo_stream.haml index 96f5c3b06..9a9d81f3e 100644 --- a/app/views/users/dossiers/update.turbo_stream.haml +++ b/app/views/users/dossiers/update.turbo_stream.haml @@ -1,4 +1,8 @@ -- if @to_shows.present? - = turbo_stream.show_all(@to_shows) -- if @to_hides.present? - = turbo_stream.hide_all(@to_hides) +- if @to_show.present? + = turbo_stream.show_all(@to_show) +- if @to_hide.present? + = turbo_stream.hide_all(@to_hide) +- @to_update.each do |champ| + = fields_for champ.input_name, champ do |form| + = turbo_stream.morph champ.input_group_id do + = render EditableChamp::EditableChampComponent.new champ:, form: