diff --git a/app/components/instructeurs/column_table_header_component.rb b/app/components/instructeurs/column_table_header_component.rb index 09af953b7..0318da05e 100644 --- a/app/components/instructeurs/column_table_header_component.rb +++ b/app/components/instructeurs/column_table_header_component.rb @@ -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 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 67c762f2c..1542f0140 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,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) diff --git a/app/views/instructeurs/procedures/show.html.haml b/app/views/instructeurs/procedures/show.html.haml index 024075234..2a9a62fca 100644 --- a/app/views/instructeurs/procedures/show.html.haml +++ b/app/views/instructeurs/procedures/show.html.haml @@ -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