fix: show custom field input fields also when empty
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
def399122c
commit
5718a37aca
1 changed files with 41 additions and 22 deletions
|
|
@ -131,15 +131,26 @@ defmodule MvWeb.MemberLive.Show do
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<%!-- Custom Fields Section --%>
|
<%!-- Custom Fields Section --%>
|
||||||
<%= if Enum.any?(@member.custom_field_values) do %>
|
<%= if Enum.any?(@custom_fields_with_values) do %>
|
||||||
<div>
|
<div>
|
||||||
<.section_box title={gettext("Custom Fields")}>
|
<.section_box title={gettext("Custom Fields")}>
|
||||||
<div class="grid grid-cols-2 gap-4">
|
<div class="grid grid-cols-2 gap-4">
|
||||||
<%= for cfv <- sort_custom_field_values(@member.custom_field_values) do %>
|
<%= for {custom_field, cfv} <- @custom_fields_with_values do %>
|
||||||
<% custom_field = cfv.custom_field %>
|
<.data_field label={custom_field.name}>
|
||||||
<% value_type = custom_field && custom_field.value_type %>
|
<%= if cfv && cfv.value do %>
|
||||||
<.data_field label={custom_field && custom_field.name}>
|
<%= if custom_field.value_type == :email && cfv.value.value && String.trim(cfv.value.value) != "" do %>
|
||||||
{format_custom_field_value(cfv.value, value_type)}
|
<a
|
||||||
|
href={"mailto:#{cfv.value.value}"}
|
||||||
|
class="text-blue-700 hover:text-blue-800 underline"
|
||||||
|
>
|
||||||
|
{cfv.value.value}
|
||||||
|
</a>
|
||||||
|
<% else %>
|
||||||
|
{format_custom_field_value(cfv.value, custom_field.value_type)}
|
||||||
|
<% end %>
|
||||||
|
<% else %>
|
||||||
|
{format_custom_field_value(nil, custom_field.value_type)}
|
||||||
|
<% end %>
|
||||||
</.data_field>
|
</.data_field>
|
||||||
<% end %>
|
<% end %>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -175,7 +186,7 @@ defmodule MvWeb.MemberLive.Show do
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def mount(_params, _session, socket) do
|
def mount(_params, _session, socket) do
|
||||||
{:ok, socket}
|
{:ok, assign(socket, :custom_fields_with_values, [])}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
|
|
@ -187,10 +198,28 @@ defmodule MvWeb.MemberLive.Show do
|
||||||
|
|
||||||
member = Ash.read_one!(query)
|
member = Ash.read_one!(query)
|
||||||
|
|
||||||
|
# Load all custom fields to display all of them, even if they have no values
|
||||||
|
{:ok, custom_fields} = Mv.Membership.list_custom_fields()
|
||||||
|
|
||||||
|
# Create a map of custom_field_id -> custom_field_value for quick lookup
|
||||||
|
custom_field_values_map =
|
||||||
|
member.custom_field_values
|
||||||
|
|> Enum.map(fn cfv -> {cfv.custom_field_id, cfv} end)
|
||||||
|
|> Map.new()
|
||||||
|
|
||||||
|
# Match all custom fields with their values (if they exist)
|
||||||
|
custom_fields_with_values =
|
||||||
|
Enum.map(custom_fields, fn cf ->
|
||||||
|
cfv = Map.get(custom_field_values_map, cf.id)
|
||||||
|
{cf, cfv}
|
||||||
|
end)
|
||||||
|
|> Enum.sort_by(fn {cf, _cfv} -> cf.name end)
|
||||||
|
|
||||||
{:noreply,
|
{:noreply,
|
||||||
socket
|
socket
|
||||||
|> assign(:page_title, page_title(socket.assigns.live_action))
|
|> assign(:page_title, page_title(socket.assigns.live_action))
|
||||||
|> assign(:member, member)}
|
|> assign(:member, member)
|
||||||
|
|> assign(:custom_fields_with_values, custom_fields_with_values)}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp page_title(:show), do: gettext("Show Member")
|
defp page_title(:show), do: gettext("Show Member")
|
||||||
|
|
@ -272,19 +301,13 @@ defmodule MvWeb.MemberLive.Show do
|
||||||
|
|
||||||
defp format_date(date), do: to_string(date)
|
defp format_date(date), do: to_string(date)
|
||||||
|
|
||||||
# Sorts custom field values by custom field name
|
|
||||||
defp sort_custom_field_values(custom_field_values) do
|
|
||||||
Enum.sort_by(custom_field_values, fn cfv ->
|
|
||||||
(cfv.custom_field && cfv.custom_field.name) || ""
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Formats custom field value based on type
|
# Formats custom field value based on type
|
||||||
|
# Returns empty string for nil/empty values (consistent with member fields behavior)
|
||||||
defp format_custom_field_value(%Ash.Union{value: value, type: type}, _expected_type) do
|
defp format_custom_field_value(%Ash.Union{value: value, type: type}, _expected_type) do
|
||||||
format_custom_field_value(value, type)
|
format_custom_field_value(value, type)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp format_custom_field_value(nil, _type), do: "—"
|
defp format_custom_field_value(nil, _type), do: ""
|
||||||
|
|
||||||
defp format_custom_field_value(value, :boolean) when is_boolean(value) do
|
defp format_custom_field_value(value, :boolean) when is_boolean(value) do
|
||||||
if value, do: gettext("Yes"), else: gettext("No")
|
if value, do: gettext("Yes"), else: gettext("No")
|
||||||
|
|
@ -295,11 +318,7 @@ defmodule MvWeb.MemberLive.Show do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp format_custom_field_value(value, :email) when is_binary(value) do
|
defp format_custom_field_value(value, :email) when is_binary(value) do
|
||||||
assigns = %{email: value}
|
if String.trim(value) == "", do: "", else: value
|
||||||
|
|
||||||
~H"""
|
|
||||||
<a href={"mailto:#{@email}"} class="text-blue-700 hover:text-blue-800 underline">{@email}</a>
|
|
||||||
"""
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp format_custom_field_value(value, :integer) when is_integer(value) do
|
defp format_custom_field_value(value, :integer) when is_integer(value) do
|
||||||
|
|
@ -307,7 +326,7 @@ defmodule MvWeb.MemberLive.Show do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp format_custom_field_value(value, _type) when is_binary(value) do
|
defp format_custom_field_value(value, _type) when is_binary(value) do
|
||||||
if String.trim(value) == "", do: "—", else: value
|
if String.trim(value) == "", do: "", else: value
|
||||||
end
|
end
|
||||||
|
|
||||||
defp format_custom_field_value(value, _type), do: to_string(value)
|
defp format_custom_field_value(value, _type), do: to_string(value)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue