formating
This commit is contained in:
parent
41e3a52482
commit
bb362e1636
3 changed files with 78 additions and 28 deletions
|
|
@ -207,41 +207,65 @@ defmodule MvWeb.MemberLive.Index do
|
||||||
|
|
||||||
# Function to sort the column if needed
|
# Function to sort the column if needed
|
||||||
defp maybe_sort(query, nil, _), do: query
|
defp maybe_sort(query, nil, _), do: query
|
||||||
defp maybe_sort(query, field, :asc) when not is_nil(field), do: Ash.Query.sort(query, [{field, :asc}])
|
|
||||||
defp maybe_sort(query, field, :desc) when not is_nil(field), do: Ash.Query.sort(query, [{field, :desc}])
|
defp maybe_sort(query, field, :asc) when not is_nil(field),
|
||||||
|
do: Ash.Query.sort(query, [{field, :asc}])
|
||||||
|
|
||||||
|
defp maybe_sort(query, field, :desc) when not is_nil(field),
|
||||||
|
do: Ash.Query.sort(query, [{field, :desc}])
|
||||||
|
|
||||||
defp maybe_sort(query, _, _), do: query
|
defp maybe_sort(query, _, _), do: query
|
||||||
|
|
||||||
# Validate that a field is sortable
|
# Validate that a field is sortable
|
||||||
defp valid_sort_field?(field) when is_atom(field) do
|
defp valid_sort_field?(field) when is_atom(field) do
|
||||||
valid_fields = [
|
valid_fields = [
|
||||||
:first_name, :last_name, :email, :street, :house_number,
|
:first_name,
|
||||||
:postal_code, :city, :phone_number, :join_date
|
:last_name,
|
||||||
|
:email,
|
||||||
|
:street,
|
||||||
|
:house_number,
|
||||||
|
:postal_code,
|
||||||
|
:city,
|
||||||
|
:phone_number,
|
||||||
|
:join_date
|
||||||
]
|
]
|
||||||
|
|
||||||
field in valid_fields
|
field in valid_fields
|
||||||
end
|
end
|
||||||
|
|
||||||
defp valid_sort_field?(_), do: false
|
defp valid_sort_field?(_), do: false
|
||||||
|
|
||||||
# Function to maybe update the sort
|
# Function to maybe update the sort
|
||||||
defp maybe_update_sort(socket, %{"sort_field" => sf, "sort_order" => so}) do
|
defp maybe_update_sort(socket, %{"sort_field" => sf, "sort_order" => so}) do
|
||||||
# Handle empty strings and nil values
|
# Handle empty strings and nil values
|
||||||
field = case sf do
|
field =
|
||||||
"" -> socket.assigns.sort_field
|
case sf do
|
||||||
nil -> socket.assigns.sort_field
|
"" ->
|
||||||
|
socket.assigns.sort_field
|
||||||
|
|
||||||
|
nil ->
|
||||||
|
socket.assigns.sort_field
|
||||||
|
|
||||||
sf when is_binary(sf) ->
|
sf when is_binary(sf) ->
|
||||||
try do
|
try do
|
||||||
String.to_existing_atom(sf)
|
String.to_existing_atom(sf)
|
||||||
rescue
|
rescue
|
||||||
ArgumentError -> socket.assigns.sort_field
|
ArgumentError -> socket.assigns.sort_field
|
||||||
end
|
end
|
||||||
sf when is_atom(sf) -> sf
|
|
||||||
_ -> socket.assigns.sort_field
|
sf when is_atom(sf) ->
|
||||||
|
sf
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
socket.assigns.sort_field
|
||||||
end
|
end
|
||||||
|
|
||||||
# Validate that the field is actually sortable
|
# Validate that the field is actually sortable
|
||||||
field = if valid_sort_field?(field), do: field, else: socket.assigns.sort_field
|
field = if valid_sort_field?(field), do: field, else: socket.assigns.sort_field
|
||||||
|
|
||||||
# Handle empty strings and nil values for sort order
|
# Handle empty strings and nil values for sort order
|
||||||
order = case so do
|
order =
|
||||||
|
case so do
|
||||||
"" -> socket.assigns.sort_order
|
"" -> socket.assigns.sort_order
|
||||||
nil -> socket.assigns.sort_order
|
nil -> socket.assigns.sort_order
|
||||||
so when so in ["asc", "desc"] -> String.to_atom(so)
|
so when so in ["asc", "desc"] -> String.to_atom(so)
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,16 @@ defmodule MvWeb.Components.SortHeaderComponentTest do
|
||||||
conn = conn_with_oidc_user(conn)
|
conn = conn_with_oidc_user(conn)
|
||||||
{:ok, view, _html} = live(conn, "/members")
|
{:ok, view, _html} = live(conn, "/members")
|
||||||
|
|
||||||
sortable_fields = [:first_name, :email, :street, :house_number, :postal_code, :city, :phone_number, :join_date]
|
sortable_fields = [
|
||||||
|
:first_name,
|
||||||
|
:email,
|
||||||
|
:street,
|
||||||
|
:house_number,
|
||||||
|
:postal_code,
|
||||||
|
:city,
|
||||||
|
:phone_number,
|
||||||
|
:join_date
|
||||||
|
]
|
||||||
|
|
||||||
for field <- sortable_fields do
|
for field <- sortable_fields do
|
||||||
assert has_element?(view, "button[phx-value-field='#{field}']")
|
assert has_element?(view, "button[phx-value-field='#{field}']")
|
||||||
|
|
@ -65,7 +74,8 @@ defmodule MvWeb.Components.SortHeaderComponentTest do
|
||||||
|
|
||||||
# Count occurrences to ensure only one ascending icon
|
# Count occurrences to ensure only one ascending icon
|
||||||
up_count = html |> String.split("hero-chevron-up ") |> length() |> Kernel.-(1)
|
up_count = html |> String.split("hero-chevron-up ") |> length() |> Kernel.-(1)
|
||||||
assert up_count == 1 # Should be exactly one chevron-up icon
|
# Should be exactly one chevron-up icon
|
||||||
|
assert up_count == 1
|
||||||
end
|
end
|
||||||
|
|
||||||
test "shows descending icon for specific field when sorted descending", %{conn: conn} do
|
test "shows descending icon for specific field when sorted descending", %{conn: conn} do
|
||||||
|
|
@ -74,7 +84,8 @@ defmodule MvWeb.Components.SortHeaderComponentTest do
|
||||||
|
|
||||||
# Count occurrences to ensure only one descending icon
|
# Count occurrences to ensure only one descending icon
|
||||||
down_count = html |> String.split("hero-chevron-down ") |> length() |> Kernel.-(1)
|
down_count = html |> String.split("hero-chevron-down ") |> length() |> Kernel.-(1)
|
||||||
assert down_count == 1 # Should be exactly one chevron-down icon
|
# Should be exactly one chevron-down icon
|
||||||
|
assert down_count == 1
|
||||||
end
|
end
|
||||||
|
|
||||||
test "multiple fields can have different icon states", %{conn: conn} do
|
test "multiple fields can have different icon states", %{conn: conn} do
|
||||||
|
|
@ -146,7 +157,9 @@ defmodule MvWeb.Components.SortHeaderComponentTest do
|
||||||
{:ok, _view, html_neutral} = live(conn, "/members")
|
{:ok, _view, html_neutral} = live(conn, "/members")
|
||||||
|
|
||||||
# Count neutral icons (should be 7 - one for each field)
|
# Count neutral icons (should be 7 - one for each field)
|
||||||
neutral_count = html_neutral |> String.split("hero-chevron-up-down") |> length() |> Kernel.-(1)
|
neutral_count =
|
||||||
|
html_neutral |> String.split("hero-chevron-up-down") |> length() |> Kernel.-(1)
|
||||||
|
|
||||||
assert neutral_count == 7
|
assert neutral_count == 7
|
||||||
|
|
||||||
# Count active icons (should be 1)
|
# Count active icons (should be 1)
|
||||||
|
|
@ -209,7 +222,12 @@ defmodule MvWeb.Components.SortHeaderComponentTest do
|
||||||
|
|
||||||
# Test aria-labels for different fields
|
# Test aria-labels for different fields
|
||||||
assert has_element?(view, "button[phx-value-field='email'][aria-label='descending']")
|
assert has_element?(view, "button[phx-value-field='email'][aria-label='descending']")
|
||||||
assert has_element?(view, "button[phx-value-field='first_name'][aria-label='Click to sort']")
|
|
||||||
|
assert has_element?(
|
||||||
|
view,
|
||||||
|
"button[phx-value-field='first_name'][aria-label='Click to sort']"
|
||||||
|
)
|
||||||
|
|
||||||
assert has_element?(view, "button[phx-value-field='city'][aria-label='Click to sort']")
|
assert has_element?(view, "button[phx-value-field='city'][aria-label='Click to sort']")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -115,7 +115,15 @@ defmodule MvWeb.MemberLive.IndexTest do
|
||||||
# default ascending sorting with first name
|
# default ascending sorting with first name
|
||||||
assert has_element?(view, "[data-testid='first_name'][aria-label='ascending']")
|
assert has_element?(view, "[data-testid='first_name'][aria-label='ascending']")
|
||||||
|
|
||||||
sortable_fields = [:email, :street, :house_number, :postal_code, :city, :phone_number, :join_date]
|
sortable_fields = [
|
||||||
|
:email,
|
||||||
|
:street,
|
||||||
|
:house_number,
|
||||||
|
:postal_code,
|
||||||
|
:city,
|
||||||
|
:phone_number,
|
||||||
|
:join_date
|
||||||
|
]
|
||||||
|
|
||||||
for field <- sortable_fields do
|
for field <- sortable_fields do
|
||||||
view
|
view
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue