Merge pull request #10511 from demarches-simplifiees/bug-PJ-multiples-desactivees
ETQ usager: je ne peux ajouter qu'une unique PJ pour les anciennes procédures qui l'exigent
This commit is contained in:
commit
76a8e71fd5
6 changed files with 23 additions and 15 deletions
|
@ -10,7 +10,7 @@ class Attachment::EditComponent < ApplicationComponent
|
|||
|
||||
EXTENSIONS_ORDER = ['jpeg', 'png', 'pdf', 'zip'].freeze
|
||||
|
||||
def initialize(champ: nil, auto_attach_url: nil, attached_file:, direct_upload: true, index: 0, as_multiple: false, view_as: :link, user_can_destroy: true, user_can_replace: false, attachments: [], **kwargs)
|
||||
def initialize(champ: nil, auto_attach_url: nil, attached_file:, direct_upload: true, index: 0, as_multiple: false, view_as: :link, user_can_destroy: true, user_can_replace: false, attachments: [], max: nil, **kwargs)
|
||||
@champ = champ
|
||||
@attached_file = attached_file
|
||||
@direct_upload = direct_upload
|
||||
|
@ -24,6 +24,7 @@ class Attachment::EditComponent < ApplicationComponent
|
|||
@attachments = attachments.presence || (kwargs.key?(:attachment) ? [kwargs.delete(:attachment)] : [])
|
||||
@attachments << attached_file.attachment if attached_file.respond_to?(:attachment) && @attachments.empty?
|
||||
@attachments.compact!
|
||||
@max = max
|
||||
|
||||
# Utilisation du premier attachement comme référence pour la rétrocompatibilité
|
||||
@attachment = @attachments.first
|
||||
|
@ -54,7 +55,7 @@ class Attachment::EditComponent < ApplicationComponent
|
|||
end
|
||||
|
||||
def destroy_attachment_path
|
||||
attachment_path(champ_id: champ&.public_id)
|
||||
attachment_path(dossier_id: champ&.dossier_id, stable_id: champ&.stable_id, row_id: champ&.row_id)
|
||||
end
|
||||
|
||||
def attachment_input_class
|
||||
|
@ -63,6 +64,7 @@ class Attachment::EditComponent < ApplicationComponent
|
|||
|
||||
def file_field_options
|
||||
track_issue_with_missing_validators if missing_validators?
|
||||
|
||||
options = {
|
||||
class: class_names("fr-upload attachment-input": true, "#{attachment_input_class}": true, "hidden": persisted?),
|
||||
direct_upload: @direct_upload,
|
||||
|
@ -76,6 +78,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
|
||||
end
|
||||
|
|
|
@ -30,10 +30,6 @@ class Attachment::MultipleComponent < ApplicationComponent
|
|||
@attachments.each_with_index(&block)
|
||||
end
|
||||
|
||||
def can_attach_next?
|
||||
@attachments.count < @max
|
||||
end
|
||||
|
||||
def empty_component_id
|
||||
champ.present? ? "attachment-multiple-empty-#{champ.public_id}" : "attachment-multiple-empty-generic"
|
||||
end
|
||||
|
|
|
@ -7,8 +7,8 @@
|
|||
%li{ id: dom_id(attachment) }
|
||||
= render Attachment::EditComponent.new(champ:, attached_file:, attachment:, index:, view_as:, user_can_destroy:, form_object_name:)
|
||||
|
||||
%div{ id: empty_component_id, class: class_names("hidden": !can_attach_next?), data: { turbo_force: :server } }
|
||||
= render Attachment::EditComponent.new(champ:, as_multiple: champ.nil?, attached_file:, attachment: nil, index: attachments_count, user_can_destroy:, form_object_name:)
|
||||
%div{ id: empty_component_id, data: { turbo_force: :server } }
|
||||
= render Attachment::EditComponent.new(champ:, as_multiple: champ.nil?, attached_file:, attachment: nil, index: attachments_count, user_can_destroy:, form_object_name:, max: @max)
|
||||
|
||||
// single poll and refresh message for all attachments
|
||||
= render Attachment::PendingPollComponent.new(attachments: attachments, poll_url:, context: poll_context)
|
||||
|
|
|
@ -21,11 +21,18 @@ class AttachmentsController < ApplicationController
|
|||
@attachment.purge_later
|
||||
flash.notice = 'La pièce jointe a bien été supprimée.'
|
||||
|
||||
@champ_id = params[:champ_id]
|
||||
@champ = find_champ if params[:dossier_id]
|
||||
|
||||
respond_to do |format|
|
||||
format.turbo_stream
|
||||
format.html { redirect_back(fallback_location: root_url) }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def find_champ
|
||||
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
|
||||
end
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
= turbo_stream.remove dom_id(@attachment, :persisted_row)
|
||||
|
||||
- if @champ_id
|
||||
= turbo_stream.show "attachment-multiple-empty-#{@champ_id}"
|
||||
= turbo_stream.focus_all "#attachment-multiple-empty-#{@champ_id} input"
|
||||
|
||||
= 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"
|
||||
|
|
|
@ -75,8 +75,8 @@ RSpec.describe Attachment::MultipleComponent, type: :component do
|
|||
context 'max attachments' do
|
||||
let(:kwargs) { { max: 1 } }
|
||||
|
||||
it 'does not render visible input file where max attachments has been reached' do
|
||||
expect(subject).to have_selector('.hidden input[type=file]')
|
||||
it 'renders a disabled input file where max attachments has been reached' do
|
||||
expect(subject).to have_selector('input[type=file][disabled]')
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue