Hello,
full disclosure I am still very new to elixir and phoenix and liveview.
I am trying to transform a liveview controller and it’s. *.leex template into a component. The controller was auto generated with mix phx.gen.live
and I am trying to turn the :show
controller into a re-usable component that can be embedded in other views.
The first of which was to insert it into page_live.html.leex
which according to the docs should work when
<%# live_render(@socket, ChatDemo.ChatRoomLive.Show, id: "ChatWindow", session: %{"id" => "07593216-b033-40e1-8c03-f32aa7375dec"} ) %>
But I get a no match of right hand side value: :not_mounted_at_router
and somehow the debug page doesn’t show the expected content for session. I know it does require an :id
in the params.
the controller works as a standalone, but not when rendered in another *.leex, is it possible to write a mount function that will work with live_render ? What am I doing wrong? or miss understanding?
the router entry:
scope "/", DemoWeb do
pipe_through [:browser, :admin_view]
live "/message/:id", MessageLive.Show, :show
live "/message/:id/show/edit", MessageLive.Show, :edit
end
the liveview render call
<%= live_render(@socket, DemoWeb.ChatRoomLive.Show, id: "ChatWindow", session: %{"id" => "07593216-b033-40e1-8c03-f32aa7375dec"} ) %>
the code seems to fail here
@impl true
def mount(params, session, socket) do
%{"id" => id} = params # FAILS HERE, but session only contains the csrf token
if connected?(socket), do: Chat.subscribe()
messages = Chat.list_message_for_chat_room(id)
{:ok, socket
|> assign(:messages, messages)
|> assign(:message, %DemoWeb.Chat.Message{})
}
end