Skip to content

Commit

Permalink
Scaffold chat view
Browse files Browse the repository at this point in the history
  • Loading branch information
tizpuppi committed Aug 14, 2024
1 parent 0dd0d38 commit 13e3bd6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/chat_web/live/chat_live.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
defmodule ChatWeb.RoomLive do
use ChatWeb, :live_view
alias ChatWeb.MessageComponent

def mount(%{"id" => room_id}, _session, socket) do
# Join the room, subscribe to PubSub topic
IO.inspect("Joining room #{room_id}")
{:ok, assign(socket, room_id: room_id, messages: [%{id: 1, content: "Hello"}])}
end

def handle_event("send_message", %{"message" => message}, socket) do
# Broadcast the message to all users in the room
# Update the messages list
IO.inspect("Received #{message}")
{:noreply, socket}
end

def render(assigns) do
~H"""
<div>
<h1>Chat Room <%= @room_id %></h1>
<div id="messages">
<%= for message <- @messages do %>
<.live_component module={MessageComponent} id={message.id} message={message} />
<% end %>
</div>
<form phx-submit="send_message">
<input type="text" name="message" placeholder="Type your message...">
<button type="submit">Send</button>
</form>
</div>
"""
end
end
11 changes: 11 additions & 0 deletions lib/chat_web/live/message_component.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule ChatWeb.MessageComponent do
use ChatWeb, :live_component

def render(assigns) do
~H"""
<div class="message">
<span class="content"><%= @message.content %></span>
</div>
"""
end
end
2 changes: 2 additions & 0 deletions lib/chat_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ defmodule ChatWeb.Router do
pipe_through :browser

get "/", PageController, :home

live "/chat/:id", RoomLive
end

# Other scopes may use custom stacks.
Expand Down

0 comments on commit 13e3bd6

Please sign in to comment.