defmodule SlaxWeb.ChatRoomLive do
@moduledoc false
use SlaxWeb, :live_view
alias Slax.Repo
alias Slax.Chat.Room
def render(assigns) do
~H"""
Rooms
<.room_link :for={room <- @rooms} room={room} active={room.id == @room.id} />
#<%= @room.name %>
<%= if @hide_topic? do %>
[Topic hidden]
<% else %>
<%= @room.topic %>
<% end %>
"""
end
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
def handle_event("toggle-topic", _params, socket) do
{:noreply, update(socket, :hide_topic?, &(!&1))}
end
attr :active, :boolean, required: true
attr :room, Room, required: true
defp room_link(assigns) do
~H"""
<.icon name="hero-hashtag" class="h-4 w-4" />
<%= @room.name %>
"""
end
end