From 081ed8a243efacd553da6e558efe2232484c25bc Mon Sep 17 00:00:00 2001 From: SimonLab Date: Mon, 10 Oct 2022 14:50:48 +0100 Subject: [PATCH] Add lists to group Create group_list_controller to add lists to groups --- lib/app/group.ex | 15 ++++++ .../controllers/group_list_controller.ex | 50 +++++++++++++++++++ .../controllers/group_member_controller.ex | 13 ++--- lib/app_web/router.ex | 6 +-- lib/app_web/templates/group/index.html.heex | 2 +- .../templates/group_list/index.html.heex | 20 ++++++++ lib/app_web/views/group_list_view.ex | 3 ++ 7 files changed, 97 insertions(+), 12 deletions(-) create mode 100644 lib/app_web/controllers/group_list_controller.ex create mode 100644 lib/app_web/templates/group_list/index.html.heex create mode 100644 lib/app_web/views/group_list_view.ex diff --git a/lib/app/group.ex b/lib/app/group.ex index d65adbbc..01dc099a 100644 --- a/lib/app/group.ex +++ b/lib/app/group.ex @@ -25,6 +25,14 @@ defmodule App.Group do |> validate_required([:name]) end + def changeset_with_lists(group, list_ids) do + lists = Repo.all(from l in List, where: l.id in ^list_ids) + + group + |> change() + |> put_assoc(:lists, lists) + end + def create_group(person_id, attrs) do person = Person.get_person!(person_id) @@ -38,5 +46,12 @@ defmodule App.Group do Group |> Repo.get!(id) |> Repo.preload(people: from(p in Person, order_by: p.name)) + |> Repo.preload(lists: from(l in List, order_by: l.name)) + end + + def update_group_with_lists(%Group{} = group, list_ids) do + group + |> Group.changeset_with_lists(list_ids) + |> Repo.update() end end diff --git a/lib/app_web/controllers/group_list_controller.ex b/lib/app_web/controllers/group_list_controller.ex new file mode 100644 index 00000000..226ea494 --- /dev/null +++ b/lib/app_web/controllers/group_list_controller.ex @@ -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 diff --git a/lib/app_web/controllers/group_member_controller.ex b/lib/app_web/controllers/group_member_controller.ex index b136ef47..7f7a1cda 100644 --- a/lib/app_web/controllers/group_member_controller.ex +++ b/lib/app_web/controllers/group_member_controller.ex @@ -1,15 +1,11 @@ defmodule AppWeb.GroupMemberController do use AppWeb, :controller - alias App.{Group, Person} + alias App.{Group, Person, Repo} import Ecto.Changeset - # plug :permission_tag when action in [:edit, :update, :delete] - def index(conn, %{"group_id" => group_id}) do group = Group.get_group!(group_id) changeset = Person.changeset(%Person{}) - # person_id = conn.assigns[:person][:id] || 0 - # tags = Tag.list_person_tags(person_id) render(conn, "index.html", changeset: changeset, group: group) end @@ -22,12 +18,17 @@ defmodule AppWeb.GroupMemberController do changeset = %Person{name: name} |> change() - |> add_error(:name, "This name doesn't seem to exist") + |> add_error(:name, "Person's name not found") |> Map.put(:action, :insert) render(conn, "index.html", changeset: changeset, group: group) person -> + group + |> change() + |> put_assoc(:people, [person | group.people]) + |> Repo.update() + redirect(conn, to: Routes.group_group_member_path(conn, :index, group_id) ) diff --git a/lib/app_web/router.ex b/lib/app_web/router.ex index 35bb53f2..8bbd694c 100644 --- a/lib/app_web/router.ex +++ b/lib/app_web/router.ex @@ -41,14 +41,10 @@ defmodule AppWeb.Router do # manage groups resources "/groups", GroupController, except: [:show] - # edit and update list to group - resources "/groups", GroupController, only: [:show] do - resources "/lists", GroupListController, only: [:edit, :update] - end - # edit and update group members resources "/groups", GroupController, only: [:show] do resources "/members", GroupMemberController, only: [:index, :create] + resources "/lists", GroupListController, only: [:index, :create] end end diff --git a/lib/app_web/templates/group/index.html.heex b/lib/app_web/templates/group/index.html.heex index 43fca6e3..7358a294 100644 --- a/lib/app_web/templates/group/index.html.heex +++ b/lib/app_web/templates/group/index.html.heex @@ -19,7 +19,7 @@ <%= link "Members", to: Routes.group_group_member_path(@conn, :index, group) %> <.td> - <%= link "Lists", to: Routes.group_path(@conn, :edit, group) %> + <%= link "Lists", to: Routes.group_group_list_path(@conn, :index, group) %> <.td> <%= link "Edit", to: Routes.group_path(@conn, :edit, group) %> diff --git a/lib/app_web/templates/group_list/index.html.heex b/lib/app_web/templates/group_list/index.html.heex new file mode 100644 index 00000000..aade5142 --- /dev/null +++ b/lib/app_web/templates/group_list/index.html.heex @@ -0,0 +1,20 @@ +<.container> +<.h2 class="text-center mt-3">Edit Group's lists + +<.form let={f} for={@changeset} action={Routes.group_group_list_path(@conn, :create, @group)} as={:group_lists} class="py-3"> + <%= if @changeset.action do %> +
+

Oops, something went wrong! Please check the errors below.

+
+ <% end %> + + <.form_field + type="checkbox_group" + form={f} + field={:selected_lists} + label="Lists" + options={@lists} + /> + <.button type="submit" label="Save" /> + + diff --git a/lib/app_web/views/group_list_view.ex b/lib/app_web/views/group_list_view.ex new file mode 100644 index 00000000..c6ca4ffe --- /dev/null +++ b/lib/app_web/views/group_list_view.ex @@ -0,0 +1,3 @@ +defmodule AppWeb.GroupListView do + use AppWeb, :view +end