fix: Admin links are not aware of the scope where ash_admin is called (#49)

* WIP - pass 1

* Pass prefix in session map and use in page_live.ex

* Update logic and add test
This commit is contained in:
Kevin Mathew 2023-03-26 06:39:46 +05:30 committed by GitHub
parent 49a008a5e2
commit 58444c47c7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 4 deletions

View file

@ -26,6 +26,18 @@ defmodule AshAdmin.PageLive do
socket
) do
otp_app = socket.endpoint.config(:otp_app)
prefix =
case prefix do
"/" ->
session["request_path"]
_ ->
request_path = session["request_path"]
[scope, _] = String.split(request_path, prefix)
scope <> prefix
end
socket = assign(socket, :prefix, prefix)
actor_paused =

View file

@ -90,6 +90,8 @@ defmodule AshAdmin.Router do
def __session__(conn, [session]), do: __session__(conn, session)
def __session__(conn, session) do
session = Map.put(session, "request_path", conn.request_path)
Enum.reduce(@cookies_to_replicate, session, fn cookie, session ->
case conn.req_cookies[cookie] do
value when value in [nil, "", "null"] ->

View file

@ -1,7 +1,6 @@
defmodule AshAdmin.Test.PageLiveTest do
use ExUnit.Case, async: false
import Plug.Conn
import Phoenix.ConnTest
import Phoenix.LiveViewTest
@endpoint AshAdmin.Test.Endpoint
@ -11,10 +10,21 @@ defmodule AshAdmin.Test.PageLiveTest do
end
test "it renders the schema by default", %{conn: conn} do
{:ok, _view, html} = live(conn, "/Api/Post")
{:ok, _view, html} = live(conn, "/api/admin")
assert html =~ "Attributes"
assert html =~ "body"
assert html =~ "String"
{:ok, _view, html} = live(conn, "/api/admin/test")
assert html =~ "Attributes"
assert html =~ "body"
assert html =~ "String"
end
test "it raises error when no route is found", %{conn: conn} do
assert_raise(Phoenix.Router.NoRouteError, fn -> live(conn, "/") end)
assert_raise(Phoenix.Router.NoRouteError, fn -> live(conn, "/Api/Post") end)
end
end

View file

@ -6,10 +6,10 @@ defmodule AshAdmin.Test.Router do
plug(:fetch_query_params)
end
scope "/" do
scope "/api" do
pipe_through(:browser)
import AshAdmin.Router
ash_admin("/")
ash_admin("/admin")
end
end