Skip to content

Commit

Permalink
improve check for showing context dashboard.
Browse files Browse the repository at this point in the history
  • Loading branch information
aesmail committed Sep 7, 2023
1 parent 8444902 commit 371e211
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
12 changes: 9 additions & 3 deletions lib/kaffy/resource_admin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -446,17 +446,23 @@ defmodule Kaffy.ResourceAdmin do
)
end

def show_context_dashboard?(resource, conn) do
Utils.get_assigned_value_or_default(
resource,
:show_context_dashboard?,
Utils.show_context_dashboard?(),
[conn]
)
end

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
11 changes: 10 additions & 1 deletion lib/kaffy/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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.
"""
Expand Down Expand Up @@ -391,7 +400,7 @@ defmodule Kaffy.Utils do
end

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

defp env(key, default \\ nil) do
Expand Down
12 changes: 6 additions & 6 deletions lib/kaffy_web/templates/layout/app.html.eex
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,19 @@
<% end %>

<%= for context <- Kaffy.Utils.contexts(@conn) do %>
<li class="nav-item<%= if @conn.assigns[:context] == to_string(context) do %> active<% end %>">
<a class="nav-link <%= if @conn.assigns[:context] != to_string(context) do %> collapsed<% end %>"
<li class="nav-item<%= if @conn.path_params["context"] == to_string(context) do %> active<% end %>">
<a class="nav-link <%= if @conn.path_params["context"] != to_string(context) do %> collapsed<% end %>"
href="#<%= context %>-context" data-toggle="collapse" aria-expanded="false" aria-controls="<%= context %>-context">
<span class="menu-title"><%= Kaffy.Utils.context_name(@conn, context) %></span>
<i class="menu-arrow"></i>
<i class="fas fa-bars menu-icon"></i>
</a>
<div class="collapse<%= if @conn.assigns[:context] == to_string(context) do %> show<% end %>" id="<%= context %>-context">
<div class="collapse<%= if @conn.path_params["context"] == to_string(context) do %> show<% end %>" id="<%= context %>-context">
<ul class="nav flex-column sub-menu">
<%= if Kaffy.Utils.context_admins_include_function?(@conn, context, :widgets) do %>
<li class="nav-item"><%= link "Dashboard", to: Kaffy.Utils.router().kaffy_context_dashboard_path(@conn, :dashboard, context), class: "nav-link" %></li>
<% end %>
<%= 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

0 comments on commit 371e211

Please sign in to comment.