directly write in new filter columns

This commit is contained in:
simon lehericey 2024-09-27 15:58:34 +02:00
parent d54ab64e40
commit e9c11a95f1
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
5 changed files with 39 additions and 20 deletions

View file

@ -30,7 +30,7 @@ class Instructeurs::ColumnFilterComponent < ApplicationComponent
{ {
selected_key: column.present? ? column.id : '', selected_key: column.present? ? column.id : '',
items: filterable_columns_options, items: filterable_columns_options,
name: :column, name: "#{prefix}[id]",
id: 'search-filter', id: 'search-filter',
'aria-describedby': 'instructeur-filter-combo-label', 'aria-describedby': 'instructeur-filter-combo-label',
form: 'filter-component', form: 'filter-component',
@ -42,6 +42,17 @@ class Instructeurs::ColumnFilterComponent < ApplicationComponent
@procedure.columns.filter(&:filterable).map { [_1.label, _1.id] } @procedure.columns.filter(&:filterable).map { [_1.label, _1.id] }
end 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 private
def find_type_de_champ(column) def find_type_de_champ(column)

View file

@ -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 = 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 .fr-select-group
= label_tag :column, t('.column'), class: 'fr-label fr-m-0', id: 'instructeur-filter-combo-label', for: 'search-filter' = label_tag :column, t('.column'), class: 'fr-label fr-m-0', id: 'instructeur-filter-combo-label', for: 'search-filter'
%react-fragment %react-fragment
@ -8,9 +10,9 @@
= label_tag :value, t('.value'), for: 'value', class: 'fr-label' = label_tag :value, t('.value'), for: 'value', class: 'fr-label'
- if column_type == :enum - 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 - 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 = hidden_field_tag :statut, statut
= submit_tag t('.add_filter'), class: 'fr-btn fr-btn--secondary fr-mt-2w' = submit_tag t('.add_filter'), class: 'fr-btn fr-btn--secondary fr-mt-2w'

View file

@ -147,8 +147,10 @@ module Instructeurs
end end
def add_filter def add_filter
if !procedure_presentation.add_filter(statut, params[:column], params[:value]) if !procedure_presentation.update(filter_params)
flash.alert = procedure_presentation.errors.full_messages # complicated way to display inner error messages
flash.alert = procedure_presentation.errors
.flat_map { _1.detail[:value].errors.full_messages }
end end
redirect_back(fallback_location: instructeur_procedure_url(procedure)) redirect_back(fallback_location: instructeur_procedure_url(procedure))
@ -158,7 +160,10 @@ module Instructeurs
@statut = statut @statut = statut
@procedure = procedure @procedure = procedure
@procedure_presentation = procedure_presentation @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 end
def remove_filter def remove_filter
@ -415,5 +420,11 @@ module Instructeurs
def sorted_column_params def sorted_column_params
params.permit(sorted_column: [:order, :id]) params.permit(sorted_column: [:order, :id])
end 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
end end

View file

@ -8,27 +8,22 @@ describe Instructeurs::ColumnFilterComponent, type: :component do
let(:procedure_id) { procedure.id } let(:procedure_id) { procedure.id }
let(:procedure_presentation) { nil } let(:procedure_presentation) { nil }
let(:statut) { nil } let(:statut) { nil }
let(:column) { nil }
before do before do
allow(component).to receive(:current_instructeur).and_return(instructeur) allow(component).to receive(:current_instructeur).and_return(instructeur)
end end
describe ".filterable_columns_options" do describe ".filterable_columns_options" do
context 'filders' do let(:filterable_column) { Column.new(procedure_id:, label: 'email', table: 'user', column: 'email') }
let(:column) { nil } let(:non_filterable_column) { Column.new(procedure_id:, label: 'depose_since', table: 'self', column: 'depose_since', filterable: false) }
let(:included_displayable_field) do let(:mocked_columns) { [filterable_column, non_filterable_column] }
[
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
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]]) } it { is_expected.to eq([[filterable_column.label, filterable_column.id]]) }
end
end end
describe '.options_for_select_of_column' do describe '.options_for_select_of_column' do

View file

@ -905,8 +905,8 @@ describe Instructeurs::ProceduresController, type: :controller do
end end
subject do subject do
column = procedure.find_column(label: "Nom").id column = procedure.find_column(label: "Nom")
post :add_filter, params: { procedure_id: procedure.id, column:, value: "n" * 4100, statut: "a-suivre" } post :add_filter, params: { procedure_id: procedure.id, a_suivre_filters: { id: column.id, filter: "n" * 110 } }
end end
it 'should render the error' do it 'should render the error' do