Skip to content

Commit ef89ff6

Browse files
Adds manage to contact group
1 parent be21c17 commit ef89ff6

File tree

11 files changed

+64
-53
lines changed

11 files changed

+64
-53
lines changed

web/commanders/contact_commander.ex

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,6 @@ defmodule Crm.ContactCommander do
6868
end
6969
end
7070

71-
def edit_contact_group(socket, sender) do
72-
group_id = socket |> select(data: "group", from: this(sender))
73-
group = Repo.get!(ContactGroup, group_id)
74-
form = "<input class='form-control' name='group' value='#{group.name}'>"
75-
note = case socket |> alert("Edit Group", form, buttons: [ok: "Save", cancel: "Cancel"]) do
76-
{ :ok, params } -> edit_group(socket, group, params["group"])
77-
{ :cancel, _ } -> "anonymous"
78-
end
79-
end
80-
81-
def edit_group(socket, group, group_param) do
82-
changeset = ContactGroup.changeset(group, %{name: group_param})
83-
case Repo.update(changeset) do
84-
{:ok, group} ->
85-
socket
86-
|> update(:text, set: group.name, on: "#group_#{group.id}")
87-
{:error, changeset} ->
88-
socket |> exec_js("alert('The name cannot be empty')")
89-
end
90-
end
91-
9271
def delete_contact_note(socket, sender) do
9372
note_id = socket |> select(data: "noteId", from: this(sender))
9473
note = Repo.get!(Note, note_id)
@@ -103,19 +82,4 @@ defmodule Crm.ContactCommander do
10382
socket
10483
socket |> delete("#article_note_id_#{note.id}")
10584
end
106-
107-
def delete_contact_group(socket, sender) do
108-
group_id = socket |> select(data: "group", from: this(sender))
109-
group = Repo.get!(ContactGroup, group_id)
110-
button = case socket |> alert("Message", "Are you Sure?", buttons: [ok: "YES", cancel: "NO"]) do
111-
{ :ok, params } -> delete_group(socket, group)
112-
{ :cancel, _ } -> "anonymous"
113-
end
114-
end
115-
116-
def delete_group(socket, group) do
117-
Repo.delete!(group)
118-
socket
119-
socket |> delete("#new_group_list_#{group.id}")
120-
end
12185
end
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
defmodule Crm.ContactGroupController do
2+
use Crm.Web, :controller
3+
4+
alias Crm.{ContactGroup}
5+
6+
def index(conn, _params) do
7+
groups = ContactGroup.all(conn.assigns.current_user.id)
8+
9+
render(conn, :index, groups: groups)
10+
end
11+
12+
def delete(conn, %{"id" => id}) do
13+
group = Repo.get!(ContactGroup, id) |> Repo.preload(:user)
14+
if group.user !== conn.assigns.current_user do
15+
conn
16+
|> put_flash(:error, "You are not the owner of that group.")
17+
|> redirect(to: "/")
18+
else
19+
Repo.delete!(group)
20+
21+
conn
22+
|> put_flash(:info, "Group #{group.name} deleted successfully.")
23+
|> redirect(to: contact_group_path(conn, :index))
24+
end
25+
end
26+
end

web/router.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ defmodule Crm.Router do
2828
resources "/contacts", ContactController
2929
resources "/users", UserController, only: [:edit, :update]
3030
get "/groups/:id", ContactController, :groups
31-
get "/stories/search", ContactController, :search
31+
get "/search/contacts", ContactController, :search
32+
end
33+
34+
scope "/manage", Crm do
35+
pipe_through [:browser, :authenticate_user]
36+
37+
resources "/groups", ContactGroupController, only: [:index, :delete]
3238
end
3339

3440
# Other scopes may use custom stacks.

web/templates/contact/_new_group.html.eex

Lines changed: 0 additions & 15 deletions
This file was deleted.

web/templates/contact/edit.html.eex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<div class="col-md-3">
22
<div class="list-group">
3+
<%= link "My Groups", to: contact_group_path(@conn, :index), class: "list-group-item" %>
34
<%= link to: contact_path(@conn, :index), class: active_group?(@conn) do %>
45
All Contacts
56
<span class="badge"><%= length(@groups) %></span>

web/templates/contact/groups.html.eex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<div class="row">
22
<div class="col-md-3">
33
<div class="list-group">
4+
<%= link "My Groups", to: contact_group_path(@conn, :index), class: "list-group-item" %>
45
<%= link to: contact_path(@conn, :index), class: active_group?(@conn) do %>
56
All Contacts
67
<span class="badge"><%= @contacts_count %></span>

web/templates/contact/index.html.eex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<div class="row">
22
<div class="col-md-3">
33
<div class="list-group">
4+
<%= link "My Groups", to: contact_group_path(@conn, :index), class: "list-group-item" %>
45
<%= link to: contact_path(@conn, :index), class: active_group?(@conn) do %>
56
All Contacts
67
<span class="badge"><%= length(@contacts) %></span>

web/templates/contact/new.html.eex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<div class="col-md-3">
22
<div class="list-group">
3+
<%= link "My Groups", to: contact_group_path(@conn, :index), class: "list-group-item" %>
34
<%= link to: contact_path(@conn, :index), class: active_group?(@conn) do %>
45
All Contacts
56
<span class="badge"><%= Crm.Contact.count(@current_user.id) %></span>
67
<% end %>
78
<div id="group_list">
8-
<%= render_many @groups, Crm.ContactView, "_new_group.html", as: :group, conn: @conn %>
9+
<%= render_many @groups, Crm.ContactView, "_group.html", as: :group, conn: @conn %>
910
</div>
1011
</div>
1112
</div><!-- /.col-md-3 -->

web/templates/contact/search.html.eex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<div class="row">
22
<div class="col-md-3">
33
<div class="list-group">
4+
<%= link "My Groups", to: contact_group_path(@conn, :index), class: "list-group-item" %>
45
<%= link to: contact_path(@conn, :index), class: active_group?(@conn) do %>
56
All Contacts
67
<span class="badge"><%= @contacts_count %></span>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<div class="container">
2+
<h3>My Groups <small class="pull-right"><%= length(@groups) %> total</small></h3>
3+
<table class="table">
4+
<thead>
5+
<tr>
6+
<th>Name</th>
7+
<th colspan="3"></th>
8+
</tr>
9+
</thead>
10+
<tbody>
11+
<%= for group <- @groups do %>
12+
<tr>
13+
<td>
14+
<%= link group.name, to: contact_path(@conn, :groups, group) %>
15+
</td>
16+
<td><%= link "Delete", to: contact_group_path(@conn, :delete, group), method: :delete, data: [confirm: "Are you sure?"], class: "text-danger" %></td>
17+
</tr>
18+
<% end %>
19+
</tbody>
20+
</table>
21+
22+
</div>

0 commit comments

Comments
 (0)