Refactor column visibility logic
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
dce2053ce7
commit
13f77b5c0a
6 changed files with 43 additions and 240 deletions
|
|
@ -76,7 +76,6 @@ defmodule MvWeb.MemberLive.Index do
|
|||
|> assign_new(:sort_order, fn -> :asc end)
|
||||
|> assign(:selected_members, MapSet.new())
|
||||
|> 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
|
||||
|
|
@ -798,11 +797,10 @@ defmodule MvWeb.MemberLive.Index do
|
|||
end
|
||||
end
|
||||
|
||||
# Gets the configuration for all member fields with their show_in_overview values.
|
||||
# Gets the list of member fields that should be visible in the overview.
|
||||
#
|
||||
# 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.
|
||||
# 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.
|
||||
#
|
||||
# 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.
|
||||
|
|
@ -810,64 +808,20 @@ defmodule MvWeb.MemberLive.Index do
|
|||
# Parameters:
|
||||
# - `settings` - The settings struct loaded from the database
|
||||
#
|
||||
# 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
|
||||
# Returns a list of atoms representing visible member field names.
|
||||
#
|
||||
# Fields are read from the global Constants module.
|
||||
@spec get_member_field_configurations(map()) :: %{atom() => boolean()}
|
||||
defp get_member_field_configurations(settings) do
|
||||
@spec get_visible_member_fields(map()) :: [atom()]
|
||||
defp get_visible_member_fields(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 || %{})
|
||||
# JSONB stores keys as strings
|
||||
visibility_config = settings.member_field_visibility || %{}
|
||||
|
||||
Enum.reduce(all_fields, %{}, fn field, acc ->
|
||||
show_in_overview = Map.get(visibility_config, field, true)
|
||||
Map.put(acc, field, show_in_overview)
|
||||
# Filter to only return visible fields
|
||||
Enum.filter(all_fields, fn field ->
|
||||
Map.get(visibility_config, Atom.to_string(field), true)
|
||||
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.
|
||||
defp normalize_visibility_config(config) when is_map(config) do
|
||||
Enum.reduce(config, %{}, fn
|
||||
{key, value}, acc when is_atom(key) ->
|
||||
Map.put(acc, key, value)
|
||||
|
||||
{key, value}, acc when is_binary(key) ->
|
||||
try do
|
||||
atom_key = String.to_existing_atom(key)
|
||||
Map.put(acc, atom_key, value)
|
||||
rescue
|
||||
ArgumentError ->
|
||||
acc
|
||||
end
|
||||
|
||||
_, acc ->
|
||||
acc
|
||||
end)
|
||||
end
|
||||
|
||||
defp normalize_visibility_config(_), do: %{}
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue