Skip to content

Commit

Permalink
Merge pull request #92 from tfwright/feat/extra-pages
Browse files Browse the repository at this point in the history
Add support for extra admin pages
  • Loading branch information
tfwright authored Jan 29, 2024
2 parents ff1a974 + f932661 commit 301fd86
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions assets/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ input[type="date"], input[type="number"] {
@apply nav__item;
@apply border-b;
@apply py-1;
@apply flex;
@apply flex-col;
}

.nav__item--selected {
Expand Down
15 changes: 15 additions & 0 deletions dev.exs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,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 @@ -505,6 +519,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
2 changes: 2 additions & 0 deletions dist/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,8 @@ input[class$="-loading"] + div nav:before {
border-bottom-width: 1px;
padding-top: 0.25rem;
padding-bottom: 0.25rem;
display: flex;
flex-direction: column;
}

.nav__list > .nav__item--selected:first-of-type {
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 ->
match?(
%{
metadata: %{
phoenix_live_view: {_, _, _, %{extra: %{session: {_, _, [^base_path, _]}}}}
}
},
r
) && is_nil(r.metadata[:resource]) &&
!Enum.member?(["home_/admin", "session_/admin"], r.helper)
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 301fd86

Please sign in to comment.