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