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
d039e4bb7d
commit
d3a346824e
1 changed files with 9 additions and 32 deletions
|
|
@ -74,9 +74,7 @@ defmodule MvWeb.MemberLive.Index do
|
||||||
|> assign_new(:sort_field, fn -> :first_name end)
|
|> assign_new(:sort_field, fn -> :first_name end)
|
||||||
|> assign_new(:sort_order, fn -> :asc end)
|
|> assign_new(:sort_order, fn -> :asc end)
|
||||||
|> assign(:selected_members, [])
|
|> assign(:selected_members, [])
|
||||||
|> assign(:settings, settings)
|
|
||||||
|> assign(:custom_fields_visible, custom_fields_visible)
|
|> 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))
|
|> assign(:member_fields_visible, get_visible_member_fields(settings))
|
||||||
|
|
||||||
# We call handle params to use the query from the URL
|
# We call handle params to use the query from the URL
|
||||||
|
|
@ -736,11 +734,10 @@ defmodule MvWeb.MemberLive.Index do
|
||||||
end
|
end
|
||||||
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
|
# Reads the visibility configuration from Settings and returns only the fields
|
||||||
# and their show_in_overview values (true or false). Fields not configured in settings
|
# where show_in_overview is true. Fields not configured in settings default to true.
|
||||||
# default to true.
|
|
||||||
#
|
#
|
||||||
# Performance: This function uses the already-loaded settings to avoid N+1 queries.
|
# 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.
|
# Settings should be loaded once in mount/3 and passed to this function.
|
||||||
|
|
@ -748,43 +745,23 @@ defmodule MvWeb.MemberLive.Index do
|
||||||
# Parameters:
|
# Parameters:
|
||||||
# - `settings` - The settings struct loaded from the database
|
# - `settings` - The settings struct loaded from the database
|
||||||
#
|
#
|
||||||
# Returns a map: %{field_name => show_in_overview}
|
# Returns a list of atoms representing visible member field names.
|
||||||
#
|
|
||||||
# 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.
|
# Fields are read from the global Constants module.
|
||||||
@spec get_member_field_configurations(map()) :: %{atom() => boolean()}
|
@spec get_visible_member_fields(map()) :: [atom()]
|
||||||
defp get_member_field_configurations(settings) do
|
defp get_visible_member_fields(settings) do
|
||||||
# Get all eligible fields from the global constants
|
# Get all eligible fields from the global constants
|
||||||
all_fields = Mv.Constants.member_fields()
|
all_fields = Mv.Constants.member_fields()
|
||||||
|
|
||||||
# Normalize visibility config (JSONB may return string keys)
|
# Normalize visibility config (JSONB may return string keys)
|
||||||
visibility_config = normalize_visibility_config(settings.member_field_visibility || %{})
|
visibility_config = normalize_visibility_config(settings.member_field_visibility || %{})
|
||||||
|
|
||||||
Enum.reduce(all_fields, %{}, fn field, acc ->
|
# Filter to only return visible fields
|
||||||
show_in_overview = Map.get(visibility_config, field, true)
|
Enum.filter(all_fields, fn field ->
|
||||||
Map.put(acc, field, show_in_overview)
|
Map.get(visibility_config, field, true)
|
||||||
end)
|
end)
|
||||||
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.
|
# Normalizes visibility config map keys from strings to atoms.
|
||||||
# JSONB in PostgreSQL converts atom keys to string keys when storing.
|
# 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.
|
# This is a local helper to avoid N+1 queries by reusing the normalization logic.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue