column_table_header_component use sorted_column

This commit is contained in:
simon lehericey 2024-09-24 15:05:25 +02:00
parent 21533f91e3
commit b582a2afc6
No known key found for this signature in database
GPG key ID: CDE670D827C7B3C5
3 changed files with 15 additions and 47 deletions

View file

@ -1,47 +1,21 @@
# frozen_string_literal: true
class Instructeurs::ColumnTableHeaderComponent < ApplicationComponent
attr_reader :procedure_presentation, :column
# maybe extract a ColumnSorter class?
#
def initialize(procedure_presentation:, column:)
def initialize(procedure_presentation:)
@procedure_presentation = procedure_presentation
@column = column
@columns = procedure_presentation.displayed_fields_for_headers
@sorted_column = procedure_presentation.sorted_column
end
def column_id
column.id
def label_and_arrow(column)
return column.label if @sorted_column.column != column
@sorted_column.ascending? ? "#{column.label} ↑" : "#{column.label} ↓"
end
def sorted_by_current_column?
procedure_presentation.sort['table'] == column.table &&
procedure_presentation.sort['column'] == column.column
end
def aria_sort(column)
return {} if @sorted_column.column != column
def sorted_ascending?
current_sort_order == 'asc'
end
def sorted_descending?
current_sort_order == 'desc'
end
def aria_sort
if sorted_by_current_column?
if sorted_ascending?
{ "aria-sort": "ascending" }
elsif sorted_descending?
{ "aria-sort": "descending" }
end
else
{}
end
end
private
def current_sort_order
procedure_presentation.sort['order']
@sorted_column.ascending? ? { "aria-sort": "ascending" } : { "aria-sort": "descending" }
end
end

View file

@ -1,9 +1,4 @@
%th{ aria_sort, scope: "col", class: column.classname }
= link_to update_sort_instructeur_procedure_path(@procedure_presentation.procedure, column_id:, order: @procedure_presentation.opposite_order_for(column.table, column.column)) do
- if sorted_by_current_column?
- if sorted_ascending?
#{column.label} ↑
- else
#{column.label} ↓
- else
#{column.label}
- @columns.each do |column|
%th{ aria_sort(column), scope: "col", class: column.classname }
= link_to update_sort_instructeur_procedure_path(@procedure_presentation.procedure, column_id: column.id, order: @procedure_presentation.opposite_order_for(column.table, column.column)) do
= label_and_arrow(column)

View file

@ -93,8 +93,7 @@
%th.text-center
%input{ type: "checkbox", disabled: @disable_checkbox_all, checked: @disable_checkbox_all, data: { action: "batch-operation#onCheckAll" }, id: dom_id(BatchOperation.new, :checkbox_all), aria: { label: t('views.instructeurs.dossiers.select_all') } }
- @procedure_presentation.displayed_fields_for_headers.each do |column|
= render Instructeurs::ColumnTableHeaderComponent.new(procedure_presentation: @procedure_presentation, column:)
= render Instructeurs::ColumnTableHeaderComponent.new(procedure_presentation: @procedure_presentation)
%th.follow-col
Actions