diff --git a/lib/mv_web/live/member_live/index.ex b/lib/mv_web/live/member_live/index.ex index 5bbb16d..6bce495 100644 --- a/lib/mv_web/live/member_live/index.ex +++ b/lib/mv_web/live/member_live/index.ex @@ -74,7 +74,9 @@ defmodule MvWeb.MemberLive.Index do |> assign_new(:sort_field, fn -> :first_name end) |> assign_new(:sort_order, fn -> :asc end) |> assign(:selected_members, []) + |> assign(:settings, settings) |> assign(:custom_fields_visible, custom_fields_visible) + |> assign(:member_field_configurations, get_member_field_configurations(settings)) |> assign(:member_fields_visible, get_visible_member_fields(settings)) # We call handle params to use the query from the URL @@ -734,10 +736,11 @@ defmodule MvWeb.MemberLive.Index do end end - # Gets the list of member fields that should be visible in the overview. + # Gets the configuration for all member fields with their show_in_overview values. # - # Reads the visibility configuration from Settings and returns only the fields - # where show_in_overview is true. Fields not configured in settings default to true. + # Reads the visibility configuration from Settings and returns a map with all member fields + # and their show_in_overview values (true or false). Fields not configured in settings + # default to true. # # Performance: This function uses the already-loaded settings to avoid N+1 queries. # Settings should be loaded once in mount/3 and passed to this function. @@ -745,23 +748,43 @@ defmodule MvWeb.MemberLive.Index do # Parameters: # - `settings` - The settings struct loaded from the database # - # Returns a list of atoms representing visible member field names. + # Returns a map: %{field_name => show_in_overview} + # + # This can be used for: + # - Rendering the overview (filtering visible fields) + # - UI configuration dropdowns (showing all fields with their current state) + # - Dynamic field management # # Fields are read from the global Constants module. - @spec get_visible_member_fields(map()) :: [atom()] - defp get_visible_member_fields(settings) do + @spec get_member_field_configurations(map()) :: %{atom() => boolean()} + defp get_member_field_configurations(settings) do # Get all eligible fields from the global constants all_fields = Mv.Constants.member_fields() # Normalize visibility config (JSONB may return string keys) visibility_config = normalize_visibility_config(settings.member_field_visibility || %{}) - # Filter to only return visible fields - Enum.filter(all_fields, fn field -> - Map.get(visibility_config, field, true) + Enum.reduce(all_fields, %{}, fn field, acc -> + show_in_overview = Map.get(visibility_config, field, true) + Map.put(acc, field, show_in_overview) end) end + # Gets the list of member fields that should be visible in the overview. + # + # Filters the member field configurations to return only fields with show_in_overview: true. + # + # Parameters: + # - `settings` - The settings struct loaded from the database + # + # Returns a list of atoms representing visible member field names. + @spec get_visible_member_fields(map()) :: [atom()] + defp get_visible_member_fields(settings) do + get_member_field_configurations(settings) + |> Enum.filter(fn {_field, show_in_overview} -> show_in_overview end) + |> Enum.map(fn {field, _show_in_overview} -> field end) + end + # Normalizes visibility config map keys from strings to atoms. # JSONB in PostgreSQL converts atom keys to string keys when storing. # This is a local helper to avoid N+1 queries by reusing the normalization logic.