- Replace Ash.get!/Ash.destroy! with Ash.get/Ash.destroy - Add case statements for Forbidden, NotFound, and generic errors - Display user-friendly flash messages for all error cases - Use Enum.map_join/3 for efficient error formatting
132 lines
4 KiB
Elixir
132 lines
4 KiB
Elixir
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"""
|
|
<Layouts.app flash={@flash} current_user={@current_user}>
|
|
<.header>
|
|
Listing Custom field values
|
|
<:actions>
|
|
<.button variant="primary" navigate={~p"/custom_field_values/new"}>
|
|
<.icon name="hero-plus" /> New Custom field value
|
|
</.button>
|
|
</:actions>
|
|
</.header>
|
|
|
|
<.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}</:col>
|
|
|
|
<:action :let={{_id, custom_field_value}}>
|
|
<div class="sr-only">
|
|
<.link navigate={~p"/custom_field_values/#{custom_field_value}"}>Show</.link>
|
|
</div>
|
|
|
|
<.link navigate={~p"/custom_field_values/#{custom_field_value}/edit"}>Edit</.link>
|
|
</:action>
|
|
|
|
<: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
|
|
</.link>
|
|
</:action>
|
|
</.table>
|
|
</Layouts.app>
|
|
"""
|
|
end
|
|
|
|
@impl true
|
|
def mount(_params, _session, socket) do
|
|
actor = current_actor(socket)
|
|
|
|
{:ok,
|
|
socket
|
|
|> assign(:page_title, "Listing Custom field values")
|
|
|> stream(:custom_field_values, Ash.read!(Mv.Membership.CustomFieldValue, actor: actor))}
|
|
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
|