Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions documentation/tutorials/getting-started-with-ash-admin.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ ash_admin "/admin", csp_nonce_assign_key: :csp_nonce_value

This will allow AshAdmin-generated inline CSS and JS blocks to execute normally.

## Action Context

AshAdmin adds `ash_admin?: true` to the context of all AshPhoenix forms it uses (read, update, and
generic actions). This allows upstream logic to know if an action is being called by AshAdmin.

## Troubleshooting

#### UI issues
Expand Down
1 change: 1 addition & 0 deletions lib/ash_admin/components/resource/data_table.ex
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ defmodule AshAdmin.Components.Resource.DataTable do
else
%{}
end
|> Map.put(:ash_admin?, true)

query =
socket.assigns[:resource]
Expand Down
37 changes: 13 additions & 24 deletions lib/ash_admin/components/resource/form.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2743,40 +2743,29 @@ defmodule AshAdmin.Components.Resource.Form do
include_non_map_types?: true
)

form_opts = [
actor: socket.assigns[:actor],
authorize?: socket.assigns[:authorizing],
context: %{ash_admin?: true},
domain: socket.assigns.domain,
forms: auto_forms,
tenant: socket.assigns[:tenant],
transform_errors: transform_errors
]
Comment on lines +2746 to +2754
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While adding context: %{ash_admin?: true}, noticed all three forms below used the exact same options, so I refactored the code a bit to reduce repetition.

Please let me know if this is OK or I should undo that.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works for me 😄 I believe there are some other cases where we make forms, there should be one for generic actions and one where we build a form for a read action.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should do those as well 😄

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zachdaniel Done! Please check again.


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

:update ->
socket.assigns.record
|> AshPhoenix.Form.for_update(socket.assigns.action.name,
domain: socket.assigns.domain,
forms: auto_forms,
actor: socket.assigns[:actor],
authorize?: socket.assigns[:authorizing],
transform_errors: transform_errors,
tenant: socket.assigns[:tenant]
)
|> AshPhoenix.Form.for_update(socket.assigns.action.name, form_opts)

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

assign(socket, :form, form |> to_form())
Expand Down
1 change: 1 addition & 0 deletions lib/ash_admin/components/resource/generic_action.ex
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ defmodule AshAdmin.Components.Resource.GenericAction do
else
%{}
end
|> Map.put(:ash_admin?, true)

form =
AshPhoenix.Form.for_action(socket.assigns.resource, socket.assigns.action.name,
Expand Down
Loading