diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml deleted file mode 100644 index ddc425b1..00000000 --- a/.github/FUNDING.yml +++ /dev/null @@ -1,12 +0,0 @@ -# These are supported funding model platforms - -github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2] -patreon: # Replace with a single Patreon username -open_collective: # kaffy -ko_fi: # Replace with a single Ko-fi username -tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel -community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry -liberapay: # Replace with a single Liberapay username -issuehunt: # Replace with a single IssueHunt username -otechie: # Replace with a single Otechie username -custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2'] diff --git a/CHANGELOG.md b/CHANGELOG.md index 9070dd4a..6437282b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Crash when updating records. ### Added -- Support for filtering `{:array, :string}` fielsd [PR#285](https://github.com/aesmail/kaffy/pull/262) +- Support for filtering `{:array, :string}` fields [PR#285](https://github.com/aesmail/kaffy/pull/262) +- Per-context dashboard [PR#287](https://github.com/aesmail/kaffy/pull/287) ## v0.10.0-rc.0 (2023-09-02) diff --git a/README.md b/README.md index 89988e62..dd38b3b7 100644 --- a/README.md +++ b/README.md @@ -106,9 +106,18 @@ plug Plug.Static, # in your config/config.exs config :kaffy, - otp_app: :my_app, - ecto_repo: MyApp.Repo, - router: MyAppWeb.Router + # required keys + otp_app: :my_app, # required + ecto_repo: MyApp.Repo, # required + router: MyAppWeb.Router, # required + # optional keys + admin_title: "My Awesome App", + admin_logo: "/images/logo.png", + admin_logo_mini: "/images/logo-mini.png", + hide_dashboard: true, + home_page: [schema: [:accounts, :user]], + enable_context_dashboards: true, # since v0.10.0 + admin_footer: "Kaffy © 2023" # since v0.10.0 ``` Note that providing pipelines with the `:pipe_through` option will add those pipelines to kaffy's `:kaffy_browser` pipeline which is defined as follows: @@ -151,7 +160,9 @@ config :kaffy, admin_title: "My Awesome App", admin_logo: "/images/logo.png", admin_logo_mini: "/images/logo-mini.png", + admin_footer: "Kaffy © 2023", hide_dashboard: false, + enable_context_dashboards: true, home_page: [kaffy: :dashboard], ecto_repo: MyApp.Repo, router: MyAppWeb.Router, diff --git a/lib/kaffy/resource_admin.ex b/lib/kaffy/resource_admin.ex index 8e7c15b5..59078983 100644 --- a/lib/kaffy/resource_admin.ex +++ b/lib/kaffy/resource_admin.ex @@ -446,8 +446,14 @@ defmodule Kaffy.ResourceAdmin do ) end - def collect_widgets(conn) do - Enum.reduce(Kaffy.Utils.contexts(conn), [], fn c, all -> + def collect_widgets(conn, context \\ :kaffy_dashboard) do + main_dashboard? = context == :kaffy_dashboard + show_context_dashboard? = Kaffy.Utils.show_context_dashboards?() + + conn + |> Kaffy.Utils.contexts() + |> Enum.filter(fn c -> main_dashboard? or (show_context_dashboard? and c == context) end) + |> Enum.reduce([], fn c, all -> widgets = Enum.reduce(Kaffy.Utils.schemas_for_context(conn, c), [], fn {_, resource}, all -> all ++ Kaffy.ResourceAdmin.widgets(resource, conn) diff --git a/lib/kaffy/routes.ex b/lib/kaffy/routes.ex index 4240df95..9ab7e96b 100644 --- a/lib/kaffy/routes.ex +++ b/lib/kaffy/routes.ex @@ -35,6 +35,11 @@ defmodule Kaffy.Routes do get("/dashboard", HomeController, :dashboard, as: :kaffy_dashboard) get("/tasks", TaskController, :index, as: :kaffy_task) get("/p/:slug", PageController, :index, as: :kaffy_page) + + if Kaffy.Utils.show_context_dashboards?() do + get("/:context", ResourceController, :dashboard, as: :kaffy_context_dashboard) + end + get("/:context/:resource", ResourceController, :index, as: :kaffy_resource) post("/:context/:resource", ResourceController, :create, as: :kaffy_resource) diff --git a/lib/kaffy/utils.ex b/lib/kaffy/utils.ex index dd023c20..572c54df 100644 --- a/lib/kaffy/utils.ex +++ b/lib/kaffy/utils.ex @@ -310,11 +310,20 @@ defmodule Kaffy.Utils do true """ @spec has_function?(module(), atom()) :: boolean() + def has_function?(nil, _), do: false + def has_function?(admin, func) do functions = admin.__info__(:functions) Keyword.has_key?(functions, func) end + def context_admins_include_function?(conn, context, func) do + schemas_for_context(conn, context) + |> Enum.filter(fn {_, options} -> Keyword.has_key?(options, :admin) end) + |> Enum.map(fn {_, options} -> has_function?(Keyword.get(options, :admin), func) end) + |> Enum.any?() + end + @doc """ Returns true if `thing` is a module, false otherwise. """ @@ -390,6 +399,10 @@ defmodule Kaffy.Utils do ) end + def show_context_dashboards?() do + env(:enable_context_dashboards, true) + end + defp env(key, default \\ nil) do Application.get_env(:kaffy, key, default) end diff --git a/lib/kaffy_web/controllers/home_controller.ex b/lib/kaffy_web/controllers/home_controller.ex index 40830ce9..20d344b7 100644 --- a/lib/kaffy_web/controllers/home_controller.ex +++ b/lib/kaffy_web/controllers/home_controller.ex @@ -8,6 +8,9 @@ defmodule KaffyWeb.HomeController do end def dashboard(conn, _params) do - render(conn, "index.html", layout: {KaffyWeb.LayoutView, "app.html"}) + render(conn, "index.html", + layout: {KaffyWeb.LayoutView, "app.html"}, + context: :kaffy_dashboard + ) end end diff --git a/lib/kaffy_web/controllers/resource_controller.ex b/lib/kaffy_web/controllers/resource_controller.ex index 61f99d7b..c4e8dfae 100644 --- a/lib/kaffy_web/controllers/resource_controller.ex +++ b/lib/kaffy_web/controllers/resource_controller.ex @@ -5,6 +5,13 @@ defmodule KaffyWeb.ResourceController do use Phoenix.HTML alias Kaffy.Pagination + def dashboard(conn, %{"context" => context}) do + render(conn, "dashboard.html", + layout: {KaffyWeb.LayoutView, "app.html"}, + context: String.to_existing_atom(context) + ) + end + def index( conn, %{ diff --git a/lib/kaffy_web/templates/home/_progress.html.eex b/lib/kaffy_web/templates/home/_progress.html.eex index 7bbdef68..789536fe 100644 --- a/lib/kaffy_web/templates/home/_progress.html.eex +++ b/lib/kaffy_web/templates/home/_progress.html.eex @@ -1,4 +1,6 @@ -
+<% color = Map.get(@widget, :color, "danger") %> +<% width = Map.get(@widget, :width, 6) %> +

