Skip to content

Commit

Permalink
preparing for per-context dashboards.
Browse files Browse the repository at this point in the history
  • Loading branch information
aesmail committed Sep 7, 2023
1 parent 6104735 commit 8444902
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 5 deletions.
13 changes: 11 additions & 2 deletions lib/kaffy/resource_admin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -446,8 +446,17 @@ 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
IO.inspect(context, label: "coming from context")
main_dashboard? = context == :kaffy_dashboard
show_context_dashboard? = Kaffy.Utils.show_context_dashboard?()

conn
|> Kaffy.Utils.contexts()
|> Enum.filter(fn c -> main_dashboard? or (show_context_dashboard? and c == context) end)
|> Enum.reduce([], fn c, all ->
IO.inspect(c, label: "collect_widgets context")

widgets =
Enum.reduce(Kaffy.Utils.schemas_for_context(conn, c), [], fn {_, resource}, all ->
all ++ Kaffy.ResourceAdmin.widgets(resource, conn)
Expand Down
5 changes: 5 additions & 0 deletions lib/kaffy/routes.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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_dashboard?() 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)

Expand Down
4 changes: 4 additions & 0 deletions lib/kaffy/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,10 @@ defmodule Kaffy.Utils do
)
end

def show_context_dashboard?() do
env(:enable_context_dashboard, true)
end

defp env(key, default \\ nil) do
Application.get_env(:kaffy, key, default)
end
Expand Down
5 changes: 4 additions & 1 deletion lib/kaffy_web/controllers/home_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions lib/kaffy_web/controllers/resource_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
%{
Expand Down
4 changes: 2 additions & 2 deletions lib/kaffy_web/templates/home/index.html.eex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div class="page-header">
<h3 class="page-title">Dashboard </h3>
<h3 class="page-title">Main Dashboard</h3>
<nav aria-label="breadcrumb">
<ul class="breadcrumb">
<li class="breadcrumb-item active" aria-current="page">
Expand All @@ -11,7 +11,7 @@
</nav>
</div>

<% widgets = Kaffy.ResourceAdmin.collect_widgets(@conn) %>
<% widgets = Kaffy.ResourceAdmin.collect_widgets(@conn, @context) %>

<%= if Enum.empty?(widgets) do %>

Expand Down
3 changes: 3 additions & 0 deletions lib/kaffy_web/templates/layout/app.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@
<div class="collapse<%= if @conn.assigns[:context] == to_string(context) do %> show<% end %>" id="<%= context %>-context">
<ul class="nav flex-column sub-menu">
<%= for {resource, options} <- Kaffy.Utils.schemas_for_context(@conn, context) do %>
<%= if Kaffy.Utils.show_context_dashboard?() do %>
<li class="nav-item"><%= link "Dashboard", to: Kaffy.Utils.router().kaffy_context_dashboard_path(@conn, :dashboard, context), class: "nav-link" %></li>
<% end %>
<%= if Kaffy.ResourceAdmin.authorized?(options, @conn) && Kaffy.Utils.visible?(options) do %>
<li class="nav-item"><%= link Kaffy.ResourceAdmin.plural_name(options), to: Kaffy.Utils.router().kaffy_resource_path(@conn, :index, context, resource), class: "nav-link" %></li>
<%= for custom_link <- Kaffy.ResourceAdmin.custom_links(options, :sub) do %>
Expand Down
47 changes: 47 additions & 0 deletions lib/kaffy_web/templates/resource/dashboard.html.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<div class="page-header">
<h3 class="page-title"><%= String.capitalize(to_string(@context)) %> Dashboard</h3>
<nav aria-label="breadcrumb">
<ul class="breadcrumb">
<li class="breadcrumb-item active" aria-current="page">
<span>
Overview <i class="fas fa-feather icon-sm text-primary align-middle"></i>
</span>
</li>
</ul>
</nav>
</div>

<% widgets = Kaffy.ResourceAdmin.collect_widgets(@conn, @context) %>

<%= if Enum.empty?(widgets) do %>

<div class="row mt-3">
<div class="col-md-12 text-center">
<h4>A powerfully simple admin package for phoenix applications.</h4>
<h4>You can add widgets to this page by defining <code>widgets/2</code> in your admin modules.</h4>
</div>
</div>

<% else %>

<div class="row mt-1 row-cols-1 row-cols-md-2 row-cols-sm-1 row-cols-xs-1">
<%= for widget <- widgets do %>
<%= if widget.type == "text" do %>
<%= render KaffyWeb.HomeView, "_text.html", widget: widget %>
<% end %>

<%= if widget.type == "chart" do %>
<%= render KaffyWeb.HomeView, "_chart.html", widget: widget %>
<% end %>

<%= if widget.type == "progress" do %>
<%= render KaffyWeb.HomeView, "_progress.html", widget: widget %>
<% end %>

<%= if widget.type == "tidbit" do %>
<%= render KaffyWeb.HomeView, "_tidbit.html", widget: widget %>
<% end %>
<% end %>
</div>

<% end %>

0 comments on commit 8444902

Please sign in to comment.