Skip to content

Commit

Permalink
fix: fix relationship loading on Resource Update form (#220) (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
netProphET authored Oct 30, 2024
1 parent 7dccb99 commit 400eb02
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions lib/ash_admin/components/resource/form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 400eb02

Please sign in to comment.