<%= @widget.title %>

@@ -6,7 +8,7 @@

<%= @widget.content %> <%= @widget.percentage %>%

-
+
diff --git a/lib/kaffy_web/templates/home/_text.html.eex b/lib/kaffy_web/templates/home/_text.html.eex index e42d1e71..f0a78896 100644 --- a/lib/kaffy_web/templates/home/_text.html.eex +++ b/lib/kaffy_web/templates/home/_text.html.eex @@ -1,4 +1,5 @@ -
+<% width = Map.get(@widget, :width, 6) %> +

<%= @widget.title %>

diff --git a/lib/kaffy_web/templates/home/_tidbit.html.eex b/lib/kaffy_web/templates/home/_tidbit.html.eex index bd8a6bc3..a4ad2949 100644 --- a/lib/kaffy_web/templates/home/_tidbit.html.eex +++ b/lib/kaffy_web/templates/home/_tidbit.html.eex @@ -1,7 +1,10 @@ -
-
+<% color = Map.get(@widget, :color, "success") %> +<% width = Map.get(@widget, :width, 3) %> + +
+
-

<%= @widget.title %>

+

<%= @widget.title %>

<%= @widget.content %>

diff --git a/lib/kaffy_web/templates/home/index.html.eex b/lib/kaffy_web/templates/home/index.html.eex index 9d671f49..2a612650 100644 --- a/lib/kaffy_web/templates/home/index.html.eex +++ b/lib/kaffy_web/templates/home/index.html.eex @@ -1,5 +1,5 @@ -<% widgets = Kaffy.ResourceAdmin.collect_widgets(@conn) %> +<% widgets = Kaffy.ResourceAdmin.collect_widgets(@conn, @context) %> <%= if Enum.empty?(widgets) do %> diff --git a/lib/kaffy_web/templates/layout/app.html.eex b/lib/kaffy_web/templates/layout/app.html.eex index 83616969..70fd0290 100644 --- a/lib/kaffy_web/templates/layout/app.html.eex +++ b/lib/kaffy_web/templates/layout/app.html.eex @@ -90,15 +90,18 @@ <% end %> <%= for context <- Kaffy.Utils.contexts(@conn) do %> -