feat(chat): validate and submit messages
This commit is contained in:
parent
6581aa3ba5
commit
b2e04be7ec
@ -11,7 +11,7 @@ defmodule Slax.Chat do
|
|||||||
|
|
||||||
def create_room(attrs) do
|
def create_room(attrs) do
|
||||||
%Room{}
|
%Room{}
|
||||||
|> Room.changeset(attrs)
|
|> change_room(attrs)
|
||||||
|> Repo.insert()
|
|> Repo.insert()
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -50,4 +50,14 @@ defmodule Slax.Chat do
|
|||||||
|
|
||||||
Repo.all(query)
|
Repo.all(query)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def create_message(room, user, attrs) do
|
||||||
|
%Message{room: room, user: user}
|
||||||
|
|> change_message(attrs)
|
||||||
|
|> Repo.insert()
|
||||||
|
end
|
||||||
|
|
||||||
|
def change_message(message, attrs \\ %{}) do
|
||||||
|
Message.changeset(message, attrs)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
@ -16,7 +16,7 @@ defmodule Slax.Chat.Message do
|
|||||||
@doc false
|
@doc false
|
||||||
def changeset(message, attrs) do
|
def changeset(message, attrs) do
|
||||||
message
|
message
|
||||||
|> cast(attrs, [:user_id, :room_id, :body])
|
|> cast(attrs, [:body])
|
||||||
|> validate_required([:user_id, :room_id, :body])
|
|> validate_required([:body])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -89,6 +89,28 @@ defmodule SlaxWeb.ChatRoomLive do
|
|||||||
<div class="flex flex-col flex-grow overflow-auto">
|
<div class="flex flex-col flex-grow overflow-auto">
|
||||||
<.message :for={message <- @messages} message={message} />
|
<.message :for={message <- @messages} message={message} />
|
||||||
</div>
|
</div>
|
||||||
|
<div class="h-12 bg-white px-4 pb-4">
|
||||||
|
<.form
|
||||||
|
id="new-message-form"
|
||||||
|
for={@new_message_form}
|
||||||
|
phx-change="validate-message"
|
||||||
|
phx-submit="submit-message"
|
||||||
|
class="flex items-center border-2 border-slate-300 rounded-sm p-1"
|
||||||
|
>
|
||||||
|
<textarea
|
||||||
|
class="flex-grow text-sm px-3 border-1 border-slate-300 mx-1 resize-none"
|
||||||
|
cols=""
|
||||||
|
id="chat-message-textarea"
|
||||||
|
name={@new_message_form[:body].name}
|
||||||
|
placeholder={"Message ##{@room.name}"}
|
||||||
|
phx-debounce
|
||||||
|
rows="1"
|
||||||
|
><%= Phoenix.HTML.Form.normalize_value("textarea", @new_message_form[:body].value) %></textarea>
|
||||||
|
<button class="flex-shrink flex items-center justify-center h-6 w-6 rounded hover:bg-slate-200">
|
||||||
|
<.icon name="hero-paper-airplane" class="h-4 w-4" />
|
||||||
|
</button>
|
||||||
|
</.form>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
"""
|
"""
|
||||||
end
|
end
|
||||||
@ -114,12 +136,9 @@ defmodule SlaxWeb.ChatRoomLive do
|
|||||||
messages = Chat.list_messages_in_room(room)
|
messages = Chat.list_messages_in_room(room)
|
||||||
|
|
||||||
{:noreply,
|
{:noreply,
|
||||||
assign(socket,
|
socket
|
||||||
hide_topic?: false,
|
|> assign(hide_topic?: false, messages: messages, page_title: "# #{room.name}", room: room)
|
||||||
messages: messages,
|
|> assign_message_form(Chat.change_message(%Message{}))}
|
||||||
page_title: "# #{room.name}",
|
|
||||||
room: room
|
|
||||||
)}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl Phoenix.LiveView
|
@impl Phoenix.LiveView
|
||||||
@ -127,6 +146,30 @@ defmodule SlaxWeb.ChatRoomLive do
|
|||||||
{:noreply, update(socket, :hide_topic?, &(!&1))}
|
{:noreply, update(socket, :hide_topic?, &(!&1))}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@impl Phoenix.LiveView
|
||||||
|
def handle_event("validate-message", %{"message" => message_params}, socket) do
|
||||||
|
changeset = Chat.change_message(%Message{}, message_params)
|
||||||
|
{:noreply, assign_message_form(socket, changeset)}
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl Phoenix.LiveView
|
||||||
|
def handle_event("submit-message", %{"message" => message_params}, socket) do
|
||||||
|
%{current_user: user, room: room} = socket.assigns
|
||||||
|
|
||||||
|
socket =
|
||||||
|
case Chat.create_message(room, user, message_params) do
|
||||||
|
{:ok, message} ->
|
||||||
|
socket
|
||||||
|
|> update(:messages, &(&1 ++ [message]))
|
||||||
|
|> assign_message_form(Chat.change_message(%Message{}))
|
||||||
|
|
||||||
|
{:error, changeset} ->
|
||||||
|
assign_message_form(socket, changeset)
|
||||||
|
end
|
||||||
|
|
||||||
|
{:noreply, socket}
|
||||||
|
end
|
||||||
|
|
||||||
attr :active, :boolean, required: true
|
attr :active, :boolean, required: true
|
||||||
attr :room, Room, required: true
|
attr :room, Room, required: true
|
||||||
|
|
||||||
@ -169,4 +212,8 @@ defmodule SlaxWeb.ChatRoomLive do
|
|||||||
defp username(user) do
|
defp username(user) do
|
||||||
user.email |> String.split("@") |> List.first() |> String.capitalize()
|
user.email |> String.split("@") |> List.first() |> String.capitalize()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp assign_message_form(socket, changeset) do
|
||||||
|
assign(socket, :new_message_form, to_form(changeset))
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user