Skip to content

Commit

Permalink
Use patch for chat navigation to preserve back button
Browse files Browse the repository at this point in the history
  • Loading branch information
jtormey committed Jun 10, 2024
1 parent 7b47628 commit 2d3d93b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 19 deletions.
4 changes: 1 addition & 3 deletions lib/lax/chat.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
defmodule Lax.Chat do
alias Lax.Channels
alias Lax.Channels.Channel
alias Lax.Indicators
alias Lax.Messages
alias Lax.Repo
alias Lax.Users.Membership

defstruct [
Expand Down Expand Up @@ -78,7 +76,7 @@ defmodule Lax.Chat do
|> put_messages()
|> put_unread_counts()
else
load(chat.user, Repo.get!(Channel, channel_id))
raise "Tried to join a channel that is not preloaded"
end
end

Expand Down
34 changes: 20 additions & 14 deletions lib/lax_web/live/chat_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ defmodule LaxWeb.ChatLive do
name={channel.name}
selected={Chat.current?(@chat, channel)}
active={Chat.has_activity?(@chat, channel)}
phx-click={JS.push("select_channel", value: %{id: channel.id})}
phx-click={JS.patch(~p"/chat/#{channel.id}")}
/>
</.sidebar_section>
Expand All @@ -46,7 +46,7 @@ defmodule LaxWeb.ChatLive do
active={Chat.has_activity?(@chat, channel)}
online_fun={&LaxWeb.Presence.Live.online?(assigns, &1)}
unread_count={Chat.unread_count(@chat, channel)}
phx-click={JS.push("select_channel", value: %{id: channel.id})}
phx-click={JS.patch(~p"/chat/#{channel.id}")}
/>
</.sidebar_section>
Expand Down Expand Up @@ -114,10 +114,20 @@ defmodule LaxWeb.ChatLive do
|> assign(:domain, :home)
|> assign(:modal, nil)
|> assign(:chat, Chat.load(socket.assigns.current_user))
|> put_page_title()
|> LaxWeb.Presence.Live.track_online_users()}
end

def handle_params(%{"id" => channel_id}, _uri, socket) do
{:noreply,
socket
|> update(:chat, &Chat.select_channel(&1, channel_id))
|> put_page_title()}
end

def handle_params(_params, _uri, socket) do
{:noreply, put_page_title(socket)}
end

def handle_event("resize", %{"width" => width}, socket) do
if user = socket.assigns.current_user do
{:ok, user} = Users.update_user_ui_settings(user, %{channels_sidebar_width: width})
Expand All @@ -139,26 +149,22 @@ defmodule LaxWeb.ChatLive do
{:noreply, assign(socket, :modal, nil)}
end

def handle_event("select_channel", %{"id" => channel_id}, socket) do
{:noreply,
socket
|> update(:chat, &Chat.select_channel(&1, channel_id))
|> put_page_title()}
end

def handle_event("delete_message", %{"id" => message_id}, socket) do
{:noreply, update(socket, :chat, &Chat.delete_message(&1, message_id))}
end

def handle_info({ChannelFormComponent, {:create_channel, _channel}}, socket) do
{:noreply, assign(socket, :modal, nil)}
def handle_info({ChannelFormComponent, {:create_channel, channel}}, socket) do
{:noreply,
socket
|> assign(:modal, nil)
|> push_patch(to: ~p"/chat/#{channel}")}
end

def handle_info({Lax.Channels, {:new_channel, _channel}}, socket) do
def handle_info({ManageChannelsComponent, :update_channels}, socket) do
{:noreply, update(socket, :chat, &Chat.reload_channels(&1))}
end

def handle_info({ManageChannelsComponent, :update_channels}, socket) do
def handle_info({Lax.Channels, {:new_channel, _channel}}, socket) do
{:noreply, update(socket, :chat, &Chat.reload_channels(&1))}
end

Expand Down
2 changes: 1 addition & 1 deletion lib/lax_web/live/chat_live/channel_form_component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ defmodule LaxWeb.ChatLive.ChannelFormComponent do
def handle_event("submit", %{"channel" => params}, socket) do
case Channels.create_and_join(socket.assigns.current_user, params) do
{:ok, channel} ->
send(self(), {__MODULE__, {:create_channel, channel}})
Channels.broadcast_new_channel(socket.assigns.current_user, channel)
send(self(), {__MODULE__, {:create_channel, channel}})
{:noreply, socket}

{:error, %Ecto.Changeset{} = changeset} ->
Expand Down
8 changes: 8 additions & 0 deletions lib/lax_web/live/direct_message_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule LaxWeb.DirectMessageLive do
alias Lax.Chat
alias Lax.Messages.Message
alias Lax.Users
alias LaxWeb.DirectMessageLive.NewDirectMessageComponent

import LaxWeb.ChatLive.Components
import LaxWeb.DirectMessageLive.Components
Expand Down Expand Up @@ -117,6 +118,13 @@ defmodule LaxWeb.DirectMessageLive do
{:noreply, update(socket, :chat, &Chat.delete_message(&1, message_id))}
end

def handle_info({NewDirectMessageComponent, {:create_direct_message, channel}}, socket) do
{:noreply,
socket
|> assign(:modal, nil)
|> push_patch(to: ~p"/direct-messages/#{channel}")}
end

def handle_info({Lax.Channels, {:new_channel, _channel}}, socket) do
{:noreply, update(socket, :chat, &Chat.reload_channels(&1))}
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ defmodule LaxWeb.DirectMessageLive.NewDirectMessageComponent do
{:ok, message} = Messages.send(channel, socket.assigns.current_user, %{text: text})
Messages.broadcast_sent_message(channel, message)

{:noreply, push_patch(socket, to: ~p"/direct-messages/#{channel}")}
send(self(), {__MODULE__, {:create_direct_message, channel}})
{:noreply, socket}

{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, put_form(socket, changeset)}
Expand Down
1 change: 1 addition & 0 deletions lib/lax_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ defmodule LaxWeb.Router do
live_session :current_user,
on_mount: [{LaxWeb.UserAuth, :mount_current_user}] do
live "/", ChatLive, :chat
live "/chat/:id", ChatLive, :chat
live "/direct-messages", DirectMessageLive, :new
live "/direct-messages/:id", DirectMessageLive, :show
live "/users/confirm/:token", UserConfirmationLive, :edit
Expand Down

0 comments on commit 2d3d93b

Please sign in to comment.