Skip to content

Commit

Permalink
List reorg WORKING with cids!!! #145 #356 #410 (but two tests still f…
Browse files Browse the repository at this point in the history
…ailing...)
  • Loading branch information
nelsonic committed Aug 27, 2023
1 parent 22d354e commit d795b1f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
26 changes: 17 additions & 9 deletions lib/app/item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -387,21 +388,28 @@ 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 =
if is_nil(item.start),
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

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
Expand Down
4 changes: 3 additions & 1 deletion lib/app/list_items.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 5 additions & 8 deletions lib/app_web/live/app_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 || "",
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit d795b1f

Please sign in to comment.