diff --git a/app/components/instructeurs/column_filter_component.rb b/app/components/instructeurs/column_filter_component.rb index 3ea2b1153..7843bcbb1 100644 --- a/app/components/instructeurs/column_filter_component.rb +++ b/app/components/instructeurs/column_filter_component.rb @@ -30,7 +30,7 @@ class Instructeurs::ColumnFilterComponent < ApplicationComponent { selected_key: column.present? ? column.id : '', items: filterable_columns_options, - name: :column, + name: "#{prefix}[id]", id: 'search-filter', 'aria-describedby': 'instructeur-filter-combo-label', form: 'filter-component', @@ -42,6 +42,17 @@ class Instructeurs::ColumnFilterComponent < ApplicationComponent @procedure.columns.filter(&:filterable).map { [_1.label, _1.id] } end + def current_filter_tags + @procedure_presentation.filters_for(@statut).flat_map do + [ + hidden_field_tag("#{prefix}[id]", _1.column.id, id: nil), + hidden_field_tag("#{prefix}[filter]", _1.filter, id: nil) + ] + end.reduce(&:concat) + end + + def prefix = "#{procedure_presentation.filters_name_for(@statut)}[]" + private def find_type_de_champ(column) diff --git a/app/components/instructeurs/column_filter_component/column_filter_component.html.haml b/app/components/instructeurs/column_filter_component/column_filter_component.html.haml index 6f43592d3..484bf88f5 100644 --- a/app/components/instructeurs/column_filter_component/column_filter_component.html.haml +++ b/app/components/instructeurs/column_filter_component/column_filter_component.html.haml @@ -1,4 +1,6 @@ = form_tag add_filter_instructeur_procedure_path(procedure), method: :post, class: 'dropdown-form large', id: 'filter-component', data: { turbo: true, controller: 'autosubmit' } do + = current_filter_tags + .fr-select-group = label_tag :column, t('.column'), class: 'fr-label fr-m-0', id: 'instructeur-filter-combo-label', for: 'search-filter' %react-fragment @@ -8,9 +10,9 @@ = label_tag :value, t('.value'), for: 'value', class: 'fr-label' - if column_type == :enum - = select_tag :value, options_for_select(options_for_select_of_column), id: 'value', name: 'value', class: 'fr-select', data: { no_autosubmit: true } + = select_tag :filter, options_for_select(options_for_select_of_column), id: 'value', name: "#{prefix}[filter]", class: 'fr-select', data: { no_autosubmit: true } - else - %input#value.fr-input{ type: column_type, name: :value, maxlength: ProcedurePresentation::FILTERS_VALUE_MAX_LENGTH, disabled: column.nil? ? true : false, data: { no_autosubmit: true } } + %input#value.fr-input{ type: column_type, name: "#{prefix}[filter]", maxlength: ProcedurePresentation::FILTERS_VALUE_MAX_LENGTH, disabled: column.nil? ? true : false, data: { no_autosubmit: true } } = hidden_field_tag :statut, statut = submit_tag t('.add_filter'), class: 'fr-btn fr-btn--secondary fr-mt-2w' diff --git a/app/controllers/instructeurs/procedures_controller.rb b/app/controllers/instructeurs/procedures_controller.rb index 91f685dd7..03d731e12 100644 --- a/app/controllers/instructeurs/procedures_controller.rb +++ b/app/controllers/instructeurs/procedures_controller.rb @@ -147,8 +147,10 @@ module Instructeurs end def add_filter - if !procedure_presentation.add_filter(statut, params[:column], params[:value]) - flash.alert = procedure_presentation.errors.full_messages + if !procedure_presentation.update(filter_params) + # complicated way to display inner error messages + flash.alert = procedure_presentation.errors + .flat_map { _1.detail[:value].errors.full_messages } end redirect_back(fallback_location: instructeur_procedure_url(procedure)) @@ -158,7 +160,10 @@ module Instructeurs @statut = statut @procedure = procedure @procedure_presentation = procedure_presentation - @column = procedure.find_column(h_id: JSON.parse(params[:column], symbolize_names: true)) + current_filter = procedure_presentation.filters_name_for(@statut) + # According to the html, the selected column is the last one + h_id = JSON.parse(params[current_filter].last[:id], symbolize_names: true) + @column = procedure.find_column(h_id:) end def remove_filter @@ -415,5 +420,11 @@ module Instructeurs def sorted_column_params params.permit(sorted_column: [:order, :id]) end + + def filter_params + keys = [:tous_filters, :a_suivre_filters, :suivis_filters, :traites_filters, :expirant_filters, :archives_filters, :supprimes_filters] + h = keys.index_with { [:id, :filter] } + params.permit(h) + end end end diff --git a/spec/components/instructeurs/column_filter_component_spec.rb b/spec/components/instructeurs/column_filter_component_spec.rb index 8c31094dd..b1dc597ae 100644 --- a/spec/components/instructeurs/column_filter_component_spec.rb +++ b/spec/components/instructeurs/column_filter_component_spec.rb @@ -8,27 +8,22 @@ describe Instructeurs::ColumnFilterComponent, type: :component do let(:procedure_id) { procedure.id } let(:procedure_presentation) { nil } let(:statut) { nil } + let(:column) { nil } before do allow(component).to receive(:current_instructeur).and_return(instructeur) end describe ".filterable_columns_options" do - context 'filders' do - let(:column) { nil } - let(:included_displayable_field) do - [ - Column.new(procedure_id:, label: 'email', table: 'user', column: 'email'), - Column.new(procedure_id:, label: "depose_since", table: "self", column: "depose_since", displayable: false) - ] - end + let(:filterable_column) { Column.new(procedure_id:, label: 'email', table: 'user', column: 'email') } + let(:non_filterable_column) { Column.new(procedure_id:, label: 'depose_since', table: 'self', column: 'depose_since', filterable: false) } + let(:mocked_columns) { [filterable_column, non_filterable_column] } - before { allow(procedure).to receive(:columns).and_return(included_displayable_field) } + before { allow(procedure).to receive(:columns).and_return(mocked_columns) } - subject { component.filterable_columns_options } + subject { component.filterable_columns_options } - it { is_expected.to eq([["email", included_displayable_field.first.id], ["depose_since", included_displayable_field.second.id]]) } - end + it { is_expected.to eq([[filterable_column.label, filterable_column.id]]) } end describe '.options_for_select_of_column' do diff --git a/spec/controllers/instructeurs/procedures_controller_spec.rb b/spec/controllers/instructeurs/procedures_controller_spec.rb index c9e1a8894..d5954af14 100644 --- a/spec/controllers/instructeurs/procedures_controller_spec.rb +++ b/spec/controllers/instructeurs/procedures_controller_spec.rb @@ -905,8 +905,8 @@ describe Instructeurs::ProceduresController, type: :controller do end subject do - column = procedure.find_column(label: "Nom").id - post :add_filter, params: { procedure_id: procedure.id, column:, value: "n" * 4100, statut: "a-suivre" } + column = procedure.find_column(label: "Nom") + post :add_filter, params: { procedure_id: procedure.id, a_suivre_filters: { id: column.id, filter: "n" * 110 } } end it 'should render the error' do