From 9a7622ebed67d0e3098648a1c3d489eee3ad681e Mon Sep 17 00:00:00 2001 From: Moritz Date: Thu, 29 Jan 2026 13:59:24 +0100 Subject: [PATCH] fix: pass actor to CustomFieldLive.FormComponent for save IndexComponent now passes actor to FormComponent; FormComponent uses assigns[:actor] instead of current_actor(socket). Add test that submits new custom field form on settings page. --- .../live/custom_field_live/form_component.ex | 3 +- .../live/custom_field_live/index_component.ex | 1 + .../live/custom_field_live/deletion_test.exs | 41 +++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/lib/mv_web/live/custom_field_live/form_component.ex b/lib/mv_web/live/custom_field_live/form_component.ex index 54ef4a7..b809a1a 100644 --- a/lib/mv_web/live/custom_field_live/form_component.ex +++ b/lib/mv_web/live/custom_field_live/form_component.ex @@ -91,7 +91,8 @@ defmodule MvWeb.CustomFieldLive.FormComponent do @impl true def handle_event("save", %{"custom_field" => custom_field_params}, socket) do - actor = MvWeb.LiveHelpers.current_actor(socket) + # Actor must be passed from parent (IndexComponent); component socket has no current_user + actor = socket.assigns[:actor] case MvWeb.LiveHelpers.submit_form(socket.assigns.form, custom_field_params, actor) do {:ok, custom_field} -> diff --git a/lib/mv_web/live/custom_field_live/index_component.ex b/lib/mv_web/live/custom_field_live/index_component.ex index ddde324..1e0bb3f 100644 --- a/lib/mv_web/live/custom_field_live/index_component.ex +++ b/lib/mv_web/live/custom_field_live/index_component.ex @@ -38,6 +38,7 @@ defmodule MvWeb.CustomFieldLive.IndexComponent do <.live_component module={MvWeb.CustomFieldLive.FormComponent} id={@form_id} + actor={@actor} custom_field={@editing_custom_field} on_save={ fn custom_field, action -> send(self(), {:custom_field_saved, custom_field, action}) end diff --git a/test/mv_web/live/custom_field_live/deletion_test.exs b/test/mv_web/live/custom_field_live/deletion_test.exs index 2490601..57a75e3 100644 --- a/test/mv_web/live/custom_field_live/deletion_test.exs +++ b/test/mv_web/live/custom_field_live/deletion_test.exs @@ -15,6 +15,7 @@ defmodule MvWeb.CustomFieldLive.DeletionTest do use MvWeb.ConnCase, async: true import Phoenix.LiveViewTest + require Ash.Query alias Mv.Membership.{CustomField, CustomFieldValue, Member} @@ -230,6 +231,46 @@ defmodule MvWeb.CustomFieldLive.DeletionTest do end end + describe "create custom field" do + test "submitting new data field form creates custom field and shows success", %{conn: conn} do + {:ok, view, _html} = live(conn, ~p"/settings") + + # Open "New Data Field" form + view + |> element("#custom-fields-component button", "New Data Field") + |> render_click() + + # Form is visible; submit with valid data + form_params = %{ + "custom_field" => %{ + "name" => "Created via Form", + "value_type" => "string", + "description" => "", + "required" => "false", + "show_in_overview" => "true" + } + } + + view + |> form("#custom-field-form-new-form", form_params) + |> render_submit() + + # Success flash (FormComponent needs actor from parent; without it KeyError would occur) + assert render(view) =~ "successfully" + + # Custom field was created in DB + system_actor = Mv.Helpers.SystemActor.get_system_actor() + search_name = "Created via Form" + + [custom_field] = + Mv.Membership.CustomField + |> Ash.Query.filter(name == ^search_name) + |> Ash.read!(actor: system_actor) + + assert custom_field.value_type == :string + end + end + # Helper functions defp create_member do system_actor = Mv.Helpers.SystemActor.get_system_actor()