From a58edec8c40b8bda2dc1a2479b1c6885f20beec8 Mon Sep 17 00:00:00 2001 From: nelsonic Date: Wed, 1 Nov 2023 17:27:53 +0000 Subject: [PATCH] add item to custom list #425 for #165 --- lib/app/list.ex | 4 +- lib/app_web/controllers/auth_controller.ex | 2 - lib/app_web/controllers/list_controller.ex | 2 +- lib/app_web/controllers/tag_controller.ex | 3 +- lib/app_web/live/app_live.ex | 47 ++++++++++++++++------ lib/app_web/live/stats_live.ex | 3 +- lib/app_web/templates/nav/nav.html.heex | 22 +++++----- 7 files changed, 55 insertions(+), 28 deletions(-) diff --git a/lib/app/list.ex b/lib/app/list.ex index fbd4ff6f..5340c244 100644 --- a/lib/app/list.ex +++ b/lib/app/list.ex @@ -177,8 +177,8 @@ defmodule App.List do `add_item_to_list/3` adds the `item.cid` to the `list.cid` for the given `person_id`. """ def add_item_to_list(item_cid, list_cid, person_id) do - list = get_list_by_cid!(list_cid) - prev_seq = get_list_seq(list) + list = get_list_by_cid!(list_cid) |> dbg() + prev_seq = get_list_seq(list) |> dbg() seq = [item_cid | prev_seq] |> Enum.join(",") update_list(list, %{seq: seq, person_id: person_id}) end diff --git a/lib/app_web/controllers/auth_controller.ex b/lib/app_web/controllers/auth_controller.ex index 02c47abb..2a750f44 100644 --- a/lib/app_web/controllers/auth_controller.ex +++ b/lib/app_web/controllers/auth_controller.ex @@ -3,8 +3,6 @@ defmodule AppWeb.AuthController do import Phoenix.Component, only: [assign_new: 3] def on_mount(:default, _params, %{"jwt" => jwt} = _session, socket) do - dbg(jwt) - {:cont, AuthPlug.assign_jwt_to_socket(socket, &Phoenix.Component.assign_new/3, jwt)} end diff --git a/lib/app_web/controllers/list_controller.ex b/lib/app_web/controllers/list_controller.ex index b2103ca8..f841378f 100644 --- a/lib/app_web/controllers/list_controller.ex +++ b/lib/app_web/controllers/list_controller.ex @@ -7,7 +7,7 @@ defmodule AppWeb.ListController do person_id = conn.assigns[:person][:id] || 0 lists = List.get_lists_for_person(person_id) - render(conn, "index.html", lists: lists) + render(conn, "index.html", lists: lists, custom_list: false) end def new(conn, _params) do diff --git a/lib/app_web/controllers/tag_controller.ex b/lib/app_web/controllers/tag_controller.ex index 478556ad..0fa43d67 100644 --- a/lib/app_web/controllers/tag_controller.ex +++ b/lib/app_web/controllers/tag_controller.ex @@ -9,7 +9,8 @@ defmodule AppWeb.TagController do render(conn, "index.html", tags: tags, - lists: App.List.get_lists_for_person(person_id) + lists: App.List.get_lists_for_person(person_id), + custom_list: false ) end diff --git a/lib/app_web/live/app_live.ex b/lib/app_web/live/app_live.ex index 4d8e8129..be44e4fb 100644 --- a/lib/app_web/live/app_live.ex +++ b/lib/app_web/live/app_live.ex @@ -13,23 +13,39 @@ defmodule AppWeb.AppLive do @stats_topic "stats" defp get_list_cid(assigns), do: assigns[:list_cid] + defp get_list_name(assigns), do: assigns[:list_name] + + defp list_cid_from_url_params(params) do + dbg(Map.get(params, "list_cid")) + if Map.has_key?(params, "list_cid"), do: Map.get(params, "list_cid", nil) + end @impl true - def mount(_params, _session, socket) do + def mount(params, _session, socket) do # subscribe to the channel if connected?(socket), do: AppWeb.Endpoint.subscribe(@topic) AppWeb.Endpoint.subscribe(@stats_topic) person_id = Person.get_person_id(socket.assigns) - # Create or Get the "all" list for the person_id - all_list = App.List.get_all_list_for_person(person_id) + custom_list = list_cid_from_url_params(params) + list_cid = if custom_list == nil do + # Create or Get the "all" list for the person_id + all_list = App.List.get_all_list_for_person(person_id) - # Temporary function to add All *existing* items to the "All" list: - App.List.add_all_items_to_all_list_for_person_id(person_id) + # Temporary function to add All *existing* items to the "All" list: + App.List.add_all_items_to_all_list_for_person_id(person_id) + + # return the "all" list cid + all_list.cid + else + custom_list + end + lists = App.List.get_lists_for_person(person_id) + list = Enum.find(lists, fn list -> list.cid == list_cid end) # Assigns - items = Item.items_with_timers(person_id) + items = Item.items_with_timers(person_id, list_cid) tags = Tag.list_person_tags(person_id) selected_tags = [] draft_item = Item.get_draft_item(person_id) @@ -41,8 +57,10 @@ defmodule AppWeb.AppLive do editing: nil, filter: "active", filter_tag: nil, - list_cid: all_list.cid, - lists: App.List.get_lists_for_person(person_id), + custom_list: custom_list, + list_cid: list_cid, + list_name: list.name, + lists: lists, tags: tags, selected_tags: selected_tags, text_value: draft_item.text || "", @@ -65,7 +83,7 @@ defmodule AppWeb.AppLive do def handle_event("create", %{"text" => text}, socket) do person_id = Person.get_person_id(socket.assigns) - {:ok, %{model: _item}} = + {:ok, %{model: item}} = Item.create_item_with_tags(%{ text: text, person_id: person_id, @@ -73,8 +91,12 @@ defmodule AppWeb.AppLive do tags: socket.assigns.selected_tags }) - # Add this newly created `item` to the "All" list: - # App.ListItem.add_item_to_all_list(item) + # Add this newly created `item` to the list current list: + list_cid = get_list_cid(socket.assigns) + list_name = get_list_name(socket.assigns) + if list_name !== "all" do + App.List.add_item_to_list(item.cid, list_cid, person_id) + end draft = Item.get_draft_item(person_id) Item.update_draft(draft, %{text: ""}) @@ -347,7 +369,8 @@ defmodule AppWeb.AppLive do @impl true def handle_info(%Broadcast{event: "update", payload: payload}, socket) do person_id = Person.get_person_id(socket.assigns) - items = Item.items_with_timers(person_id) + list_cid = get_list_cid(socket.assigns) + items = Item.items_with_timers(person_id, list_cid) is_editing_item = socket.assigns.editing # If the item is being edited, we update the timer list of the item being edited. diff --git a/lib/app_web/live/stats_live.ex b/lib/app_web/live/stats_live.ex index f121f04a..92dd8d68 100644 --- a/lib/app_web/live/stats_live.ex +++ b/lib/app_web/live/stats_live.ex @@ -23,7 +23,8 @@ defmodule AppWeb.StatsLive do metrics: metrics, sort_column: :person_id, sort_order: :asc, - lists: App.List.get_lists_for_person(person_id) + lists: App.List.get_lists_for_person(person_id), + custom_list: false )} end diff --git a/lib/app_web/templates/nav/nav.html.heex b/lib/app_web/templates/nav/nav.html.heex index cc6f8ae7..1c65ecaa 100644 --- a/lib/app_web/templates/nav/nav.html.heex +++ b/lib/app_web/templates/nav/nav.html.heex @@ -59,16 +59,20 @@
- - dwyl logo - + <%= if @custom_list do %> +

<%= list_name(@list_name) %>

+ <% else %> + + dwyl logo + + <% end %> +
-