From a0671a13215580be969a39eb522fb55cd34e3a37 Mon Sep 17 00:00:00 2001 From: simon lehericey Date: Sat, 26 Oct 2024 21:12:39 +0200 Subject: [PATCH] remove prefix, use status in controller --- .../instructeurs/column_filter_component.rb | 8 +++---- .../column_filter_component.html.haml | 3 +-- .../column_filter_value_component.rb | 9 ++++---- .../procedure_presentation_controller.rb | 23 ++++++++----------- .../procedure_presentation_controller_spec.rb | 7 ++++-- 5 files changed, 22 insertions(+), 28 deletions(-) diff --git a/app/components/instructeurs/column_filter_component.rb b/app/components/instructeurs/column_filter_component.rb index 93d4364b4..f92a3c623 100644 --- a/app/components/instructeurs/column_filter_component.rb +++ b/app/components/instructeurs/column_filter_component.rb @@ -14,7 +14,7 @@ class Instructeurs::ColumnFilterComponent < ApplicationComponent { selected_key: column.present? ? column.id : '', items: filterable_columns_options, - name: "#{prefix}[id]", + name: "filters[][id]", id: 'search-filter', 'aria-describedby': 'instructeur-filter-combo-label', form: 'filter-component', @@ -29,11 +29,9 @@ class Instructeurs::ColumnFilterComponent < ApplicationComponent 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) + hidden_field_tag("filters[][id]", _1.column.id, id: nil), + hidden_field_tag("filters[][filter]", _1.filter, id: nil) ] end.reduce(&:concat) end - - def prefix = "#{procedure_presentation.filters_name_for(@statut)}[]" end 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 ae6d34935..c8b94293f 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 @@ -18,8 +18,7 @@ } = label_tag :value, t('.value'), for: 'value', class: 'fr-label' - = render Instructeurs::ColumnFilterValueComponent.new(column:, prefix:) + = render Instructeurs::ColumnFilterValueComponent.new(column:) = hidden_field_tag :statut, statut - = hidden_field_tag :prefix, prefix = submit_tag t('.add_filter'), class: 'fr-btn fr-btn--secondary fr-mt-2w' diff --git a/app/components/instructeurs/column_filter_value_component.rb b/app/components/instructeurs/column_filter_value_component.rb index 684964b29..d83a239ea 100644 --- a/app/components/instructeurs/column_filter_value_component.rb +++ b/app/components/instructeurs/column_filter_value_component.rb @@ -3,9 +3,8 @@ class Instructeurs::ColumnFilterValueComponent < ApplicationComponent attr_reader :column - def initialize(column:, prefix:) + def initialize(column:) @column = column - @prefix = prefix end def column_type = column.present? ? column.type : :text @@ -15,15 +14,15 @@ class Instructeurs::ColumnFilterValueComponent < ApplicationComponent select_tag :filter, options_for_select(options_for_select_of_column), id: 'value', - name: "#{@prefix}[filter]", + name: "filters[][filter]", class: 'fr-select', data: { no_autosubmit: true } else tag.input( class: 'fr-input', id: 'value', - type: html_column_type, - name: "#{@prefix}[filter]", + type:, + name: "filters[][filter]", maxlength: FilteredColumn::FILTERS_VALUE_MAX_LENGTH, disabled: column.nil? ? true : false, data: { no_autosubmit: true }, diff --git a/app/controllers/instructeurs/procedure_presentation_controller.rb b/app/controllers/instructeurs/procedure_presentation_controller.rb index b79d678d7..952464188 100644 --- a/app/controllers/instructeurs/procedure_presentation_controller.rb +++ b/app/controllers/instructeurs/procedure_presentation_controller.rb @@ -2,7 +2,7 @@ module Instructeurs class ProcedurePresentationController < InstructeurController - before_action :set_procedure_presentation + before_action :set_procedure_presentation, only: [:update] def update if !@procedure_presentation.update(procedure_presentation_params) @@ -15,11 +15,9 @@ module Instructeurs end def refresh_column_filter - prefix = params[:prefix] - key = prefix.gsub('[]', '') - column = ColumnType.new.cast(params[key].last['id']) - - component = Instructeurs::ColumnFilterValueComponent.new(column:, prefix:) + # According to the html, the selected filters is the last one + column = ColumnType.new.cast(params['filters'].last['id']) + component = Instructeurs::ColumnFilterValueComponent.new(column:) render turbo_stream: turbo_stream.replace('value', component) end @@ -29,15 +27,12 @@ module Instructeurs def procedure = @procedure_presentation.procedure def procedure_presentation_params - # TODO: peut etre simplifier en transformer un parametre filter -> tous_filter, suivant le params statut + h = params.permit(displayed_columns: [], sorted_column: [:order, :id], filters: [:id, :filter]).to_h - - filters = [ - :tous_filters, :a_suivre_filters, :suivis_filters, :traites_filters, - :expirant_filters, :archives_filters, :supprimes_filters - ].index_with { [:id, :filter] } - - h = params.permit(displayed_columns: [], sorted_column: [:order, :id], **filters).to_h + if params[:statut].present? + filter_name = @procedure_presentation.filters_name_for(params[:statut]) + h[filter_name] = h.delete("filters") # move filters to the right key, ex: tous_filters + end # React ComboBox/MultiComboBox return [''] when no value is selected # We need to remove them diff --git a/spec/controllers/instructeurs/procedure_presentation_controller_spec.rb b/spec/controllers/instructeurs/procedure_presentation_controller_spec.rb index df0548301..ccc3d51cb 100644 --- a/spec/controllers/instructeurs/procedure_presentation_controller_spec.rb +++ b/spec/controllers/instructeurs/procedure_presentation_controller_spec.rb @@ -22,7 +22,8 @@ describe Instructeurs::ProcedurePresentationController, type: :controller do { displayed_columns: [state_column.id], sorted_column: { order: 'asc', id: state_column.id }, - tous_filters: [{ id: state_column.id, filter: 'en_construction' }] + filters: [{ id: state_column.id, filter: 'en_construction' }], + statut: 'tous' } end @@ -71,7 +72,9 @@ describe Instructeurs::ProcedurePresentationController, type: :controller do context 'with an error in filters' do before { sign_in(instructeur.user) } - let(:presentation_params) { { tous_filters: [{ id: state_column.id, filter: '' }] } } + let(:presentation_params) do + { filters: [{ id: state_column.id, filter: '' }], statut: 'tous' } + end it 'does not update the procedure_presentation' do subject