feat: map rooms route and add it to sidebar room list

This commit is contained in:
João Paulo Dubas 2024-10-10 00:45:16 +00:00
parent 7765b11f46
commit e20857f1d6
Signed by: joao.dubas
SSH Key Fingerprint: SHA256:V1mixgOGRc/YMhGx/DNkOSmJxgA2vHNrDZEk3wt/kOA
2 changed files with 12 additions and 2 deletions

View File

@ -39,8 +39,17 @@ defmodule SlaxWeb.ChatRoomLive do
"""
end
def mount(_params, _session, socket) do
def mount(params, _session, socket) do
[room | _] = rooms = Repo.all(Room)
# NOTE: (jpd) on the lesson **15 path params and routes** the author makes a new call to the database. I changed
# this behaviour and fetch the room from the list.
room =
Enum.find(
rooms,
&(Integer.to_string(&1.id) == Map.get(params, "id", Integer.to_string(room.id)))
)
{:ok, assign(socket, hide_topic?: false, room: room, rooms: rooms)}
end
@ -58,7 +67,7 @@ defmodule SlaxWeb.ChatRoomLive do
"flex items-center h-8 text-sm pl-8 pr-3",
(@active && "bg-slate-300") || "hover:bg-slate-300"
]}
href="#"
href={~p"/rooms/#{@room}"}
>
<.icon name="hero-hashtag" class="h-4 w-4" />
<span class={["ml-2 leading-none", @active && "font-bold"]}>

View File

@ -19,6 +19,7 @@ defmodule SlaxWeb.Router do
get "/home", PageController, :home
live "/", ChatRoomLive
live "/rooms/:id", ChatRoomLive
end
# Other scopes may use custom stacks.