diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index b1767ef5..d8f1b70c 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -5,7 +5,10 @@ class TopicsController < ApplicationController before_action :set_topic, only: [ :show, :edit, :update, :destroy, :archive, :unarchive ] def index - @pagy, @topics = pagy(scope.includes(:documents_attachments).search_with_params(search_params)) + topics = scope.includes(:documents_attachments).search_with_params(search_params) + topics = topics.reorder(created_at: Topic.sort_order(search_params[:order]&.to_sym)) if search_params[:sort] == "created_at" + + @pagy, @topics = pagy(topics) @available_providers = other_available_providers @languages = scope.map(&:language).uniq.sort_by(&:name) end @@ -107,7 +110,7 @@ def mutator def search_params return { state: :active } unless params[:search].present? - params.require(:search).permit(:query, :state, :language_id, :year, :month, :order, tag_list: []) + params.require(:search).permit(:query, :state, :language_id, :year, :month, :sort, :order, tag_list: []) end helper_method :search_params diff --git a/app/helpers/topics_helper.rb b/app/helpers/topics_helper.rb index f0be16ff..d8935fd3 100644 --- a/app/helpers/topics_helper.rb +++ b/app/helpers/topics_helper.rb @@ -1,4 +1,31 @@ module TopicsHelper + def topic_sort_header(label, column) + current_column = search_params[:sort].presence_in(%w[published_at created_at]) || "published_at" + current_order = search_params[:order].presence_in(%w[asc desc]) || "desc" + active = current_column == column.to_s + next_order = current_order + next_order = current_order == "asc" ? "desc" : "asc" if active + indicator = active ? (current_order == "asc" ? "▲" : "▼") : "↕" + query = search_params.to_h.merge(sort: column, order: next_order) + + link_to topics_path(search: query), + class: "inline-flex items-center gap-1 hover:text-gray-900 focus-visible:outline-2 focus-visible:outline-offset-2", + aria: { label: "Sort by #{label} #{next_order == "asc" ? "ascending" : "descending"}" }, + data: { turbo_frame: "_top" } do + safe_join([ + content_tag(:span, label), + content_tag(:span, indicator, class: active ? "text-gray-700" : "text-gray-400", aria: { hidden: true }), + ]) + end + end + + def topic_sort_aria(column) + current_column = search_params[:sort].presence_in(%w[published_at created_at]) || "published_at" + return "none" unless current_column == column.to_s + + search_params[:order] == "asc" ? "ascending" : "descending" + end + def card_preview_media(file) case file.content_type in /image/ then render_image(file) diff --git a/app/views/topics/_list.html.erb b/app/views/topics/_list.html.erb index b126f705..8429acc9 100644 --- a/app/views/topics/_list.html.erb +++ b/app/views/topics/_list.html.erb @@ -2,26 +2,21 @@ <% if @topics.any? %> <% @topics.each do |topic| %>
| Title | - - -Language | - -# of Documents | -State | -Actions | +<%= topic_sort_header("Published", :published_at) %> | +Title | +<%= topic_sort_header("Added", :created_at) %> | +Language | + +# of Documents | +State | +Actions |
|---|