From b6464c1963526b88eb1155a86e530558fab06876 Mon Sep 17 00:00:00 2001 From: mfo Date: Mon, 22 Jul 2024 11:32:07 +0200 Subject: [PATCH] tech: use facet_id to sort fields --- .../notified_toggle_component.html.haml | 2 +- .../column_table_header_component.rb | 5 +++++ .../column_table_header_component.html.haml | 2 +- .../instructeurs/procedures_controller.rb | 2 +- app/models/concerns/facets_concern.rb | 4 +++- app/models/procedure_presentation.rb | 10 ++++++---- .../procedures/_header_field.html.haml | 9 --------- config/routes.rb | 2 +- .../instructeurs/procedures_controller_spec.rb | 18 ++++++++++++++++++ spec/models/concerns/facets_concern_spec.rb | 2 ++ 10 files changed, 38 insertions(+), 18 deletions(-) delete mode 100644 app/views/instructeurs/procedures/_header_field.html.haml diff --git a/app/components/dossiers/notified_toggle_component/notified_toggle_component.html.haml b/app/components/dossiers/notified_toggle_component/notified_toggle_component.html.haml index 4f6eca594..2b87518fd 100644 --- a/app/components/dossiers/notified_toggle_component/notified_toggle_component.html.haml +++ b/app/components/dossiers/notified_toggle_component/notified_toggle_component.html.haml @@ -1,4 +1,4 @@ -= form_tag update_sort_instructeur_procedure_path(procedure_id: @procedure.id, table: 'notifications', column: 'notifications', order: opposite_order), method: :get, data: { controller: 'autosubmit' } do += form_tag update_sort_instructeur_procedure_path(procedure_id: @procedure.id, facet_id: 'notifications/notifications', order: opposite_order), method: :get, data: { controller: 'autosubmit' } do .fr-fieldset__element.fr-m-0 .fr-checkbox-group.fr-checkbox-group--sm = check_box_tag :order, opposite_order, active? diff --git a/app/components/instructeurs/column_table_header_component.rb b/app/components/instructeurs/column_table_header_component.rb index 9c2d2df36..9d210bb37 100644 --- a/app/components/instructeurs/column_table_header_component.rb +++ b/app/components/instructeurs/column_table_header_component.rb @@ -1,12 +1,17 @@ class Instructeurs::ColumnTableHeaderComponent < ApplicationComponent attr_reader :procedure_presentation, :facet # maybe extract a FacetSorter class? + # def initialize(procedure_presentation:, facet:) @procedure_presentation = procedure_presentation @facet = facet end + def facet_id + facet.id + end + def sorted_by_current_facet? procedure_presentation.sort['table'] == facet.table && procedure_presentation.sort['column'] == facet.column diff --git a/app/components/instructeurs/column_table_header_component/column_table_header_component.html.haml b/app/components/instructeurs/column_table_header_component/column_table_header_component.html.haml index faacf92a0..fee5caf74 100644 --- a/app/components/instructeurs/column_table_header_component/column_table_header_component.html.haml +++ b/app/components/instructeurs/column_table_header_component/column_table_header_component.html.haml @@ -1,5 +1,5 @@ %th{ aria_sort, scope: "col", class: facet.classname } - = link_to update_sort_instructeur_procedure_path(@procedure_presentation.procedure, table: facet.table, column: facet.column, order: @procedure_presentation.opposite_order_for(facet.table, facet.column)) do + = link_to update_sort_instructeur_procedure_path(@procedure_presentation.procedure, facet_id:, order: @procedure_presentation.opposite_order_for(facet.table, facet.column)) do - if sorted_by_current_facet? - if sorted_ascending? #{facet.label} ↑ diff --git a/app/controllers/instructeurs/procedures_controller.rb b/app/controllers/instructeurs/procedures_controller.rb index c7bd254b7..61ca05d9f 100644 --- a/app/controllers/instructeurs/procedures_controller.rb +++ b/app/controllers/instructeurs/procedures_controller.rb @@ -141,7 +141,7 @@ module Instructeurs end def update_sort - procedure_presentation.update_sort(params[:table], params[:column], params[:order]) + procedure_presentation.update_sort(params[:facet_id], params[:order]) redirect_back(fallback_location: instructeur_procedure_url(procedure)) end diff --git a/app/models/concerns/facets_concern.rb b/app/models/concerns/facets_concern.rb index f041f2c8b..417ab1d64 100644 --- a/app/models/concerns/facets_concern.rb +++ b/app/models/concerns/facets_concern.rb @@ -15,6 +15,8 @@ module FacetsConcern end def dossier_facets + common = [Facet.new(table: 'self', column: 'id', classname: 'number-col'), Facet.new(table: 'notifications', column: 'notifications', label: "notifications", filterable: false)] + dates = ['created_at', 'updated_at', 'depose_at', 'en_construction_at', 'en_instruction_at', 'processed_at'] .map { |column| Facet.new(table: 'self', column:, type: :date) } @@ -23,7 +25,7 @@ module FacetsConcern states = [Facet.new(table: 'self', column: 'state', type: :enum, scope: 'instructeurs.dossiers.filterable_state', virtual: true)] - [dates, sva_svr_facets(for_filters: true), virtual_dates, states].flatten.compact + [common, dates, sva_svr_facets(for_filters: true), virtual_dates, states].flatten.compact end def sva_svr_facets(for_filters: false) diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index 03ae96513..9ebe1535f 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -124,11 +124,13 @@ class ProcedurePresentation < ApplicationRecord end end - def update_sort(table, column, order) + def update_sort(facet_id, order) + facet = procedure.find_facet(id: facet_id) + update!(sort: { - TABLE => table, - COLUMN => column, - ORDER => order.presence || opposite_order_for(table, column) + TABLE => facet.table, + COLUMN => facet.column, + ORDER => order.presence || opposite_order_for(facet.table, facet.column) }) end diff --git a/app/views/instructeurs/procedures/_header_field.html.haml b/app/views/instructeurs/procedures/_header_field.html.haml deleted file mode 100644 index 2d46b77b5..000000000 --- a/app/views/instructeurs/procedures/_header_field.html.haml +++ /dev/null @@ -1,9 +0,0 @@ -%th{ @procedure_presentation.aria_sort(@procedure_presentation.sort['order'], field), scope: "col", class: field.classname } - = link_to update_sort_instructeur_procedure_path(@procedure, table: field.table, column: field.column, order: @procedure_presentation.opposite_order_for(field.table, field.column)) do - - if @procedure_presentation.sorted_by?(field) - - if @procedure_presentation.sort['order'] == 'asc' - #{field.label} ↑ - - else - #{field.label} ↓ - - else - #{field.label} diff --git a/config/routes.rb b/config/routes.rb index cc1f9f7b3..502307d7c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -473,7 +473,7 @@ Rails.application.routes.draw do end patch 'update_displayed_fields' - get 'update_sort/:table/:column' => 'procedures#update_sort', as: 'update_sort' + get 'update_sort/:facet_id' => 'procedures#update_sort', as: 'update_sort' post 'add_filter' post 'update_filter' get 'remove_filter' diff --git a/spec/controllers/instructeurs/procedures_controller_spec.rb b/spec/controllers/instructeurs/procedures_controller_spec.rb index 8aad3822d..c729d0b6c 100644 --- a/spec/controllers/instructeurs/procedures_controller_spec.rb +++ b/spec/controllers/instructeurs/procedures_controller_spec.rb @@ -872,6 +872,24 @@ describe Instructeurs::ProceduresController, type: :controller do end end + describe '#update_filter' do + let(:instructeur) { create(:instructeur) } + let(:procedure) { create(:procedure, :for_individual) } + def procedure_presentation = instructeur.assign_to.first.procedure_presentation_or_default_and_errors.first + + before do + create(:assign_to, instructeur:, groupe_instructeur: build(:groupe_instructeur, procedure:)) + + sign_in(instructeur.user) + end + + it 'can change order' do + expect { get :update_sort, params: { procedure_id: procedure.id, facet_id: "individual/nom", order: 'asc' } } + .to change { procedure_presentation.sort } + .from({ "column" => "notifications", "order" => "desc", "table" => "notifications" }) + .to({ "column" => "nom", "order" => "asc", "table" => "individual" }) + end + end describe '#add_filter' do let(:instructeur) { create(:instructeur) } let(:procedure) { create(:procedure, :for_individual) } diff --git a/spec/models/concerns/facets_concern_spec.rb b/spec/models/concerns/facets_concern_spec.rb index 5ba738441..acf82c6ae 100644 --- a/spec/models/concerns/facets_concern_spec.rb +++ b/spec/models/concerns/facets_concern_spec.rb @@ -14,6 +14,8 @@ describe FacetsConcern do let(:tdc_private_2) { procedure.active_revision.types_de_champ_private[1] } let(:expected) { [ + { label: 'Nº dossier', table: 'self', column: 'id', classname: 'number-col', virtual: false, type: :text, scope: '', value_column: :value, filterable: true }, + { label: 'notifications', table: 'notifications', column: 'notifications', virtual: false, type: :text, scope: '', value_column: :value, filterable: false }, { label: 'Créé le', table: 'self', column: 'created_at', classname: '', virtual: false, type: :date, scope: '', value_column: :value, filterable: true }, { label: 'Mis à jour le', table: 'self', column: 'updated_at', classname: '', virtual: false, type: :date, scope: '', value_column: :value, filterable: true }, { label: 'Déposé le', table: 'self', column: 'depose_at', classname: '', virtual: false, type: :date, scope: '', value_column: :value, filterable: true },