From 3e4aa0cde3063b9b9e00766f4a40b4e21c4b2a80 Mon Sep 17 00:00:00 2001 From: Benoit Queyron <72251526+Benoit-MINT@users.noreply.github.com> Date: Thu, 11 Jul 2024 17:33:52 +0200 Subject: [PATCH] disabled input with PJ and insert input when PJ is deleted --- app/components/attachment/edit_component.rb | 7 +++---- app/controllers/attachments_controller.rb | 14 +++++++++++++- app/views/attachments/destroy.turbo_stream.haml | 8 ++++---- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/app/components/attachment/edit_component.rb b/app/components/attachment/edit_component.rb index 96a5a8ea4..5ca264507 100644 --- a/app/components/attachment/edit_component.rb +++ b/app/components/attachment/edit_component.rb @@ -28,7 +28,6 @@ class Attachment::EditComponent < ApplicationComponent # Utilisation du premier attachement comme référence pour la rétrocompatibilité @attachment = @attachments.first - # When parent form has nested attributes, pass the form builder object_name # to correctly infer the input attribute name. @form_object_name = kwargs.delete(:form_object_name) @@ -55,7 +54,7 @@ class Attachment::EditComponent < ApplicationComponent end def destroy_attachment_path - attachment_path(dossier_id: champ&.dossier_id, stable_id: champ&.stable_id, row_id: champ&.row_id) + attachment_path(dossier_id: champ&.dossier_id, stable_id: champ&.stable_id, row_id: champ&.row_id, auto_attach_url: @auto_attach_url, view_as: @view_as) end def attachment_input_class @@ -66,7 +65,7 @@ class Attachment::EditComponent < ApplicationComponent track_issue_with_missing_validators if missing_validators? options = { - class: class_names("fr-upload attachment-input": true, "#{attachment_input_class}": true, "hidden": persisted?), + class: class_names("fr-upload attachment-input": true, "#{attachment_input_class}": true), direct_upload: @direct_upload, id: input_id, aria: { describedby: champ&.describedby_id }, @@ -78,7 +77,7 @@ class Attachment::EditComponent < ApplicationComponent options.merge!(has_content_type_validator? ? { accept: accept_content_type } : {}) options[:multiple] = true if as_multiple? - options[:disabled] = true if @max && @index >= @max + options[:disabled] = true if (@max && @index >= @max) || persisted? options end diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb index 721ec2cc2..17d5515b2 100644 --- a/app/controllers/attachments_controller.rb +++ b/app/controllers/attachments_controller.rb @@ -21,7 +21,11 @@ class AttachmentsController < ApplicationController @attachment.purge_later flash.notice = 'La pièce jointe a bien été supprimée.' - @champ = find_champ if params[:dossier_id] + if params[:dossier_id] + @champ = find_champ + else + @attachment_options = attachment_options + end respond_to do |format| format.turbo_stream @@ -35,4 +39,12 @@ class AttachmentsController < ApplicationController dossier = policy_scope(Dossier).includes(:champs).find(params[:dossier_id]) dossier.champs.find_by(stable_id: params[:stable_id], row_id: params[:row_id]) end + + def attachment_options + { + attached_file: @attachment.record.public_send(@attachment.name), + auto_attach_url: params[:auto_attach_url], + view_as: params[:view_as]&.to_sym + } + end end diff --git a/app/views/attachments/destroy.turbo_stream.haml b/app/views/attachments/destroy.turbo_stream.haml index e66582528..f17dcaabe 100644 --- a/app/views/attachments/destroy.turbo_stream.haml +++ b/app/views/attachments/destroy.turbo_stream.haml @@ -1,9 +1,9 @@ -= turbo_stream.remove dom_id(@attachment, :persisted_row) - -= turbo_stream.show_all ".attachment-input-#{@attachment.id}" - - if @champ = fields_for @champ.input_name, @champ do |form| = turbo_stream.replace @champ.input_group_id do = render EditableChamp::EditableChampComponent.new champ: @champ, form: form = turbo_stream.focus_all "#attachment-multiple-empty-#{@champ.public_id} input" +- else + = turbo_stream.replace dom_id(@attachment, :edit) do + = render Attachment::EditComponent.new(**@attachment_options) + = turbo_stream.focus_all "##{dom_id(@attachment.record, @attachment.name)}"