Skip to content

Commit

Permalink
fix: properly reassign form on update
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Aug 4, 2023
1 parent b0fb75a commit 568c3eb
Showing 1 changed file with 42 additions and 46 deletions.
88 changes: 42 additions & 46 deletions lib/ash_admin/components/resource/form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1581,56 +1581,52 @@ defmodule AshAdmin.Components.Resource.Form do
end

defp assign_form(socket) do
if socket.assigns[:initialized] do
socket
else
transform_errors = fn
_, %{class: :forbidden} ->
{nil, "Forbidden", []}
transform_errors = fn
_, %{class: :forbidden} ->
{nil, "Forbidden", []}

_, other ->
other
end
_, other ->
other
end

auto_forms =
AshPhoenix.Form.Auto.auto(socket.assigns.resource, socket.assigns.action.name,
include_non_map_types?: true
)

form =
case socket.assigns.action.type do
:create ->
socket.assigns.resource
|> AshPhoenix.Form.for_create(socket.assigns.action.name,
api: socket.assigns.api,
actor: socket.assigns[:actor],
authorize?: socket.assigns[:authorizing],
forms: auto_forms,
transform_errors: transform_errors
)
auto_forms =
AshPhoenix.Form.Auto.auto(socket.assigns.resource, socket.assigns.action.name,
include_non_map_types?: true
)

:update ->
socket.assigns.record
|> AshPhoenix.Form.for_update(socket.assigns.action.name,
api: socket.assigns.api,
forms: auto_forms,
actor: socket.assigns[:actor],
authorize?: socket.assigns[:authorizing],
transform_errors: transform_errors
)
form =
case socket.assigns.action.type do
:create ->
socket.assigns.resource
|> AshPhoenix.Form.for_create(socket.assigns.action.name,
api: socket.assigns.api,
actor: socket.assigns[:actor],
authorize?: socket.assigns[:authorizing],
forms: auto_forms,
transform_errors: transform_errors
)

:destroy ->
socket.assigns.record
|> AshPhoenix.Form.for_destroy(socket.assigns.action.name,
api: socket.assigns.api,
forms: auto_forms,
actor: socket.assigns[:actor],
authorize?: socket.assigns[:authorizing],
transform_errors: transform_errors
)
end
:update ->
socket.assigns.record
|> AshPhoenix.Form.for_update(socket.assigns.action.name,
api: socket.assigns.api,
forms: auto_forms,
actor: socket.assigns[:actor],
authorize?: socket.assigns[:authorizing],
transform_errors: transform_errors
)

assign(socket, :form, form |> to_form())
end
:destroy ->
socket.assigns.record
|> AshPhoenix.Form.for_destroy(socket.assigns.action.name,
api: socket.assigns.api,
forms: auto_forms,
actor: socket.assigns[:actor],
authorize?: socket.assigns[:authorizing],
transform_errors: transform_errors
)
end

assign(socket, :form, form |> to_form())
end
end

0 comments on commit 568c3eb

Please sign in to comment.