[Enhancement] Add sorting, pagination, and new attributes to sources index table (#510)
* WIP - started improving handling of sorting for sources index table * WIP - Added UI to table to indicate sort column and direction * Refactored toggle liveview into a livecomponent * Added sorting for all table attrs * Added pagination to the sources table * Added tests for updated liveviews and live components * Add tests for new helper methods * Added fancy new CSS to my sources table * Added size to sources table * Adds relative div to ensure that sorting arrow doesn't run away * Fixed da tests
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
defmodule PinchflatWeb.Helpers.PaginationHelpers do
|
||||
@moduledoc """
|
||||
Methods for working with pagination, usually in the context of LiveViews or LiveComponents.
|
||||
|
||||
These methods are fairly simple, but they're commonly repeated across different Live entities
|
||||
"""
|
||||
|
||||
alias Pinchflat.Repo
|
||||
alias Pinchflat.Utils.NumberUtils
|
||||
|
||||
@doc """
|
||||
Given a query, a page number, and a number of records per page, returns a map of pagination attributes.
|
||||
|
||||
Returns map()
|
||||
"""
|
||||
def get_pagination_attributes(query, page, records_per_page) do
|
||||
total_record_count = Repo.aggregate(query, :count, :id)
|
||||
total_pages = max(ceil(total_record_count / records_per_page), 1)
|
||||
clamped_page = NumberUtils.clamp(page, 1, total_pages)
|
||||
|
||||
%{
|
||||
page: clamped_page,
|
||||
total_pages: total_pages,
|
||||
total_record_count: total_record_count,
|
||||
limit: records_per_page,
|
||||
offset: (clamped_page - 1) * records_per_page
|
||||
}
|
||||
end
|
||||
|
||||
@doc """
|
||||
Given a current page number, a direction to move in, and the total number of pages, returns the updated page number.
|
||||
The updated page number is clamped to the range [1, total_pages].
|
||||
|
||||
Returns integer()
|
||||
"""
|
||||
def update_page_number(current_page, direction, total_pages) do
|
||||
updated_page =
|
||||
case to_string(direction) do
|
||||
"inc" -> current_page + 1
|
||||
"dec" -> current_page - 1
|
||||
end
|
||||
|
||||
NumberUtils.clamp(updated_page, 1, total_pages)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,20 @@
|
||||
defmodule PinchflatWeb.Helpers.SortingHelpers do
|
||||
@moduledoc """
|
||||
Methods for working with sorting, usually in the context of LiveViews or LiveComponents.
|
||||
|
||||
These methods are fairly simple, but they're commonly repeated across different Live entities
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Given the old sort attribute, the new sort attribute, and the old sort direction, returns the new sort direction.
|
||||
|
||||
Returns :asc | :desc
|
||||
"""
|
||||
def get_sort_direction(old_sort_attr, new_sort_attr, old_sort_direction) do
|
||||
case {new_sort_attr, old_sort_direction} do
|
||||
{^old_sort_attr, :desc} -> :asc
|
||||
{^old_sort_attr, _} -> :desc
|
||||
_ -> :asc
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user