format and linting: reduced complexity of function
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
carla 2025-10-24 11:25:32 +02:00
parent fcf737f66f
commit 660d587cc1
2 changed files with 44 additions and 40 deletions

View file

@ -237,40 +237,8 @@ defmodule MvWeb.MemberLive.Index do
# Function to maybe update the sort
defp maybe_update_sort(socket, %{"sort_field" => sf, "sort_order" => so}) do
# Handle empty strings and nil values
field =
case sf do
"" ->
socket.assigns.sort_field
nil ->
socket.assigns.sort_field
sf when is_binary(sf) ->
try do
String.to_existing_atom(sf)
rescue
ArgumentError -> socket.assigns.sort_field
end
sf when is_atom(sf) ->
sf
_ ->
socket.assigns.sort_field
end
# Validate that the field is actually sortable
field = if valid_sort_field?(field), do: field, else: socket.assigns.sort_field
# Handle empty strings and nil values for sort order
order =
case so do
"" -> socket.assigns.sort_order
nil -> socket.assigns.sort_order
so when so in ["asc", "desc"] -> String.to_atom(so)
_ -> socket.assigns.sort_order
end
field = determine_field(socket.assigns.sort_field, sf)
order = determine_order(socket.assigns.sort_order, so)
socket
|> assign(:sort_field, field)
@ -279,6 +247,42 @@ defmodule MvWeb.MemberLive.Index do
defp maybe_update_sort(socket, _), do: socket
defp determine_field(default, sf) do
case sf do
"" ->
default
nil ->
default
sf when is_binary(sf) ->
sf
|> String.to_existing_atom()
|> handle_atom_conversion(default)
sf when is_atom(sf) ->
handle_atom_conversion(sf, default)
_ ->
default
end
end
defp handle_atom_conversion(val, default) when is_atom(val) do
if valid_sort_field?(val), do: val, else: default
end
defp handle_atom_conversion(_, default), do: default
defp determine_order(default, so) do
case so do
"" -> default
nil -> default
so when so in ["asc", "desc"] -> String.to_atom(so)
_ -> default
end
end
# Function to update search parameters
defp maybe_update_search(socket, %{"query" => query}) when query != "" do
assign(socket, :query, query)

View file

@ -47,7 +47,7 @@ defmodule MvWeb.Components.SortHeaderComponentTest do
describe "sort icons" do
test "shows neutral icon for specific field when not sorted", %{conn: conn} do
conn = conn_with_oidc_user(conn)
{:ok, view, html} = live(conn, "/members")
{:ok, view, _html} = live(conn, "/members")
# The neutral icon has the opcity class we can test for
# Test that EMAIL field specifically shows neutral icon
@ -68,23 +68,23 @@ defmodule MvWeb.Components.SortHeaderComponentTest do
# Test that OTHER fields still show neutral icons
assert has_element?(view, "[data-testid='first_name'] .opacity-40")
# Test HTML content - should contain chevron-up AND chevron-up-down
# Test HTML content - should contain chevronup AND chevron up down
assert html =~ "hero-chevron-up"
assert html =~ "hero-chevron-up-down"
# Count occurrences to ensure only one ascending icon
up_count = html |> String.split("hero-chevron-up ") |> length() |> Kernel.-(1)
# Should be exactly one chevron-up icon
# Should be exactly one chevronup icon
assert up_count == 1
end
test "shows descending icon for specific field when sorted descending", %{conn: conn} do
conn = conn_with_oidc_user(conn)
{:ok, view, html} = live(conn, "/members?query=&sort_field=email&sort_order=desc")
{:ok, _view, html} = live(conn, "/members?query=&sort_field=email&sort_order=desc")
# Count occurrences to ensure only one descending icon
down_count = html |> String.split("hero-chevron-down ") |> length() |> Kernel.-(1)
# Should be exactly one chevron-down icon
# Should be exactly one chevrondown icon
assert down_count == 1
end
@ -237,7 +237,7 @@ defmodule MvWeb.Components.SortHeaderComponentTest do
conn = conn_with_oidc_user(conn)
{:ok, view, _html} = live(conn, "/members")
# Click on the first_name sort header
# Click on the first name sort header
view
|> element("button[phx-value-field='first_name']")
|> render_click()