tech: use facet_id to sort fields

This commit is contained in:
mfo 2024-07-22 11:32:07 +02:00
parent 743ff8c8a9
commit b6464c1963
No known key found for this signature in database
GPG key ID: 7CE3E1F5B794A8EC
10 changed files with 38 additions and 18 deletions

View file

@ -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-fieldset__element.fr-m-0
.fr-checkbox-group.fr-checkbox-group--sm .fr-checkbox-group.fr-checkbox-group--sm
= check_box_tag :order, opposite_order, active? = check_box_tag :order, opposite_order, active?

View file

@ -1,12 +1,17 @@
class Instructeurs::ColumnTableHeaderComponent < ApplicationComponent class Instructeurs::ColumnTableHeaderComponent < ApplicationComponent
attr_reader :procedure_presentation, :facet attr_reader :procedure_presentation, :facet
# maybe extract a FacetSorter class? # maybe extract a FacetSorter class?
#
def initialize(procedure_presentation:, facet:) def initialize(procedure_presentation:, facet:)
@procedure_presentation = procedure_presentation @procedure_presentation = procedure_presentation
@facet = facet @facet = facet
end end
def facet_id
facet.id
end
def sorted_by_current_facet? def sorted_by_current_facet?
procedure_presentation.sort['table'] == facet.table && procedure_presentation.sort['table'] == facet.table &&
procedure_presentation.sort['column'] == facet.column procedure_presentation.sort['column'] == facet.column

View file

@ -1,5 +1,5 @@
%th{ aria_sort, scope: "col", class: facet.classname } %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_by_current_facet?
- if sorted_ascending? - if sorted_ascending?
#{facet.label} ↑ #{facet.label} ↑

View file

@ -141,7 +141,7 @@ module Instructeurs
end end
def update_sort 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)) redirect_back(fallback_location: instructeur_procedure_url(procedure))
end end

View file

@ -15,6 +15,8 @@ module FacetsConcern
end end
def dossier_facets 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'] dates = ['created_at', 'updated_at', 'depose_at', 'en_construction_at', 'en_instruction_at', 'processed_at']
.map { |column| Facet.new(table: 'self', column:, type: :date) } .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)] 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 end
def sva_svr_facets(for_filters: false) def sva_svr_facets(for_filters: false)

View file

@ -124,11 +124,13 @@ class ProcedurePresentation < ApplicationRecord
end end
end end
def update_sort(table, column, order) def update_sort(facet_id, order)
facet = procedure.find_facet(id: facet_id)
update!(sort: { update!(sort: {
TABLE => table, TABLE => facet.table,
COLUMN => column, COLUMN => facet.column,
ORDER => order.presence || opposite_order_for(table, column) ORDER => order.presence || opposite_order_for(facet.table, facet.column)
}) })
end end

View file

@ -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}

View file

@ -473,7 +473,7 @@ Rails.application.routes.draw do
end end
patch 'update_displayed_fields' 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 'add_filter'
post 'update_filter' post 'update_filter'
get 'remove_filter' get 'remove_filter'

View file

@ -872,6 +872,24 @@ describe Instructeurs::ProceduresController, type: :controller do
end end
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 describe '#add_filter' do
let(:instructeur) { create(:instructeur) } let(:instructeur) { create(:instructeur) }
let(:procedure) { create(:procedure, :for_individual) } let(:procedure) { create(:procedure, :for_individual) }

View file

@ -14,6 +14,8 @@ describe FacetsConcern do
let(:tdc_private_2) { procedure.active_revision.types_de_champ_private[1] } let(:tdc_private_2) { procedure.active_revision.types_de_champ_private[1] }
let(:expected) { 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: '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: '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 }, { label: 'Déposé le', table: 'self', column: 'depose_at', classname: '', virtual: false, type: :date, scope: '', value_column: :value, filterable: true },