-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create group_list_controller to add lists to groups
- Loading branch information
Showing
7 changed files
with
97 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
defmodule AppWeb.GroupListController do | ||
use AppWeb, :controller | ||
alias App.{Group, List, Repo} | ||
import Ecto.Changeset | ||
|
||
def index(conn, %{"group_id" => group_id}) do | ||
person_id = conn.assigns[:person][:id] | ||
group = Group.get_group!(group_id) | ||
|
||
lists = | ||
List.list_person_lists(person_id) | ||
|> Enum.concat(group.lists) | ||
|> Enum.uniq_by(& &1.id) | ||
|> Enum.map(&{&1.name, &1.id}) | ||
|
||
selected_list_ids = Enum.map(group.lists, & &1.id) | ||
|
||
data = %{} | ||
types = %{selected_lists: {:array, :string}} | ||
params = %{selected_lists: selected_list_ids} | ||
|
||
changeset = | ||
{data, types} | ||
|> Ecto.Changeset.cast(params, Map.keys(types)) | ||
|
||
render(conn, "index.html", | ||
group: group, | ||
lists: lists, | ||
changeset: changeset | ||
) | ||
|
||
render(conn, "index.html", group: group) | ||
end | ||
|
||
def create(conn, %{"group_id" => group_id} = params) do | ||
group = Group.get_group!(group_id) | ||
|
||
list_ids = | ||
case params["group_lists"]["selected_lists"] do | ||
"" -> [] | ||
ids -> ids | ||
end | ||
|
||
{:ok, _group} = Group.update_group_with_lists(group, list_ids) | ||
|
||
conn | ||
|> put_flash(:info, "Item's list updated successfully.") | ||
|> redirect(to: Routes.group_group_list_path(conn, :index, group)) | ||
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
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 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,20 @@ | ||
<.container> | ||
<.h2 class="text-center mt-3">Edit Group's lists</.h2> | ||
|
||
<.form let={f} for={@changeset} action={Routes.group_group_list_path(@conn, :create, @group)} as={:group_lists} class="py-3"> | ||
<%= if @changeset.action do %> | ||
<div class="alert alert-danger"> | ||
<p>Oops, something went wrong! Please check the errors below.</p> | ||
</div> | ||
<% end %> | ||
|
||
<.form_field | ||
type="checkbox_group" | ||
form={f} | ||
field={:selected_lists} | ||
label="Lists" | ||
options={@lists} | ||
/> | ||
<.button type="submit" label="Save" /> | ||
</.form> | ||
</.container> |
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,3 @@ | ||
defmodule AppWeb.GroupListView do | ||
use AppWeb, :view | ||
end |