-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dashboard view and complete conversation creation code
- Loading branch information
1 parent
548f4fc
commit 9f41825
Showing
10 changed files
with
188 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,9 @@ | ||
defmodule CuriousMessengerWeb.PageController do | ||
use CuriousMessengerWeb, :controller | ||
|
||
alias CuriousMessenger.Auth | ||
alias CuriousMessenger.Chat.Conversation | ||
|
||
plug CuriousMessengerWeb.AssignUser, preload: :conversations | ||
plug :assign_contacts | ||
plug :assign_new_conversation_changeset | ||
|
||
def index(conn, opts \\ []) do | ||
render(conn, "index.html") | ||
end | ||
|
||
defp assign_contacts(conn, _params) do | ||
users = Auth.list_auth_users() | ||
|
||
conn | ||
|> assign(:contacts, users) | ||
end | ||
|
||
defp assign_new_conversation_changeset(conn, _params) do | ||
changeset = %Conversation{} |> Conversation.changeset(%{}) | ||
|
||
conn | ||
|> assign(:conversation_changeset, changeset) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
defmodule CuriousMessengerWeb.DashboardLive do | ||
use Phoenix.LiveView, container: {:div, [class: "row"]} | ||
use Phoenix.HTML | ||
|
||
alias CuriousMessenger.Auth | ||
alias CuriousMessenger.Chat.Conversation | ||
alias CuriousMessengerWeb.DashboardView | ||
alias CuriousMessenger.Repo | ||
alias Ecto.Changeset | ||
|
||
def render(assigns) do | ||
DashboardView.render("show.html", assigns) | ||
end | ||
|
||
def mount(%{current_user: current_user}, socket) do | ||
{:ok, | ||
socket | ||
|> assign(current_user: current_user) | ||
|> assign_new_conversation_changeset() | ||
|> assign_contacts(current_user)} | ||
end | ||
|
||
def handle_event( | ||
"create_conversation", | ||
%{"conversation" => conversation_form}, | ||
%{ | ||
assigns: %{ | ||
conversation_changeset: changeset, | ||
current_user: current_user, | ||
contacts: contacts | ||
} | ||
} = socket | ||
) do | ||
conversation_form = | ||
Map.put( | ||
conversation_form, | ||
"title", | ||
if(conversation_form["title"] == "", | ||
do: build_title(changeset, contacts), | ||
else: conversation_form["title"] | ||
) | ||
) | ||
|
||
{:ok, _} = | ||
%Conversation{} | ||
|> Conversation.changeset(conversation_form) | ||
|> Repo.insert() | ||
|
||
{:noreply, | ||
assign( | ||
socket, | ||
:current_user, | ||
Repo.preload(current_user, :conversations, force: true) | ||
)} | ||
end | ||
|
||
def handle_event( | ||
"add_member", | ||
%{"user-id" => new_member_id}, | ||
%{assigns: %{conversation_changeset: changeset}} = socket | ||
) do | ||
{:ok, new_member_id} = Ecto.Type.cast(:integer, new_member_id) | ||
|
||
old_members = socket.assigns[:conversation_changeset].changes.conversation_members | ||
existing_ids = old_members |> Enum.map(& &1.changes.user_id) | ||
|
||
cond do | ||
new_member_id not in existing_ids -> | ||
new_members = [%{user_id: new_member_id} | old_members] | ||
|
||
new_changeset = Changeset.put_change(changeset, :conversation_members, new_members) | ||
|
||
{:noreply, assign(socket, :conversation_changeset, new_changeset)} | ||
|
||
true -> | ||
{:noreply, socket} | ||
end | ||
end | ||
|
||
def handle_event( | ||
"remove_member", | ||
%{"user-id" => removed_member_id}, | ||
%{assigns: %{conversation_changeset: changeset}} = socket | ||
) do | ||
{:ok, removed_member_id} = Ecto.Type.cast(:integer, removed_member_id) | ||
|
||
old_members = socket.assigns[:conversation_changeset].changes.conversation_members | ||
new_members = old_members |> Enum.reject(&(&1.changes[:user_id] == removed_member_id)) | ||
|
||
new_changeset = Changeset.put_change(changeset, :conversation_members, new_members) | ||
|
||
{:noreply, assign(socket, :conversation_changeset, new_changeset)} | ||
end | ||
|
||
defp assign_new_conversation_changeset(socket) do | ||
changeset = | ||
%Conversation{} | ||
|> Conversation.changeset(%{ | ||
"conversation_members" => [%{owner: true, user_id: socket.assigns[:current_user].id}] | ||
}) | ||
|
||
assign(socket, :conversation_changeset, changeset) | ||
end | ||
|
||
defp assign_contacts(socket, current_user) do | ||
users = Auth.list_auth_users() | ||
|
||
assign(socket, :contacts, users) | ||
end | ||
|
||
defp build_title(changeset, contacts) do | ||
user_ids = Enum.map(changeset.changes.conversation_members, & &1.changes.user_id) | ||
nicknames = contacts |> Enum.filter(&(&1.id in user_ids)) |> Enum.map(& &1.nickname) | ||
Enum.join(nicknames, ", ") | ||
end | ||
end |
47 changes: 47 additions & 0 deletions
47
lib/curious_messenger_web/templates/dashboard/show.html.leex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<article class="column"> | ||
<h2>Ongoing Conversations</h2> | ||
<%= for conversation <- @current_user.conversations do %> | ||
<div> | ||
<%= link conversation.title, to: Routes.conversation_path(@socket, CuriousMessengerWeb.ConversationLive, conversation.id, @current_user.id) %> | ||
</div> | ||
<% end %> | ||
</article> | ||
|
||
<article class="column"> | ||
<h2>Create Conversation</h2> | ||
|
||
<%= form_for @conversation_changeset, | ||
"", | ||
[phx_submit: :create_conversation], fn f -> %> | ||
<p> | ||
<%= inputs_for f, :conversation_members, fn cmf -> %> | ||
<% user_id = cmf.source.changes[:user_id] %> | ||
<% nickname = @contacts |> Enum.find(&(&1.id == user_id)) |> Map.get(:nickname) %> | ||
|
||
<%= link "#{nickname} #{if user_id == @current_user.id, do: "(me)", else: "✖"} ", | ||
to: "#!", | ||
phx_click: (unless user_id == @current_user.id, do: "remove_member"), | ||
phx_value_user_id: user_id %> | ||
|
||
<%= hidden_input cmf, :user_id, value: user_id %> | ||
<% end %> | ||
</p> | ||
|
||
<p> | ||
<%= text_input f, :title, placeholder: "Title (optional)" %> | ||
<%= submit "Create", disabled: Enum.count(@conversation_changeset.changes[:conversation_members]) < 2 %> | ||
</p> | ||
|
||
<ul> | ||
<%= for user <- Enum.reject(@contacts, &(&1.id == @current_user.id)) do %> | ||
<li> | ||
<%= link user.nickname, | ||
to: "#!", | ||
phx_click: "add_member", | ||
phx_value_user_id: user.id %> | ||
</li> | ||
<% end %> | ||
</ul> | ||
<% end %> | ||
|
||
</article> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
defmodule CuriousMessengerWeb.DashboardView do | ||
use CuriousMessengerWeb, :view | ||
|
||
require IEx | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters