diff --git a/app/components/attachment/edit_component.rb b/app/components/attachment/edit_component.rb index 80b8a3ace..15069dc48 100644 --- a/app/components/attachment/edit_component.rb +++ b/app/components/attachment/edit_component.rb @@ -72,21 +72,6 @@ class Attachment::EditComponent < ApplicationComponent }.merge(has_content_type_validator? ? { accept: accept_content_type } : {}) end - def in_progress? - return false if attachment.nil? - return true if attachment.virus_scanner.pending? - return true if attachment.watermark_pending? - - false - end - - def poll_controller_options - { - controller: 'turbo-poll', - turbo_poll_url_value: poll_url - } - end - def poll_url if champ.present? auto_attach_url diff --git a/app/components/attachment/edit_component/edit_component.html.haml b/app/components/attachment/edit_component/edit_component.html.haml index 0617e9d5e..65f975247 100644 --- a/app/components/attachment/edit_component/edit_component.html.haml +++ b/app/components/attachment/edit_component/edit_component.html.haml @@ -27,11 +27,10 @@ - if !as_multiple? = file_field(champ, field_name, **file_field_options) - - if in_progress? - %div{ data: poll_controller_options } - - if attachment.created_at < 30.seconds.ago - .fr-mt-2w - = render Attachment::LongProcessingRefreshComponent.new(attachment: attachment) + - if persisted? + - Attachment::PendingPollComponent.new(attachment: attachment, poll_url:).then do |component| + .fr-mt-2w + = render component .attachment-error.hidden %p.fr-error-text= t('.errors.uploading') diff --git a/app/components/attachment/long_processing_refresh_component.rb b/app/components/attachment/long_processing_refresh_component.rb deleted file mode 100644 index f209cb727..000000000 --- a/app/components/attachment/long_processing_refresh_component.rb +++ /dev/null @@ -1,5 +0,0 @@ -class Attachment::LongProcessingRefreshComponent < ApplicationComponent - def initialize(attachment: nil) - @attachment = attachment - end -end diff --git a/app/components/attachment/long_processing_refresh_component/long_processing_refresh_component.html.haml b/app/components/attachment/long_processing_refresh_component/long_processing_refresh_component.html.haml deleted file mode 100644 index 480cba57c..000000000 --- a/app/components/attachment/long_processing_refresh_component/long_processing_refresh_component.html.haml +++ /dev/null @@ -1,5 +0,0 @@ -= render Dsfr::CalloutComponent.new(title: nil) do |c| - - c.with_body do - = t(".explanation") - - c.with_bottom do - = button_tag t(".reload"), type: "button", class: "fr-btn", data: { action: 'click->turbo-poll#refresh' } diff --git a/app/components/attachment/multiple_component.rb b/app/components/attachment/multiple_component.rb index 4a1ad3341..39e6c4f0d 100644 --- a/app/components/attachment/multiple_component.rb +++ b/app/components/attachment/multiple_component.rb @@ -4,11 +4,12 @@ class Attachment::MultipleComponent < ApplicationComponent renders_one :template - attr_reader :champ attr_reader :attached_file - attr_reader :user_can_download - attr_reader :user_can_destroy + attr_reader :attachments + attr_reader :champ attr_reader :max + attr_reader :user_can_destroy + attr_reader :user_can_download delegate :count, :empty?, to: :attachments, prefix: true @@ -34,36 +35,8 @@ class Attachment::MultipleComponent < ApplicationComponent "attachment-multiple-empty-#{champ.id}" end - def in_progress? - @attachments.any? do - attachment_in_progress?(_1) - end - end - - def in_progress_long? - @attachments.any? do - attachment_in_progress?(_1) && _1.created_at < 30.seconds.ago - end - end - - def poll_controller_options - { - controller: 'turbo-poll', - turbo_poll_url_value: auto_attach_url - } - end - def auto_attach_url helpers.auto_attach_url(champ) end - - private - - def attachments - @attachments - end - - def attachment_in_progress?(attachment) - attachment.virus_scanner.pending? || attachment.watermark_pending? - end + alias poll_url auto_attach_url end diff --git a/app/components/attachment/multiple_component/multiple_component.html.haml b/app/components/attachment/multiple_component/multiple_component.html.haml index c0997d4ab..9dd4acf92 100644 --- a/app/components/attachment/multiple_component/multiple_component.html.haml +++ b/app/components/attachment/multiple_component/multiple_component.html.haml @@ -9,7 +9,4 @@ = render Attachment::EditComponent.new(champ:, attached_file:, attachment: nil, index: attachments_count, user_can_destroy:) // single poll and refresh message for all attachments - - if in_progress? - %div{ data: poll_controller_options } - - if in_progress_long? - = render Attachment::LongProcessingRefreshComponent.new + = render Attachment::PendingPollComponent.new(attachments: attachments, poll_url:) diff --git a/app/components/attachment/pending_poll_component.rb b/app/components/attachment/pending_poll_component.rb new file mode 100644 index 000000000..78ff9a139 --- /dev/null +++ b/app/components/attachment/pending_poll_component.rb @@ -0,0 +1,33 @@ +class Attachment::PendingPollComponent < ApplicationComponent + def initialize(poll_url:, attachment: nil, attachments: nil) + @poll_url = poll_url + @attachments = if attachment.present? + [attachment] + else + attachments + end + end + + def render? + @attachments.any? { pending_attachment?(_1) } + end + + def long_pending? + @attachments.any? do + pending_attachment?(_1) && _1.created_at < 30.seconds.ago + end + end + + def poll_controller_options + { + controller: 'turbo-poll', + turbo_poll_url_value: @poll_url + } + end + + private + + def pending_attachment?(attachment) + attachment.virus_scanner.pending? || attachment.watermark_pending? + end +end diff --git a/app/components/attachment/long_processing_refresh_component/long_processing_refresh_component.en.yml b/app/components/attachment/pending_poll_component/pending_poll_component.en.yml similarity index 100% rename from app/components/attachment/long_processing_refresh_component/long_processing_refresh_component.en.yml rename to app/components/attachment/pending_poll_component/pending_poll_component.en.yml diff --git a/app/components/attachment/long_processing_refresh_component/long_processing_refresh_component.fr.yml b/app/components/attachment/pending_poll_component/pending_poll_component.fr.yml similarity index 100% rename from app/components/attachment/long_processing_refresh_component/long_processing_refresh_component.fr.yml rename to app/components/attachment/pending_poll_component/pending_poll_component.fr.yml diff --git a/app/components/attachment/pending_poll_component/pending_poll_component.html.haml b/app/components/attachment/pending_poll_component/pending_poll_component.html.haml new file mode 100644 index 000000000..665b4779e --- /dev/null +++ b/app/components/attachment/pending_poll_component/pending_poll_component.html.haml @@ -0,0 +1,7 @@ +%div{ data: poll_controller_options } + - if long_pending? + = render Dsfr::CalloutComponent.new(title: nil) do |c| + - c.with_body do + = t(".explanation") + - c.with_bottom do + = button_tag t(".reload"), type: "button", class: "fr-btn", data: { action: 'click->turbo-poll#refresh' } diff --git a/spec/components/attachment/edit_component_spec.rb b/spec/components/attachment/edit_component_spec.rb index 0f23f1320..581ebf622 100644 --- a/spec/components/attachment/edit_component_spec.rb +++ b/spec/components/attachment/edit_component_spec.rb @@ -122,16 +122,6 @@ RSpec.describe Attachment::EditComponent, type: :component do expect(subject).to have_selector('[data-controller=turbo-poll]') end - context "process is taking longer than expected" do - before do - champ.piece_justificative_file.attachments[0].update!(created_at: 5.minutes.ago) - end - - it 'renders a refresh button' do - expect(subject).to have_button("Recharger") - end - end - context "when used as multiple context" do let(:kwargs) { { as_multiple: true } } diff --git a/spec/components/attachment/multiple_component_spec.rb b/spec/components/attachment/multiple_component_spec.rb index 6de1054b2..b34ab17cb 100644 --- a/spec/components/attachment/multiple_component_spec.rb +++ b/spec/components/attachment/multiple_component_spec.rb @@ -97,12 +97,5 @@ RSpec.describe Attachment::MultipleComponent, type: :component do it 'setup polling' do expect(subject).to have_selector('[data-controller=turbo-poll]') end - - context "process is taking longer than expected" do - let(:created_at) { 5.minutes.ago } - it 'renders a refresh button' do - expect(subject).to have_button("Recharger") - end - end end end diff --git a/spec/components/attachment/pending_poll_component_spec.rb b/spec/components/attachment/pending_poll_component_spec.rb new file mode 100644 index 000000000..90232aea5 --- /dev/null +++ b/spec/components/attachment/pending_poll_component_spec.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +require "rails_helper" + +RSpec.describe Attachment::PendingPollComponent, type: :component do + let(:champ) { create(:champ_titre_identite) } + let(:attachment) { champ.piece_justificative_file.attachments.first } + let(:component) { + described_class.new(poll_url: "poll-here", attachment:) + } + + subject { + render_inline(component).to_html + } + + context "when watermark is pending" do + it "renders turbo poll attributes" do + expect(subject).to have_selector("[data-controller='turbo-poll'][data-turbo-poll-url-value='poll-here']") + end + + it "renders" do + expect(component).to be_render + end + + it "does not render manual reload" do + expect(component).not_to have_content("Recharger") + end + + context "when watermark is pending for a long time" do + before do + attachment.created_at = 5.minutes.ago + end + + it "renders manual reload" do + expect(subject).to have_content("Recharger") + end + end + end + + context "when waterkmark is done" do + before do + attachment.blob[:metadata] = { watermark: true } + end + + it "does not render" do + expect(component).not_to be_render + end + + context "when antivirus is in progress" do + before do + attachment.blob[:metadata] = { virus_scan_result: ActiveStorage::VirusScanner::PENDING } + end + + it "renders" do + expect(component).to be_render + end + end + end +end