diff --git a/app/components/attachment/edit_component.rb b/app/components/attachment/edit_component.rb index 7abb27f93..79238f284 100644 --- a/app/components/attachment/edit_component.rb +++ b/app/components/attachment/edit_component.rb @@ -4,12 +4,14 @@ class Attachment::EditComponent < ApplicationComponent attr_reader :attachment attr_reader :user_can_destroy alias user_can_destroy? user_can_destroy + attr_reader :user_can_replace + alias user_can_replace? user_can_replace attr_reader :as_multiple alias as_multiple? as_multiple 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, **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, **kwargs) @as_multiple = as_multiple @attached_file = attached_file @auto_attach_url = auto_attach_url @@ -18,6 +20,7 @@ class Attachment::EditComponent < ApplicationComponent @index = index @view_as = view_as @user_can_destroy = user_can_destroy + @user_can_replace = user_can_replace # attachment passed by kwarg because we don't want a default (nil) value. @attachment = if kwargs.key?(:attachment) @@ -64,13 +67,14 @@ class Attachment::EditComponent < ApplicationComponent def file_field_options track_issue_with_missing_validators if missing_validators? { - class: "fr-upload attachment-input #{attachment_input_class} #{persisted? ? 'hidden' : ''}", + class: class_names("fr-upload attachment-input": true, "#{attachment_input_class}": true, "hidden": persisted?, "fr-mt-2w": user_can_replace?), direct_upload: @direct_upload, id: input_id, aria: { describedby: champ&.describedby_id }, data: { auto_attach_url: }.merge(has_file_size_validator? ? { max_file_size: } : {}) + .merge(user_can_replace? ? { replace_attachment_target: "input" } : {}) }.merge(has_content_type_validator? ? { accept: accept_content_type } : {}) end @@ -103,6 +107,23 @@ class Attachment::EditComponent < ApplicationComponent } end + def replace_button_options + { + type: 'button', + data: { + action: "click->replace-attachment#open" + } + } + end + + def replace_controller_attributes + return {} unless user_can_replace? || as_multiple? + + { + "data-controller": 'replace-attachment' + } + end + def retry_button_options { type: 'button', @@ -155,7 +176,9 @@ class Attachment::EditComponent < ApplicationComponent def auto_attach_url return @auto_attach_url if @auto_attach_url.present? - return helpers.auto_attach_url(@champ) if @champ.present? + params = { replace_attachment_id: @attachment.id } if user_can_replace? && @attachment.present? + + return helpers.auto_attach_url(@champ, params) if @champ.present? nil end diff --git a/app/components/attachment/edit_component/edit_component.en.yml b/app/components/attachment/edit_component/edit_component.en.yml index 33148c1be..551e21cd3 100644 --- a/app/components/attachment/edit_component/edit_component.en.yml +++ b/app/components/attachment/edit_component/edit_component.en.yml @@ -3,6 +3,7 @@ en: max_file_size: "File size limit : %{max_file_size}." allowed_formats: "Supported formats : %{formats}" retry: Retry + replace: Replace delete: Delete errors: uploading: "An error occurred while sending the file." diff --git a/app/components/attachment/edit_component/edit_component.fr.yml b/app/components/attachment/edit_component/edit_component.fr.yml index fa4e86141..931d20741 100644 --- a/app/components/attachment/edit_component/edit_component.fr.yml +++ b/app/components/attachment/edit_component/edit_component.fr.yml @@ -4,6 +4,7 @@ fr: allowed_formats: "Formats supportés : %{formats}" retry: Réessayer delete: Supprimer + replace: Remplacer errors: uploading: "Une erreur s’est produite pendant l’envoi du fichier." virus_infected: "Virus détecté, merci d’envoyer un autre fichier." diff --git a/app/components/attachment/edit_component/edit_component.html.haml b/app/components/attachment/edit_component/edit_component.html.haml index 90ddb9512..183cf1550 100644 --- a/app/components/attachment/edit_component/edit_component.html.haml +++ b/app/components/attachment/edit_component/edit_component.html.haml @@ -1,9 +1,11 @@ -.attachment.fr-upload-group{ { id: attachment ? dom_id(attachment, :edit) : nil, class: class_names("fr-mb-2w": !(as_multiple? && downloadable?)) }.compact } +.attachment.fr-upload-group{ { id: attachment ? dom_id(attachment, :edit) : nil, class: class_names("fr-mb-2w": !(as_multiple? && downloadable?)) }.compact, **replace_controller_attributes } - if persisted? %div{ id: dom_id(attachment, :persisted_row) } .flex.flex-gap-2{ class: class_names("attachment-error": attachment.virus_scanner_error?) } - if user_can_destroy? = link_to(t('.delete'), destroy_attachment_path, **remove_button_options, class: "fr-btn fr-btn--tertiary fr-btn--sm fr-icon-delete-line", title: "Supprimer le fichier #{attachment.filename}") + - elsif user_can_replace? + = button_tag t('.replace'), **replace_button_options, class: "fr-btn fr-btn--tertiary fr-btn--sm", title: "Remplacer le fichier #{attachment.filename}" .fr-py-1v - if downloadable? diff --git a/app/components/editable_champ/piece_justificative_component.rb b/app/components/editable_champ/piece_justificative_component.rb index 9a14d389f..3d1cfaf80 100644 --- a/app/components/editable_champ/piece_justificative_component.rb +++ b/app/components/editable_champ/piece_justificative_component.rb @@ -11,6 +11,10 @@ class EditableChamp::PieceJustificativeComponent < EditableChamp::EditableChampB !@champ.mandatory? || @champ.dossier.brouillon? end + def user_can_replace? + @champ.mandatory? && @champ.dossier.en_construction? + end + def max [true, nil].include?(@champ.procedure&.piece_justificative_multiple?) ? Attachment::MultipleComponent::DEFAULT_MAX_ATTACHMENTS : 1 end diff --git a/app/components/editable_champ/piece_justificative_component/piece_justificative_component.html.haml b/app/components/editable_champ/piece_justificative_component/piece_justificative_component.html.haml index 4d549cb22..322c029aa 100644 --- a/app/components/editable_champ/piece_justificative_component/piece_justificative_component.html.haml +++ b/app/components/editable_champ/piece_justificative_component/piece_justificative_component.html.haml @@ -1,4 +1,4 @@ -= render Attachment::MultipleComponent.new(champ: @champ, attached_file: @champ.piece_justificative_file, form_object_name: @form.object_name, view_as:, user_can_destroy: user_can_destroy?, max:) do |c| += render Attachment::MultipleComponent.new(champ: @champ, attached_file: @champ.piece_justificative_file, form_object_name: @form.object_name, view_as:, user_can_destroy: user_can_destroy?, user_can_replace: user_can_replace?, max:) do |c| - if @champ.type_de_champ.piece_justificative_template&.attached? - c.with_template do = render partial: "shared/piece_justificative_template", locals: { champ: @champ } diff --git a/app/components/editable_champ/titre_identite_component.rb b/app/components/editable_champ/titre_identite_component.rb index 7bb8536d0..cde3489ad 100644 --- a/app/components/editable_champ/titre_identite_component.rb +++ b/app/components/editable_champ/titre_identite_component.rb @@ -1,2 +1,9 @@ class EditableChamp::TitreIdentiteComponent < EditableChamp::EditableChampBaseComponent + def user_can_destroy? + !@champ.mandatory? || @champ.dossier.brouillon? + end + + def user_can_replace? + @champ.mandatory? && @champ.dossier.en_construction? + end end diff --git a/app/components/editable_champ/titre_identite_component/titre_identite_component.html.haml b/app/components/editable_champ/titre_identite_component/titre_identite_component.html.haml index 1a56c074d..778ce957c 100644 --- a/app/components/editable_champ/titre_identite_component/titre_identite_component.html.haml +++ b/app/components/editable_champ/titre_identite_component/titre_identite_component.html.haml @@ -1,5 +1,5 @@ -- user_can_destroy = !@champ.mandatory? || @champ.dossier.brouillon? - if @champ.type_de_champ.piece_justificative_template&.attached? = render partial: "shared/piece_justificative_template", locals: { champ: @champ } -= render Attachment::EditComponent.new(champ: @form.object, attached_file: @champ.piece_justificative_file, attachment: @champ.piece_justificative_file[0], form_object_name: @form.object_name, user_can_destroy:) += render Attachment::EditComponent.new(champ: @form.object, attached_file: @champ.piece_justificative_file, attachment: @champ.piece_justificative_file[0], form_object_name: @form.object_name, + user_can_destroy: user_can_destroy?, user_can_replace: user_can_replace?) diff --git a/app/controllers/champs/piece_justificative_controller.rb b/app/controllers/champs/piece_justificative_controller.rb index f95968b62..92e776370 100644 --- a/app/controllers/champs/piece_justificative_controller.rb +++ b/app/controllers/champs/piece_justificative_controller.rb @@ -28,9 +28,22 @@ class Champs::PieceJustificativeController < ApplicationController end def attach_piece_justificative - @champ.piece_justificative_file.attach(params[:blob_signed_id]) - save_succeed = @champ.save + save_succeed = nil + + ActiveStorage::Attachment.transaction do + if params.key?(:replace_attachment_id) + @champ.piece_justificative_file.attachments.find do + _1.id == params[:replace_attachment_id].to_i + end&.destroy + end + + @champ.piece_justificative_file.attach(params[:blob_signed_id]) + + save_succeed = @champ.save + end + @champ.dossier.update(last_champ_updated_at: Time.zone.now.utc) if save_succeed + save_succeed end diff --git a/app/helpers/champ_helper.rb b/app/helpers/champ_helper.rb index 4368730c7..2e7971a85 100644 --- a/app/helpers/champ_helper.rb +++ b/app/helpers/champ_helper.rb @@ -7,11 +7,11 @@ module ChampHelper simple_format(auto_linked_text, {}, sanitize: false) end - def auto_attach_url(object) + def auto_attach_url(object, params = {}) if object.is_a?(Champ) - champs_attach_piece_justificative_url(object.id) + champs_attach_piece_justificative_url(object.id, params) elsif object.is_a?(TypeDeChamp) - piece_justificative_template_admin_procedure_type_de_champ_url(stable_id: object.stable_id, procedure_id: object.procedure.id) + piece_justificative_template_admin_procedure_type_de_champ_url(stable_id: object.stable_id, procedure_id: object.procedure.id, **params) end end end diff --git a/app/javascript/controllers/replace_attachment_controller.ts b/app/javascript/controllers/replace_attachment_controller.ts new file mode 100644 index 000000000..701df521d --- /dev/null +++ b/app/javascript/controllers/replace_attachment_controller.ts @@ -0,0 +1,13 @@ +import { ApplicationController } from './application_controller'; +import { show } from '@utils'; + +export class ReplaceAttachmentController extends ApplicationController { + static targets = ['input']; + + declare readonly inputTarget: HTMLInputElement; + + open() { + show(this.inputTarget); + this.inputTarget.click(); // opens input prompt + } +} diff --git a/spec/controllers/champs/piece_justificative_controller_spec.rb b/spec/controllers/champs/piece_justificative_controller_spec.rb index 6deb1e060..ac09d58d6 100644 --- a/spec/controllers/champs/piece_justificative_controller_spec.rb +++ b/spec/controllers/champs/piece_justificative_controller_spec.rb @@ -5,6 +5,8 @@ describe Champs::PieceJustificativeController, type: :controller do let(:champ) { dossier.champs_public.first } describe '#update' do + let(:replace_attachment_id) { nil } + render_views before { sign_in user } @@ -12,8 +14,9 @@ describe Champs::PieceJustificativeController, type: :controller do put :update, params: { position: '1', champ_id: champ.id, - blob_signed_id: file - }, format: :turbo_stream + blob_signed_id: file, + replace_attachment_id: + }.compact, format: :turbo_stream end context 'when the file is valid' do @@ -65,6 +68,33 @@ describe Champs::PieceJustificativeController, type: :controller do expect(JSON.parse(response.body)).to eq({ 'errors' => ['La pièce justificative n’est pas d’un type accepté'] }) end end + + context 'replace an attachment' do + let(:file) { fixture_file_upload('spec/fixtures/files/piece_justificative_0.pdf', 'application/pdf') } + before { subject } + + context "attachment associated to dossier" do + let(:champ) { create(:champ, :with_piece_justificative_file, dossier:) } + let(:replace_attachment_id) { champ.piece_justificative_file.first.id } + + it "replaces an existing attachment" do + champ.reload + + expect(champ.piece_justificative_file.attachments.count).to eq(1) + expect(champ.piece_justificative_file.attachments.first.filename).to eq("piece_justificative_0.pdf") + end + end + + context "attachment not associated to dossier" do + let(:other_champ) { create(:champ, :with_piece_justificative_file) } + let(:replace_attachment_id) { other_champ.piece_justificative_file.first.id } + + it "add attachment, don't replace attachment" do + expect(champ.reload.piece_justificative_file.attachments.count).to eq(1) + expect(other_champ.reload.piece_justificative_file.attachments.count).to eq(1) + end + end + end end describe '#template' do