From 4f102d26df963f5418b535a03bce4e1647312437 Mon Sep 17 00:00:00 2001 From: Zach Daniel Date: Sun, 21 Mar 2021 12:02:27 -0400 Subject: [PATCH] improvement: general fixes, configurable action lists --- .../components/resource/data_table.ex | 67 ++++++++++--------- lib/ash_admin/components/resource/form.ex | 15 ++++- lib/ash_admin/components/resource/nav.ex | 4 +- lib/ash_admin/components/resource/resource.ex | 1 - .../components/top_nav/actor_select.ex | 8 ++- lib/ash_admin/pages/page_live.ex | 8 --- lib/ash_admin/resource/resource.ex | 35 ++++++++-- 7 files changed, 87 insertions(+), 51 deletions(-) diff --git a/lib/ash_admin/components/resource/data_table.ex b/lib/ash_admin/components/resource/data_table.ex index 6334aa3..882a27a 100644 --- a/lib/ash_admin/components/resource/data_table.ex +++ b/lib/ash_admin/components/resource/data_table.ex @@ -116,41 +116,44 @@ defmodule AshAdmin.Components.Resource.DataTable do def render(assigns) do ~H""" -
-
-
-
-
-
- {{AshAdmin.Components.Resource.Form.render_attributes(assigns, @resource, @action, form)}} -
- -
-
+
+
+
+
+
+
+
+ {{AshAdmin.Components.Resource.Form.render_attributes(assigns, @resource, @action, form)}} +
+ +
+
+
-
-
-
-
- {{ {:error, %{query: query}} = @data - nil }} -
    -
  • - {{ message(error) }} -
  • -
-
-
- {{render_pagination_links(assigns, :top)}} - - {{render_pagination_links(assigns, :bottom)}} + +
+
+
+ {{ {:error, %{query: query}} = @data + nil }} +
    +
  • + {{ message(error) }} +
  • +
+
+
+ {{render_pagination_links(assigns, :top)}} +
+ {{render_pagination_links(assigns, :bottom)}} + diff --git a/lib/ash_admin/components/resource/form.ex b/lib/ash_admin/components/resource/form.ex index 85a27d8..3e902ca 100644 --- a/lib/ash_admin/components/resource/form.ex +++ b/lib/ash_admin/components/resource/form.ex @@ -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 diff --git a/lib/ash_admin/components/resource/nav.ex b/lib/ash_admin/components/resource/nav.ex index 2c4e621..f6fc7fc 100644 --- a/lib/ash_admin/components/resource/nav.ex +++ b/lib/ash_admin/components/resource/nav.ex @@ -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), diff --git a/lib/ash_admin/components/resource/resource.ex b/lib/ash_admin/components/resource/resource.ex index 958fb15..aae415e 100644 --- a/lib/ash_admin/components/resource/resource.ex +++ b/lib/ash_admin/components/resource/resource.ex @@ -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) diff --git a/lib/ash_admin/components/top_nav/actor_select.ex b/lib/ash_admin/components/top_nav/actor_select.ex index d0356bd..4f2292e 100644 --- a/lib/ash_admin/components/top_nav/actor_select.ex +++ b/lib/ash_admin/components/top_nav/actor_select.ex @@ -110,7 +110,6 @@ defmodule AshAdmin.Components.TopNav.ActorSelect do defp render_actor_link(assigns, apis_and_resources) do ~H""" -
- {{ AshAdmin.Resource.name(resource) }} + Set {{ AshAdmin.Resource.name(resource) }} + + | +
""" diff --git a/lib/ash_admin/pages/page_live.ex b/lib/ash_admin/pages/page_live.ex index 4ad26e2..a6476d5 100644 --- a/lib/ash_admin/pages/page_live.ex +++ b/lib/ash_admin/pages/page_live.ex @@ -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 diff --git a/lib/ash_admin/resource/resource.ex b/lib/ash_admin/resource/resource.ex index ebfca99..3188117 100644 --- a/lib/ash_admin/resource/resource.ex +++ b/lib/ash_admin/resource/resource.ex @@ -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