feat(chat): add form to new room modal

This commit is contained in:
João Paulo Dubas 2025-01-30 00:47:50 +00:00
parent 33f36a5254
commit cf925bb87b
Signed by: joao.dubas
SSH Key Fingerprint: SHA256:V1mixgOGRc/YMhGx/DNkOSmJxgA2vHNrDZEk3wt/kOA

View File

@ -211,7 +211,18 @@ defmodule SlaxWeb.ChatRoomLive do
</div>
<.modal id="new-room-modal">
<.header>New chat room</.header>
(Form goes here)
<.simple_form
for={@new_room_form}
id="room-form"
phx-change="validate-room"
phx-submit="save-room"
>
<.input field={@new_room_form[:name]} type="text" label="Name" phx-debounce />
<.input field={@new_room_form[:topic]} type="text" label="Topic" phx-debounce />
<:actions>
<.button phx-disable-with="Saving..." class="w-full">Save</.button>
</:actions>
</.simple_form>
</.modal>
"""
end
@ -240,6 +251,7 @@ defmodule SlaxWeb.ChatRoomLive do
timezone: timezone,
users: users
)
|> assign_room_form(Chat.change_room(%Room{}))
|> stream_configure(:messages,
dom_id: fn
%Message{id: id} -> "messages-#{id}"
@ -302,6 +314,16 @@ defmodule SlaxWeb.ChatRoomLive do
{:noreply, assign_message_form(socket, changeset)}
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_room_form(socket, changeset)}
end
@impl Phoenix.LiveView
def handle_event("submit-message", %{"message" => message_params}, socket) do
%{current_user: user, room: room} = socket.assigns
@ -342,6 +364,22 @@ defmodule SlaxWeb.ChatRoomLive do
{:noreply, socket}
end
@impl Phoenix.LiveView
def handle_event("save-room", %{"room" => room_params}, socket) do
case Chat.create_room(room_params) do
{:ok, room} ->
Chat.join_room!(room, socket.assigns.current_user)
{:noreply,
socket
|> put_flash(:info, "Created room")
|> push_navigate(to: ~p"/rooms/#{room}")}
{:error, changeset} ->
{:noreply, assign_room_form(socket, changeset)}
end
end
@impl Phoenix.LiveView
def handle_info({:new_message, message}, socket) do
room = socket.assigns.room
@ -386,6 +424,10 @@ defmodule SlaxWeb.ChatRoomLive do
{:noreply, assign(socket, online_users: online_users)}
end
defp assign_room_form(socket, changeset) do
assign(socket, :new_room_form, to_form(changeset))
end
attr :active, :boolean, required: true
attr :room, Room, required: true