From 743ff8c8a9a0fccf7bf5b18ac96d5f7ec86ab7a0 Mon Sep 17 00:00:00 2001 From: mfo Date: Mon, 22 Jul 2024 09:37:15 +0200 Subject: [PATCH] extract Instructeurs::FacetTableHeaderComponent wrapping sort logic --- .../column_table_header_component.rb | 40 +++++++++++++++++++ .../column_table_header_component.html.haml | 9 +++++ app/models/procedure_presentation.rb | 17 -------- .../instructeurs/procedures/show.html.haml | 4 +- 4 files changed, 51 insertions(+), 19 deletions(-) create mode 100644 app/components/instructeurs/column_table_header_component.rb create mode 100644 app/components/instructeurs/column_table_header_component/column_table_header_component.html.haml diff --git a/app/components/instructeurs/column_table_header_component.rb b/app/components/instructeurs/column_table_header_component.rb new file mode 100644 index 000000000..9c2d2df36 --- /dev/null +++ b/app/components/instructeurs/column_table_header_component.rb @@ -0,0 +1,40 @@ +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 sorted_by_current_facet? + procedure_presentation.sort['table'] == facet.table && + procedure_presentation.sort['column'] == facet.column + end + + def sorted_ascending? + current_sort_order == 'asc' + end + + def sorted_descending? + current_sort_order == 'desc' + end + + def aria_sort + if sorted_by_current_facet? + 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'] + 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 new file mode 100644 index 000000000..faacf92a0 --- /dev/null +++ b/app/components/instructeurs/column_table_header_component/column_table_header_component.html.haml @@ -0,0 +1,9 @@ +%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 + - if sorted_by_current_facet? + - if sorted_ascending? + #{facet.label} ↑ + - else + #{facet.label} ↓ + - else + #{facet.label} diff --git a/app/models/procedure_presentation.rb b/app/models/procedure_presentation.rb index f3638529e..03ae96513 100644 --- a/app/models/procedure_presentation.rb +++ b/app/models/procedure_presentation.rb @@ -146,23 +146,6 @@ class ProcedurePresentation < ApplicationRecord slice(:filters, :sort, :displayed_fields) end - def sorted_by?(facet) - sort['table'] == facet.table && - sort['column'] == facet.column - end - - def aria_sort(order, field) - if sorted_by?(field) - if order == 'asc' - { "aria-sort": "ascending" } - elsif order == 'desc' - { "aria-sort": "descending" } - end - else - {} - end - end - private def sorted_ids(dossiers, count) diff --git a/app/views/instructeurs/procedures/show.html.haml b/app/views/instructeurs/procedures/show.html.haml index 69e33b9cf..43e617a65 100644 --- a/app/views/instructeurs/procedures/show.html.haml +++ b/app/views/instructeurs/procedures/show.html.haml @@ -98,8 +98,8 @@ %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 |field| - = render partial: "header_field", locals: { field: field } + - @procedure_presentation.displayed_fields_for_headers.each do |facet| + = render Instructeurs::ColumnTableHeaderComponent.new(procedure_presentation: @procedure_presentation, facet:) %th.follow-col Actions