defmodule MvWeb.CustomFieldValueLive.Index do @moduledoc """ LiveView for displaying and managing custom field values. ## Features - List all custom field values with their values and types - Show which member each custom field value belongs to - Display custom field information - Navigate to custom field value details and edit forms - Delete custom field values ## Relationships Each custom field value is linked to: - A member (the custom field value owner) - A custom field (defining value type and behavior) ## Events - `delete` - Remove a custom field value from the database ## Note Custom field values are typically managed through the member edit form. This view provides a global overview of all custom field values. """ use MvWeb, :live_view on_mount {MvWeb.LiveHelpers, :ensure_user_role_loaded} import MvWeb.LiveHelpers, only: [current_actor: 1] @impl true def render(assigns) do ~H""" <.header> Listing Custom field values <:actions> <.button variant="primary" navigate={~p"/custom_field_values/new"}> <.icon name="hero-plus" /> New Custom field value <.table id="custom_field_values" rows={@streams.custom_field_values} row_click={ fn {_id, custom_field_value} -> JS.navigate(~p"/custom_field_values/#{custom_field_value}") end } > <:col :let={{_id, custom_field_value}} label="Id">{custom_field_value.id} <:action :let={{_id, custom_field_value}}>
<.link navigate={~p"/custom_field_values/#{custom_field_value}"}>Show
<.link navigate={~p"/custom_field_values/#{custom_field_value}/edit"}>Edit <:action :let={{id, custom_field_value}}> <.link phx-click={JS.push("delete", value: %{id: custom_field_value.id}) |> hide("##{id}")} data-confirm="Are you sure?" > Delete
""" end @impl true def mount(_params, _session, socket) do actor = current_actor(socket) # Early return if no actor (prevents exceptions in unauthenticated tests) if is_nil(actor) do {:ok, socket |> assign(:page_title, "Listing Custom field values") |> stream(:custom_field_values, [])} else case Ash.read(Mv.Membership.CustomFieldValue, actor: actor) do {:ok, custom_field_values} -> {:ok, socket |> assign(:page_title, "Listing Custom field values") |> stream(:custom_field_values, custom_field_values)} {:error, %Ash.Error.Forbidden{}} -> {:ok, socket |> assign(:page_title, "Listing Custom field values") |> stream(:custom_field_values, []) |> put_flash(:error, gettext("You do not have permission to view custom field values"))} {:error, error} -> {:ok, socket |> assign(:page_title, "Listing Custom field values") |> stream(:custom_field_values, []) |> put_flash(:error, format_error(error))} end end end @impl true def handle_event("delete", %{"id" => id}, socket) do actor = MvWeb.LiveHelpers.current_actor(socket) case Ash.get(Mv.Membership.CustomFieldValue, id, actor: actor) do {:ok, custom_field_value} -> case Ash.destroy(custom_field_value, actor: actor) do :ok -> {:noreply, socket |> stream_delete(:custom_field_values, custom_field_value) |> put_flash(:info, gettext("Custom field value deleted successfully"))} {:error, %Ash.Error.Forbidden{}} -> {:noreply, put_flash( socket, :error, gettext("You do not have permission to delete this custom field value") )} {:error, error} -> {:noreply, put_flash(socket, :error, format_error(error))} end {:error, %Ash.Error.Query.NotFound{}} -> {:noreply, put_flash(socket, :error, gettext("Custom field value not found"))} {:error, %Ash.Error.Forbidden{} = _error} -> {:noreply, put_flash( socket, :error, gettext("You do not have permission to access this custom field value") )} {:error, error} -> {:noreply, put_flash(socket, :error, format_error(error))} end end defp format_error(%Ash.Error.Invalid{errors: errors}) do Enum.map_join(errors, ", ", fn %{message: message} -> message end) end defp format_error(error) do inspect(error) end end