mirror of
https://github.com/ash-project/ash_admin.git
synced 2024-09-19 21:03:52 +12:00
improvement: general fixes, configurable action lists
This commit is contained in:
parent
90edb4e266
commit
4f102d26df
7 changed files with 87 additions and 51 deletions
|
@ -116,41 +116,44 @@ defmodule AshAdmin.Components.Resource.DataTable do
|
|||
|
||||
def render(assigns) do
|
||||
~H"""
|
||||
<div class="sm:mt-0 bg-gray-300 min-h-screen">
|
||||
<div class="md:grid md:grid-cols-3 md:gap-6 md:mx-16 md:pt-10">
|
||||
<div class="md:mt-0 md:col-span-2">
|
||||
<div :if={{@action.arguments != []}} class="shadow-lg overflow-hidden pt-2 sm:rounded-md bg-white">
|
||||
<div class="px-4 sm:p-6">
|
||||
<Form :if={{@query}} as="query" for={{@query}} change="validate" submit="save" :let={{form: form}}>
|
||||
{{AshAdmin.Components.Resource.Form.render_attributes(assigns, @resource, @action, form)}}
|
||||
<div class="px-4 py-3 text-right sm:px-6">
|
||||
<button
|
||||
type="submit"
|
||||
class="inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
|
||||
>
|
||||
Run Query
|
||||
</button>
|
||||
</div>
|
||||
</Form>
|
||||
<div>
|
||||
<div class="sm:mt-0 bg-gray-300 min-h-screen">
|
||||
<div :if={{@action.arguments != []}} class="md:grid md:grid-cols-3 md:gap-6 md:mx-16 md:pt-10 mb-10">
|
||||
<div class="md:mt-0 md:col-span-2">
|
||||
<div class="shadow-lg overflow-hidden pt-2 sm:rounded-md bg-white">
|
||||
<div class="px-4 sm:p-6">
|
||||
<Form :if={{@query}} as="query" for={{@query}} change="validate" submit="save" :let={{form: form}}>
|
||||
{{AshAdmin.Components.Resource.Form.render_attributes(assigns, @resource, @action, form)}}
|
||||
<div class="px-4 py-3 text-right sm:px-6">
|
||||
<button
|
||||
type="submit"
|
||||
class="inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
|
||||
>
|
||||
Run Query
|
||||
</button>
|
||||
</div>
|
||||
</Form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div :if={{@action.arguments == [] || @params["args"]}} class="h-full overflow-scroll">
|
||||
<div class="shadow-lg overflow-scroll sm:rounded-md bg-white">
|
||||
<div :if={{ match?({:error, _}, @data) }}>
|
||||
{{ {:error, %{query: query}} = @data
|
||||
nil }}
|
||||
<ul>
|
||||
<li :for={{ error <- query.errors }}>
|
||||
{{ message(error) }}
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="px-2">
|
||||
{{render_pagination_links(assigns, :top)}}
|
||||
<Table :if={{ match?({:ok, _data}, @data) }} data={{data(@data)}} resource={{@resource}} api={{@api}} set_actor={{@set_actor}} attributes={{AshAdmin.Resource.table_columns(@resource)}}/>
|
||||
{{render_pagination_links(assigns, :bottom)}}
|
||||
|
||||
<div :if={{@action.arguments == [] || @params["args"]}} class="h-full overflow-scroll md:mx-4">
|
||||
<div class="shadow-lg overflow-scroll sm:rounded-md bg-white">
|
||||
<div :if={{ match?({:error, _}, @data) }}>
|
||||
{{ {:error, %{query: query}} = @data
|
||||
nil }}
|
||||
<ul>
|
||||
<li :for={{ error <- query.errors }}>
|
||||
{{ message(error) }}
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="px-2">
|
||||
{{render_pagination_links(assigns, :top)}}
|
||||
<Table :if={{ match?({:ok, _data}, @data) }} data={{data(@data)}} resource={{@resource}} api={{@api}} set_actor={{@set_actor}} attributes={{AshAdmin.Resource.table_columns(@resource)}}/>
|
||||
{{render_pagination_links(assigns, :bottom)}}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -826,7 +826,20 @@ defmodule AshAdmin.Components.Resource.Form do
|
|||
end
|
||||
|
||||
defp actions(resource, type) do
|
||||
for %{type: ^type} = action <- Ash.Resource.Info.actions(resource) do
|
||||
action_names =
|
||||
case type do
|
||||
:create ->
|
||||
AshAdmin.Resource.create_actions(resource)
|
||||
|
||||
:update ->
|
||||
AshAdmin.Resource.update_actions(resource)
|
||||
|
||||
:destroy ->
|
||||
AshAdmin.Resource.destroy_actions(resource)
|
||||
end
|
||||
|
||||
for %{type: ^type, name: name} = action <- Ash.Resource.Info.actions(resource),
|
||||
is_nil(action_names) || name in action_names do
|
||||
{to_name(action.name), to_string(action.name)}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -49,13 +49,13 @@ defmodule AshAdmin.Components.Resource.Nav do
|
|||
end
|
||||
|
||||
defp data_groups(socket, api, resource, current_action) do
|
||||
show_actions = AshAdmin.Resource.get_actions(resource)
|
||||
read_actions = AshAdmin.Resource.read_actions(resource)
|
||||
|
||||
[
|
||||
resource
|
||||
|> Ash.Resource.Info.actions()
|
||||
|> Enum.filter(&(&1.type == :read))
|
||||
|> Enum.reject(&(&1.name in show_actions))
|
||||
|> Enum.filter(&(is_nil(read_actions) || &1.name in read_actions))
|
||||
|> Enum.map(fn action ->
|
||||
%{
|
||||
text: action_name(action),
|
||||
|
|
|
@ -14,7 +14,6 @@ defmodule AshAdmin.Components.Resource do
|
|||
prop(set_actor, :event, required: true)
|
||||
prop(authorizing, :boolean, required: true)
|
||||
prop(tenant, :string, required: true)
|
||||
prop(recover_filter, :any)
|
||||
prop(url_path, :string, default: "")
|
||||
prop(params, :map, default: %{})
|
||||
prop(primary_key, :any, default: nil)
|
||||
|
|
|
@ -110,7 +110,6 @@ defmodule AshAdmin.Components.TopNav.ActorSelect do
|
|||
|
||||
defp render_actor_link(assigns, apis_and_resources) do
|
||||
~H"""
|
||||
<a href="#" id="actor-banner" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Set actor</a>
|
||||
<div aria-labelledby="actor-banner">
|
||||
<LiveRedirect
|
||||
to={{ash_action_path(
|
||||
|
@ -120,9 +119,12 @@ defmodule AshAdmin.Components.TopNav.ActorSelect do
|
|||
:read,
|
||||
Ash.Resource.Info.primary_action(resource, :read).name
|
||||
)}}
|
||||
:for={{ {api, resource} <- apis_and_resources }}
|
||||
:for.with_index={{ {{api, resource}, i} <- apis_and_resources }}
|
||||
>
|
||||
{{ AshAdmin.Resource.name(resource) }}
|
||||
Set {{ AshAdmin.Resource.name(resource) }}
|
||||
<span :if={{i != (Enum.count(apis_and_resources) - 1)}}>
|
||||
|
|
||||
</span>
|
||||
</LiveRedirect>
|
||||
</div>
|
||||
"""
|
||||
|
|
|
@ -58,7 +58,6 @@ defmodule AshAdmin.PageLive do
|
|||
:authorizing,
|
||||
AshAdmin.ActorPlug.session_bool(session["actor_authorizing"]) || false
|
||||
)
|
||||
|> assign(:recover_filter, nil)
|
||||
|> assign(:actor_paused, actor_paused)}
|
||||
end
|
||||
|
||||
|
@ -116,13 +115,6 @@ defmodule AshAdmin.PageLive do
|
|||
def handle_params(params, url, socket) do
|
||||
url = URI.parse(url)
|
||||
|
||||
socket =
|
||||
if params["filter"] && socket.assigns[:resource] do
|
||||
assign(socket, :recover_filter, params["filter"])
|
||||
else
|
||||
socket
|
||||
end
|
||||
|
||||
socket =
|
||||
if params["primary_key"] do
|
||||
case decode_primary_key(socket.assigns.resource, params["primary_key"]) do
|
||||
|
|
|
@ -35,10 +35,25 @@ defmodule AshAdmin.Resource do
|
|||
doc:
|
||||
"The action to use when linking to the resource/viewing a single record. Defaults to the primary read action."
|
||||
],
|
||||
get_actions: [
|
||||
read_actions: [
|
||||
type: {:list, :atom},
|
||||
doc:
|
||||
"A list of read actions that can be used to show resource details. These actions should accept arguments that produce one record e.g `get_user_by_id`."
|
||||
"A list of read actions that can be used to show resource details. By default, all actions are included"
|
||||
],
|
||||
create_actions: [
|
||||
type: {:list, :atom},
|
||||
doc:
|
||||
"A list of create actions that can be create records. By default, all actions are included"
|
||||
],
|
||||
update_actions: [
|
||||
type: {:list, :atom},
|
||||
doc:
|
||||
"A list of update actions that can be update records. By default, all actions are included"
|
||||
],
|
||||
destroy_actions: [
|
||||
type: {:list, :atom},
|
||||
doc:
|
||||
"A list of destroy actions that can be destroy records. By default, all actions are included"
|
||||
],
|
||||
table_columns: [
|
||||
type: {:list, :atom},
|
||||
|
@ -64,8 +79,20 @@ defmodule AshAdmin.Resource do
|
|||
Ash.Dsl.Extension.get_opt(resource, [:admin], :actor?, false, true)
|
||||
end
|
||||
|
||||
def get_actions(resource) do
|
||||
Ash.Dsl.Extension.get_opt(resource, [:admin], :get_actions, false, []) || []
|
||||
def read_actions(resource) do
|
||||
Ash.Dsl.Extension.get_opt(resource, [:admin], :read_actions, nil, true)
|
||||
end
|
||||
|
||||
def create_actions(resource) do
|
||||
Ash.Dsl.Extension.get_opt(resource, [:admin], :create_actions, nil, true)
|
||||
end
|
||||
|
||||
def update_actions(resource) do
|
||||
Ash.Dsl.Extension.get_opt(resource, [:admin], :update_actions, nil, true)
|
||||
end
|
||||
|
||||
def destroy_actions(resource) do
|
||||
Ash.Dsl.Extension.get_opt(resource, [:admin], :destroy_actions, nil, true)
|
||||
end
|
||||
|
||||
def show_action(resource) do
|
||||
|
|
Loading…
Reference in a new issue