disabled input with PJ and insert input when PJ is deleted

This commit is contained in:
Benoit Queyron 2024-07-11 17:33:52 +02:00
parent ceedbe9c45
commit 3e4aa0cde3
No known key found for this signature in database
GPG key ID: 9E723480C7317E22
3 changed files with 20 additions and 9 deletions

View file

@ -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

View file

@ -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

View file

@ -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)}"