feat(chat): simplify socket response

Add helper methods to create the tuples `{:ok, socket}` and
`{:noreply, socket}`, and make them available in `SlaxWeb` module.
This commit is contained in:
João Paulo Dubas 2025-02-05 00:05:49 +00:00
parent 2063870a9e
commit 489be57db7
Signed by: joao.dubas
SSH Key Fingerprint: SHA256:V1mixgOGRc/YMhGx/DNkOSmJxgA2vHNrDZEk3wt/kOA
5 changed files with 72 additions and 64 deletions

View File

@ -55,6 +55,8 @@ defmodule SlaxWeb do
use Phoenix.LiveView, use Phoenix.LiveView,
layout: {SlaxWeb.Layouts, :app} layout: {SlaxWeb.Layouts, :app}
import SlaxWeb.SocketHelpers, only: [ok: 1, noreply: 1]
unquote(html_helpers()) unquote(html_helpers())
end end
end end

View File

@ -237,24 +237,22 @@ defmodule SlaxWeb.ChatRoomLive do
Enum.each(rooms, &Chat.subscribe_to_room/1) Enum.each(rooms, &Chat.subscribe_to_room/1)
socket = socket
socket |> assign(
|> assign( hide_topic?: false,
hide_topic?: false, online_users: OnlineUsers.list(),
online_users: OnlineUsers.list(), rooms: rooms,
rooms: rooms, timezone: timezone,
timezone: timezone, users: users
users: users )
) |> assign_room_form(Chat.change_room(%Room{}))
|> assign_room_form(Chat.change_room(%Room{})) |> stream_configure(:messages,
|> stream_configure(:messages, dom_id: fn
dom_id: fn %Message{id: id} -> "messages-#{id}"
%Message{id: id} -> "messages-#{id}" :unread_marker -> "messages-unread-marker"
:unread_marker -> "messages-unread-marker" end
end )
) |> ok()
{:ok, socket}
end end
@impl Phoenix.LiveView @impl Phoenix.LiveView
@ -274,28 +272,28 @@ defmodule SlaxWeb.ChatRoomLive do
Chat.update_last_read_id(room, socket.assigns.current_user) Chat.update_last_read_id(room, socket.assigns.current_user)
{:noreply, socket
socket |> assign(
|> assign( hide_topic?: false,
hide_topic?: false, joined?: Chat.joined?(room, socket.assigns.current_user),
joined?: Chat.joined?(room, socket.assigns.current_user), page_title: "# #{room.name}",
page_title: "# #{room.name}", room: room
room: room )
) |> stream(:messages, messages, reset: true)
|> stream(:messages, messages, reset: true) |> assign_message_form(Chat.change_message(%Message{}))
|> assign_message_form(Chat.change_message(%Message{})) |> push_event("scroll_messages_to_bottom", %{})
|> push_event("scroll_messages_to_bottom", %{}) |> update(:rooms, fn rooms ->
|> update(:rooms, fn rooms -> room_id = room.id
room_id = room.id
Enum.map(rooms, fn Enum.map(rooms, fn
%Room{id: ^room_id} = room -> %Room{id: ^room_id} = room ->
%Room{room | unread_message_count: 0} %Room{room | unread_message_count: 0}
other_room -> other_room ->
other_room other_room
end) end)
end)} end)
|> noreply()
end end
@impl Phoenix.LiveView @impl Phoenix.LiveView

View File

@ -43,33 +43,32 @@ defmodule SlaxWeb.ChatRoomLive.Edit do
|> push_navigate(to: ~p"/") |> push_navigate(to: ~p"/")
end end
{:ok, socket} ok(socket)
end end
@impl Phoenix.LiveView @impl Phoenix.LiveView
def handle_event("save-room", %{"room" => room_params}, socket) do def handle_event("save-room", %{"room" => room_params}, socket) do
socket = socket.assigns.room
case Chat.update_room(socket.assigns.room, room_params) do |> Chat.update_room(room_params)
{:ok, room} -> |> case do
socket {:ok, room} ->
|> put_flash(:info, "Room #{room.name} updated succesfully") socket
|> push_navigate(to: ~p"/rooms/#{room}") |> put_flash(:info, "Room #{room.name} updated succesfully")
|> push_navigate(to: ~p"/rooms/#{room}")
{:error, changeset} -> {:error, changeset} ->
assign_form(socket, changeset) assign_form(socket, changeset)
end end
|> noreply()
{:noreply, socket}
end end
@impl Phoenix.LiveView @impl Phoenix.LiveView
def handle_event("validate-room", %{"room" => room_params}, socket) do def handle_event("validate-room", %{"room" => room_params}, socket) do
changeset = socket.assigns.room
socket.assigns.room |> Chat.change_room(room_params)
|> Chat.change_room(room_params) |> Map.put(:action, :validate)
|> Map.put(:action, :validate) |> then(&assign_form(socket, &1))
|> noreply()
{:noreply, assign_form(socket, changeset)}
end end
defp assign_form(socket, %Ecto.Changeset{} = changeset) do defp assign_form(socket, %Ecto.Changeset{} = changeset) do

View File

@ -62,13 +62,11 @@ defmodule SlaxWeb.ChatRoomLive.Index do
def mount(_params, _session, socket) do def mount(_params, _session, socket) do
rooms = Chat.list_rooms_with_joined(socket.assigns.current_user) rooms = Chat.list_rooms_with_joined(socket.assigns.current_user)
socket = socket
socket |> assign(page_title: "All rooms")
|> assign(page_title: "All rooms") |> stream_configure(:rooms, dom_id: fn {room, _joined?} -> "rooms-#{room.id}" end)
|> stream_configure(:rooms, dom_id: fn {room, _joined?} -> "rooms-#{room.id}" end) |> stream(:rooms, rooms)
|> stream(:rooms, rooms) |> ok()
{:ok, socket}
end end
@impl Phoenix.LiveView @impl Phoenix.LiveView
@ -78,7 +76,9 @@ defmodule SlaxWeb.ChatRoomLive.Index do
|> Chat.get_room!() |> Chat.get_room!()
|> Chat.toggle_room_membership(socket.assigns.current_user) |> Chat.toggle_room_membership(socket.assigns.current_user)
{:noreply, stream_insert(socket, :rooms, {room, joined?})} socket
|> stream_insert(:rooms, {room, joined?})
|> noreply()
end end
defp open_room(room) do defp open_room(room) do

View File

@ -0,0 +1,9 @@
defmodule SlaxWeb.SocketHelpers do
@moduledoc """
Helper functions to encapsulate a socket into proper tuples.
"""
def ok(socket), do: {:ok, socket}
def noreply(socket), do: {:noreply, socket}
end