diff --git a/app/components/dossiers/edit_footer_component.rb b/app/components/dossiers/edit_footer_component.rb index fca7fab45..ac77bbfea 100644 --- a/app/components/dossiers/edit_footer_component.rb +++ b/app/components/dossiers/edit_footer_component.rb @@ -1,4 +1,6 @@ class Dossiers::EditFooterComponent < ApplicationComponent + delegate :can_passer_en_construction?, :ineligibilite_rules_computable?, to: :@dossier + def initialize(dossier:, annotation:) @dossier = dossier @annotation = annotation @@ -14,24 +16,37 @@ class Dossiers::EditFooterComponent < ApplicationComponent @annotation.present? end + def disabled_submit_buttons_options + { + class: 'fr-text--sm fr-mb-0 fr-mr-2w', + data: { 'fr-opened': "true" }, + aria: { controls: 'modal-eligibilite-rules-dialog' } + } + end + def submit_draft_button_options { class: 'fr-btn fr-btn--sm', - disabled: !owner?, + disabled: !owner? || ineligibilite_rules_invalid?, method: :post, - data: { 'disable-with': t('.submitting'), controller: 'autosave-submit' } + data: { 'disable-with': t('.submitting'), controller: 'autosave-submit', turbo_force: :server } } end def submit_en_construction_button_options { class: 'fr-btn fr-btn--sm', + disabled: ineligibilite_rules_invalid?, method: :post, - data: { 'disable-with': t('.submitting'), controller: 'autosave-submit' }, + data: { 'disable-with': t('.submitting'), controller: 'autosave-submit', turbo_force: :server }, form: { id: "form-submit-en-construction" } } end + def ineligibilite_rules_invalid? + ineligibilite_rules_computable? && !can_passer_en_construction? + end + def render? !@dossier.for_procedure_preview? end diff --git a/app/components/dossiers/edit_footer_component/edit_footer_component.en.yml b/app/components/dossiers/edit_footer_component/edit_footer_component.en.yml index 098e6ec0b..b6de7d121 100644 --- a/app/components/dossiers/edit_footer_component/edit_footer_component.en.yml +++ b/app/components/dossiers/edit_footer_component/edit_footer_component.en.yml @@ -2,5 +2,6 @@ en: submit: Submit the file submit_changes: Submit file changes + submit_disabled: File submission disabled submitting: Submitting… invite_notice: You are invited to make amendments to this file but only the owner themselves can submit it. diff --git a/app/components/dossiers/edit_footer_component/edit_footer_component.fr.yml b/app/components/dossiers/edit_footer_component/edit_footer_component.fr.yml index 33937aed6..8ffd062db 100644 --- a/app/components/dossiers/edit_footer_component/edit_footer_component.fr.yml +++ b/app/components/dossiers/edit_footer_component/edit_footer_component.fr.yml @@ -2,5 +2,6 @@ fr: submit: Déposer le dossier submit_changes: Déposer les modifications + submit_disabled: Pourquoi je ne peux pas déposer mon dossier ? submitting: Envoi en cours… invite_notice: En tant qu’invité, vous pouvez remplir ce formulaire – mais le titulaire du dossier doit le déposer lui-même. diff --git a/app/components/dossiers/edit_footer_component/edit_footer_component.html.haml b/app/components/dossiers/edit_footer_component/edit_footer_component.html.haml index 77540bd16..fb4ab8fb1 100644 --- a/app/components/dossiers/edit_footer_component/edit_footer_component.html.haml +++ b/app/components/dossiers/edit_footer_component/edit_footer_component.html.haml @@ -3,8 +3,13 @@ = render Dossiers::AutosaveFooterComponent.new(dossier: @dossier, annotation: annotation?) - if !annotation? && @dossier.can_transition_to_en_construction? + - if ineligibilite_rules_invalid? + = link_to t('.submit_disabled'), "#", disabled_submit_buttons_options = button_to t('.submit'), brouillon_dossier_url(@dossier), submit_draft_button_options - - elsif @dossier.forked_with_changes? + + - if @dossier.forked_with_changes? + - if ineligibilite_rules_invalid? + = link_to t('.submit_disabled'), "#", disabled_submit_buttons_options = button_to t('.submit_changes'), modifier_dossier_url(@dossier.editing_fork_origin), submit_en_construction_button_options diff --git a/app/models/dossier.rb b/app/models/dossier.rb index 14d9cd4f7..e83edfb97 100644 --- a/app/models/dossier.rb +++ b/app/models/dossier.rb @@ -156,7 +156,7 @@ class Dossier < ApplicationRecord state :sans_suite event :passer_en_construction, after: :after_passer_en_construction, after_commit: :after_commit_passer_en_construction do - transitions from: :brouillon, to: :en_construction + transitions from: :brouillon, to: :en_construction, guard: :can_passer_en_construction? end event :passer_en_instruction, after: :after_passer_en_instruction, after_commit: :after_commit_passer_en_instruction do @@ -562,6 +562,12 @@ class Dossier < ApplicationRecord procedure.feature_enabled?(:blocking_pending_correction) && pending_correction? end + def can_passer_en_construction? + return true if !revision.ineligibilite_enabled + + !revision.ineligibilite_rules.compute(champs_for_revision(scope: :public)) + end + def can_passer_en_instruction? return false if blocked_with_pending_correction? diff --git a/spec/components/dossiers/edit_footer_component_spec.rb b/spec/components/dossiers/edit_footer_component_spec.rb new file mode 100644 index 000000000..40e60802b --- /dev/null +++ b/spec/components/dossiers/edit_footer_component_spec.rb @@ -0,0 +1,50 @@ +RSpec.describe Dossiers::EditFooterComponent, type: :component do + let(:annotation) { false } + let(:component) { Dossiers::EditFooterComponent.new(dossier:, annotation:) } + + subject { render_inline(component).to_html } + + before { allow(component).to receive(:owner?).and_return(true) } + + context 'when brouillon' do + let(:dossier) { create(:dossier, :brouillon) } + + context 'when dossier can be submitted' do + before { allow(component).to receive(:ineligibilite_rules_invalid?).and_return(false) } + it 'renders submit button without disabled' do + expect(subject).to have_selector('button', text: 'Déposer le dossier') + end + end + + context 'when dossier can not be submitted' do + before { allow(component).to receive(:ineligibilite_rules_invalid?).and_return(true) } + it 'renders submit button with disabled' do + expect(subject).to have_selector('a', text: 'Pourquoi je ne peux pas déposer mon dossier ?') + expect(subject).to have_selector('button[disabled]', text: 'Déposer le dossier') + end + end + end + + context 'when en construction' do + let(:fork_origin) { create(:dossier, :en_construction) } + let(:dossier) { fork_origin.clone(fork: true) } + before { allow(dossier).to receive(:forked_with_changes?).and_return(true) } + + context 'when dossier can be submitted' do + before { allow(component).to receive(:ineligibilite_rules_invalid?).and_return(false) } + + it 'renders submit button without disabled' do + expect(subject).to have_selector('button', text: 'Déposer les modifications') + end + end + + context 'when dossier can not be submitted' do + before { allow(component).to receive(:ineligibilite_rules_invalid?).and_return(true) } + + it 'renders submit button with disabled' do + expect(subject).to have_selector('a', text: 'Pourquoi je ne peux pas déposer mon dossier ?') + expect(subject).to have_selector('button[disabled]', text: 'Déposer les modifications') + end + end + end +end