From d795b1f2b707c0bfd4b6aaef4d0637615e859fc2 Mon Sep 17 00:00:00 2001 From: nelsonic Date: Sun, 27 Aug 2023 07:59:05 +0100 Subject: [PATCH] List reorg WORKING with cids!!! #145 #356 #410 (but two tests still failing...) --- lib/app/item.ex | 26 +++++++++++++++++--------- lib/app/list_items.ex | 4 +++- lib/app_web/live/app_live.ex | 13 +++++-------- 3 files changed, 25 insertions(+), 18 deletions(-) diff --git a/lib/app/item.ex b/lib/app/item.ex index e90524d4..4266c9cb 100644 --- a/lib/app/item.ex +++ b/lib/app/item.ex @@ -226,7 +226,8 @@ defmodule App.Item do all_list = App.List.get_all_list_for_person(person_id) # dbg(all_list) # |> Enum.join(",") - item_ids = App.ListItems.get_list_items(all_list.cid) + seq = App.ListItems.get_list_items(all_list.cid) + |> dbg() sql = """ SELECT i.id, i.cid, i.text, i.status, i.person_id, i.updated_at, @@ -240,14 +241,14 @@ defmodule App.Item do """ values = - Ecto.Adapters.SQL.query!(Repo, sql, [item_ids]) + Ecto.Adapters.SQL.query!(Repo, sql, [seq]) |> map_columns_to_values() items_tags = list_person_items(person_id) |> Enum.reduce(%{}, fn i, acc -> Map.put(acc, i.id, i) end) - accumulate_item_timers(values) + accumulate_item_timers(values, seq) |> Enum.map(fn t -> Map.put(t, :tags, items_tags[t.id].tags) end) @@ -359,7 +360,7 @@ defmodule App.Item do This function *relies* on the list of items being ordered by timer_id ASC because it "pops" the last timer and ignores it to avoid double-counting. """ - def accumulate_item_timers(items_with_timers) do + def accumulate_item_timers(items_with_timers, seq) do # e.g: %{0 => 0, 1 => 6, 2 => 5, 3 => 24, 4 => 7} timer_id_diff_map = map_timer_diff(items_with_timers) @@ -387,8 +388,8 @@ defmodule App.Item do end)} end) - # creates a nested map: %{ item.id: %{id: 1, text: "my item", etc.}} - Map.new(items_with_timers, fn item -> + # creates a nested map: %{ item.cid: %{id: 1, text: "my item", etc.}} + cid_item_map = Map.new(items_with_timers, fn item -> time_elapsed = Map.get(item_id_timer_diff_map, item.id) start = @@ -396,12 +397,19 @@ defmodule App.Item do do: nil, else: NaiveDateTime.add(item.start, -time_elapsed) - {item.id, %{item | start: start}} + {item.cid, %{item | start: start}} end) + + # return the list of items in the order of seq + Enum.map(seq, fn cid -> + dbg(cid) + cid_item_map[cid] + end) + # |> dbg() # Return the list of items without duplicates and only the last/active timer: - |> Map.values() + # Map.values(cid_item_map) # Sort list by item.id descending (ordered by timer_id ASC above) so newest item first: - |> Enum.sort_by(fn i -> i.id end, :desc) + # |> Enum.sort_by(fn i -> i.id end, :desc) end # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # diff --git a/lib/app/list_items.ex b/lib/app/list_items.ex index 02ebd52e..b8585960 100644 --- a/lib/app/list_items.ex +++ b/lib/app/list_items.ex @@ -107,14 +107,16 @@ defmodule App.ListItems do # Add add each `item.id` to the sequence of item ids: seq = Enum.reduce(all_items, prev_seq, fn i, acc -> + # Avoid adding duplicates if Enum.member?(acc, i.cid) do acc else [i.cid | acc] end end) + |> Enum.uniq() + |> Enum.filter(fn cid -> cid != nil && cid != "" end) |> Enum.join(",") - # |> dbg() create_list_items_seq(all_list.cid, person_id, seq) end diff --git a/lib/app_web/live/app_live.ex b/lib/app_web/live/app_live.ex index cf0cf96d..959fcf74 100644 --- a/lib/app_web/live/app_live.ex +++ b/lib/app_web/live/app_live.ex @@ -14,10 +14,7 @@ defmodule AppWeb.AppLive do defp get_person_id(assigns), do: assigns[:person][:id] || 0 - defp get_list_id(assigns), - do: - assigns[:list_id] || - App.List.get_all_list_for_person(get_person_id(assigns)) + defp get_list_cid(assigns), do: assigns[:list_cid] @impl true def mount(_params, _session, socket) do @@ -43,7 +40,7 @@ defmodule AppWeb.AppLive do editing: nil, filter: "active", filter_tag: nil, - list_id: all_list.id, + list_cid: all_list.cid, tags: tags, selected_tags: selected_tags, text_value: draft_item.text || "", @@ -305,14 +302,14 @@ defmodule AppWeb.AppLive do %{"seq" => seq}, socket ) do - list_id = get_list_id(socket.assigns) + list_cid = get_list_cid(socket.assigns) person_id = get_person_id(socket.assigns) IO.puts( - "updateIndexes -> seq: #{seq} | list_id: #{list_id} | person_id: #{person_id}" + "updateIndexes -> seq: #{seq} | list_id: #{list_cid} | person_id: #{person_id}" ) - App.ListItems.create_list_items_seq(list_id, person_id, seq) + App.ListItems.create_list_items_seq(list_cid, person_id, seq) {:noreply, socket} end