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:
parent
2063870a9e
commit
489be57db7
@ -55,6 +55,8 @@ defmodule SlaxWeb do
|
||||
use Phoenix.LiveView,
|
||||
layout: {SlaxWeb.Layouts, :app}
|
||||
|
||||
import SlaxWeb.SocketHelpers, only: [ok: 1, noreply: 1]
|
||||
|
||||
unquote(html_helpers())
|
||||
end
|
||||
end
|
||||
|
@ -237,7 +237,6 @@ defmodule SlaxWeb.ChatRoomLive do
|
||||
|
||||
Enum.each(rooms, &Chat.subscribe_to_room/1)
|
||||
|
||||
socket =
|
||||
socket
|
||||
|> assign(
|
||||
hide_topic?: false,
|
||||
@ -253,8 +252,7 @@ defmodule SlaxWeb.ChatRoomLive do
|
||||
:unread_marker -> "messages-unread-marker"
|
||||
end
|
||||
)
|
||||
|
||||
{:ok, socket}
|
||||
|> ok()
|
||||
end
|
||||
|
||||
@impl Phoenix.LiveView
|
||||
@ -274,7 +272,6 @@ defmodule SlaxWeb.ChatRoomLive do
|
||||
|
||||
Chat.update_last_read_id(room, socket.assigns.current_user)
|
||||
|
||||
{:noreply,
|
||||
socket
|
||||
|> assign(
|
||||
hide_topic?: false,
|
||||
@ -295,7 +292,8 @@ defmodule SlaxWeb.ChatRoomLive do
|
||||
other_room ->
|
||||
other_room
|
||||
end)
|
||||
end)}
|
||||
end)
|
||||
|> noreply()
|
||||
end
|
||||
|
||||
@impl Phoenix.LiveView
|
||||
|
@ -43,13 +43,14 @@ defmodule SlaxWeb.ChatRoomLive.Edit do
|
||||
|> push_navigate(to: ~p"/")
|
||||
end
|
||||
|
||||
{:ok, socket}
|
||||
ok(socket)
|
||||
end
|
||||
|
||||
@impl Phoenix.LiveView
|
||||
def handle_event("save-room", %{"room" => room_params}, socket) do
|
||||
socket =
|
||||
case Chat.update_room(socket.assigns.room, room_params) do
|
||||
socket.assigns.room
|
||||
|> Chat.update_room(room_params)
|
||||
|> case do
|
||||
{:ok, room} ->
|
||||
socket
|
||||
|> put_flash(:info, "Room #{room.name} updated succesfully")
|
||||
@ -58,18 +59,16 @@ defmodule SlaxWeb.ChatRoomLive.Edit do
|
||||
{:error, changeset} ->
|
||||
assign_form(socket, changeset)
|
||||
end
|
||||
|
||||
{:noreply, socket}
|
||||
|> noreply()
|
||||
end
|
||||
|
||||
@impl Phoenix.LiveView
|
||||
def handle_event("validate-room", %{"room" => room_params}, socket) do
|
||||
changeset =
|
||||
socket.assigns.room
|
||||
|> Chat.change_room(room_params)
|
||||
|> Map.put(:action, :validate)
|
||||
|
||||
{:noreply, assign_form(socket, changeset)}
|
||||
|> then(&assign_form(socket, &1))
|
||||
|> noreply()
|
||||
end
|
||||
|
||||
defp assign_form(socket, %Ecto.Changeset{} = changeset) do
|
||||
|
@ -62,13 +62,11 @@ defmodule SlaxWeb.ChatRoomLive.Index do
|
||||
def mount(_params, _session, socket) do
|
||||
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}
|
||||
|> ok()
|
||||
end
|
||||
|
||||
@impl Phoenix.LiveView
|
||||
@ -78,7 +76,9 @@ defmodule SlaxWeb.ChatRoomLive.Index do
|
||||
|> Chat.get_room!()
|
||||
|> Chat.toggle_room_membership(socket.assigns.current_user)
|
||||
|
||||
{:noreply, stream_insert(socket, :rooms, {room, joined?})}
|
||||
socket
|
||||
|> stream_insert(:rooms, {room, joined?})
|
||||
|> noreply()
|
||||
end
|
||||
|
||||
defp open_room(room) do
|
||||
|
9
lib/slax_web/live/socket_helpers.ex
Normal file
9
lib/slax_web/live/socket_helpers.ex
Normal 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
|
Loading…
x
Reference in New Issue
Block a user