From 400eb02822d3f07b86af6a69642b470b04f81c4a Mon Sep 17 00:00:00 2001 From: Mike Schell Date: Tue, 29 Oct 2024 21:56:23 -0400 Subject: [PATCH] fix: fix relationship loading on Resource Update form (#220) (#221) --- lib/ash_admin/components/resource/form.ex | 24 ++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/ash_admin/components/resource/form.ex b/lib/ash_admin/components/resource/form.ex index 26af6e5..822550d 100644 --- a/lib/ash_admin/components/resource/form.ex +++ b/lib/ash_admin/components/resource/form.ex @@ -1464,6 +1464,7 @@ defmodule AshAdmin.Components.Resource.Form do if AshPhoenix.Form.has_form?(socket.assigns.form, path) do nested_form = AshPhoenix.Form.get_form(socket.assigns.form, path) + form = socket.assigns.form |> AshPhoenix.Form.remove_form(path) @@ -1598,12 +1599,29 @@ defmodule AshAdmin.Components.Resource.Form do path, fn adding_form -> if adding_form.data do - new_data = Ash.load!(adding_form.data, relationship, domain: socket.assigns.domain) + new_data = + Ash.load!(adding_form.data, relationship, domain: socket.assigns.domain) + + updated_form = + adding_form + |> Map.put(:data, new_data) + |> AshPhoenix.Form.validate(adding_form.params, errors: false) + |> AshPhoenix.Form.update_options(fn opts -> + Keyword.put(opts, :forms, [ + {relationship, + [ + type: :list, + resource: + Ash.Resource.Info.relationship(adding_form.resource, relationship).destination, + data: Map.get(new_data, relationship) + ]} + ]) + end) if Map.has_key?(adding_form.source, :data) do - %{adding_form | data: new_data, source: %{adding_form.source | data: new_data}} + %{updated_form | data: new_data, source: %{adding_form.source | data: new_data}} else - %{adding_form | data: new_data} + updated_form end else adding_form