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:
@@ -43,33 +43,32 @@ 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
|
||||
{:ok, room} ->
|
||||
socket
|
||||
|> put_flash(:info, "Room #{room.name} updated succesfully")
|
||||
|> push_navigate(to: ~p"/rooms/#{room}")
|
||||
socket.assigns.room
|
||||
|> Chat.update_room(room_params)
|
||||
|> case do
|
||||
{:ok, room} ->
|
||||
socket
|
||||
|> put_flash(:info, "Room #{room.name} updated succesfully")
|
||||
|> push_navigate(to: ~p"/rooms/#{room}")
|
||||
|
||||
{:error, changeset} ->
|
||||
assign_form(socket, changeset)
|
||||
end
|
||||
|
||||
{:noreply, socket}
|
||||
{:error, changeset} ->
|
||||
assign_form(socket, changeset)
|
||||
end
|
||||
|> 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)}
|
||||
socket.assigns.room
|
||||
|> Chat.change_room(room_params)
|
||||
|> Map.put(:action, :validate)
|
||||
|> then(&assign_form(socket, &1))
|
||||
|> noreply()
|
||||
end
|
||||
|
||||
defp assign_form(socket, %Ecto.Changeset{} = changeset) do
|
||||
|
Reference in New Issue
Block a user