Skip to content

Commit

Permalink
Add support for extra admin pages
Browse files Browse the repository at this point in the history
  • Loading branch information
tfwright committed Jan 27, 2024
1 parent 5ed8c58 commit 3966cd0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
15 changes: 15 additions & 0 deletions dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,20 @@ defmodule DemoWeb.PostsAdmin.Home do
end
end

defmodule DemoWeb.Extra do
use Phoenix.LiveView

@impl true
def render(assigns) do
~H"""
<div class="flex h-full items-center justify-center">
<div class="w-1/2">
This is an extra page
</div>
</div>
"""
end
end

defmodule DemoWeb.Router do
use Phoenix.Router
Expand All @@ -499,6 +513,7 @@ defmodule DemoWeb.Router do
live_admin "/admin", title: "DevAdmin" do
admin_resource "/users/profiles", Demo.Accounts.User.Profile
admin_resource "/users", DemoWeb.UserAdmin
live "/extra", DemoWeb.Extra, :index, as: :extra
end

live_admin "/posts-admin", components: [home: DemoWeb.PostsAdmin.Home] do
Expand Down
36 changes: 36 additions & 0 deletions lib/live_admin/components/nav.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,33 @@ defmodule LiveAdmin.Components.Nav do
import LiveAdmin,
only: [resource_title: 2, route_with_params: 2, trans: 1]

@impl true
def update(assigns, socket) do
base_path = assigns.base_path

extra_pages =
socket.router
|> Phoenix.Router.routes()
|> Enum.filter(fn r -> r.path == "/admin/extra" end)
|> Enum.filter(fn r ->
match?(
%{
metadata: %{
phoenix_live_view: {_, _, _, %{extra: %{session: {_, _, [^base_path, _]}}}}
}
},
r
)
end)

socket =
socket
|> assign(assigns)
|> assign(extra_pages: extra_pages)

{:ok, socket}
end

@impl true
def render(assigns) do
nested_resources =
Expand Down Expand Up @@ -39,6 +66,15 @@ defmodule LiveAdmin.Components.Nav do
config={@config}
/>
</li>
<%= if Enum.any?(@extra_pages) do %>
<li class="nav__item--group">
<%= for route <- @extra_pages do %>
<.link navigate={route.path}>
<%= humanize(route.helper) %>
</.link>
<% end %>
</li>
<% end %>
<li class="nav__item--group">
<.link navigate={route_with_params(assigns, resource_path: "session")}>
<%= trans("Session") %>
Expand Down

0 comments on commit 3966cd0

Please sign in to comment.