Refactor column visibility logic
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Rafael Epplée 2025-12-02 12:16:02 +01:00
parent d039e4bb7d
commit d3a346824e
No known key found for this signature in database
GPG key ID: B4EFE6DC59FAE118

View file

@ -74,9 +74,7 @@ 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
@ -736,11 +734,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.
@ -748,43 +745,23 @@ 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 || %{})
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, 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.