feat(chat): show joined rooms on index page

This commit is contained in:
João Paulo Dubas 2024-11-17 21:40:46 +00:00
parent 9755dc2b9e
commit 3f97444908
Signed by: joao.dubas
SSH Key Fingerprint: SHA256:V1mixgOGRc/YMhGx/DNkOSmJxgA2vHNrDZEk3wt/kOA
2 changed files with 27 additions and 3 deletions

View File

@ -60,6 +60,18 @@ defmodule Slax.Chat do
|> Enum.sort_by(& &1.name)
end
def list_rooms_with_joined(%User{} = user) do
query =
from(r in Room,
left_join: m in RoomMembership,
on: r.id == m.room_id and m.user_id == ^user.id,
select: {r, not is_nil(m.id)},
order_by: [asc: :name]
)
Repo.all(query)
end
def list_messages_in_room(%Room{id: room_id}) do
query =
from(m in Message,

View File

@ -13,7 +13,7 @@ defmodule SlaxWeb.ChatRoomLive.Index do
<div class="bg-slate-50 border rounded">
<div id="rooms" class="divide-y" phx-update="stream">
<div
:for={{id, room} <- @streams.rooms}
:for={{id, {room, joined?}} <- @streams.rooms}
class="cursor-pointer p-4 flex justify-between items-center group first:rounded-t last:rounded-b"
id={id}
phx-click={JS.navigate(~p"/rooms/#{room}")}
@ -26,6 +26,12 @@ defmodule SlaxWeb.ChatRoomLive.Index do
</span>
</div>
<div class="text-gray-500 text-sm">
<%= if joined? do %>
<span class="text-green-600 font-bold"> Joined</span>
<% end %>
<%= if joined? && room.topic do %>
<span class="mx-1">·</span>
<% end %>
<%= if room.topic do %>
<%= room.topic %>
<% end %>
@ -40,8 +46,14 @@ defmodule SlaxWeb.ChatRoomLive.Index do
@impl Phoenix.LiveView
def mount(_params, _session, socket) do
rooms = Chat.list_rooms()
socket = socket |> assign(page_title: "All rooms") |> stream(:rooms, rooms)
rooms = Chat.list_rooms_with_joined(socket.assigns.current_user)
socket =
socket
|> assign(page_title: "All rooms")
|> stream_configure(:rooms, dom_id: fn {room, _joined?} -> "rooms-#{room.id}" end)
|> stream(:rooms, rooms)
{:ok, socket}
end
end