add inital_key and initial_sort_mode to table component

This commit is contained in:
shibao 2022-11-09 21:03:46 -05:00
parent 67304ae22e
commit 1f017ced4a

View File

@ -36,18 +36,38 @@ defmodule CanneryWeb.Components.TableComponent do
list(%{ list(%{
(key :: atom()) => any() | {custom_sort_value :: String.t(), value :: any()} (key :: atom()) => any() | {custom_sort_value :: String.t(), value :: any()}
}), }),
optional(:inital_key) => atom(),
optional(:initial_sort_mode) => atom(),
optional(any()) => any() optional(any()) => any()
}, },
Socket.t() Socket.t()
) :: {:ok, Socket.t()} ) :: {:ok, Socket.t()}
def update(%{columns: columns, rows: rows} = assigns, socket) do def update(%{columns: columns, rows: rows} = assigns, socket) do
initial_key = columns |> List.first() |> Map.get(:key) initial_key =
rows = rows |> Enum.sort_by(fn row -> row |> Map.get(initial_key) end, :asc) if assigns |> Map.has_key?(:initial_key) do
assigns.initial_key
else
columns |> List.first() |> Map.get(:key)
end
initial_sort_mode =
if assigns |> Map.has_key?(:initial_sort_mode) do
assigns.initial_sort_mode
else
:asc
end
rows = rows |> Enum.sort_by(fn row -> row |> Map.get(initial_key) end, initial_sort_mode)
socket = socket =
socket socket
|> assign(assigns) |> assign(assigns)
|> assign(columns: columns, rows: rows, last_sort_key: initial_key, sort_mode: :asc) |> assign(
columns: columns,
rows: rows,
last_sort_key: initial_key,
sort_mode: initial_sort_mode
)
{:ok, socket} {:ok, socket}
end end