diff --git a/assets/js/app.js b/assets/js/app.js
index c06b977e..10917a36 100644
--- a/assets/js/app.js
+++ b/assets/js/app.js
@@ -35,7 +35,7 @@ Hooks.Items = {
this.el.addEventListener("update-indexes", e => {
const item_id = e.detail.fromItemId
- const list_ids = get_list_item_ids()
+ const list_ids = get_list_item_cids()
console.log("update-indexes", e.detail, "list: ", list_ids)
// Check if both "from" and "to" are defined
if(item_id && itemId_to && item_id != itemId_to) {
@@ -53,11 +53,11 @@ Hooks.Items = {
* and returns a String containing the IDs as a space-separated list e.g: "1 2 3 42 71 93"
* This is used to determine the `position` of the `item` that has been moved.
*/
-function get_list_item_ids() {
+function get_list_item_cids() {
console.log("invoke get_list_item_ids")
- const lis = document.querySelectorAll("label[phx-value-id]");
+ const lis = document.querySelectorAll("label[phx-value-cid]");
return Object.values(lis).map(li => {
- return li.attributes["phx-value-id"].nodeValue
+ return li.attributes["phx-value-cid"].nodeValue
}).join(",")
}
diff --git a/lib/app/item.ex b/lib/app/item.ex
index 2fa3e84b..e90524d4 100644
--- a/lib/app/item.ex
+++ b/lib/app/item.ex
@@ -56,6 +56,7 @@ defmodule App.Item do
%Item{}
|> changeset(attrs)
|> PaperTrail.insert(originator: %{id: Map.get(attrs, :person_id, 0)})
+ |> App.ListItems.add_papertrail_item_to_all_list()
end
@doc """
@@ -226,7 +227,6 @@ defmodule App.Item do
# dbg(all_list)
# |> Enum.join(",")
item_ids = App.ListItems.get_list_items(all_list.cid)
- # dbg(item_ids)
sql = """
SELECT i.id, i.cid, i.text, i.status, i.person_id, i.updated_at,
diff --git a/lib/app/list_items.ex b/lib/app/list_items.ex
index d611575c..02ebd52e 100644
--- a/lib/app/list_items.ex
+++ b/lib/app/list_items.ex
@@ -1,10 +1,12 @@
defmodule App.ListItems do
+ require Logger
use Ecto.Schema
import Ecto.Changeset
alias App.{Repo}
alias __MODULE__
schema "list_items" do
+ field :list_cid, :string
field :list_id, :id
field :person_id, :integer
field :seq, :string
@@ -15,8 +17,8 @@ defmodule App.ListItems do
@doc false
def changeset(list_items, attrs) do
list_items
- |> cast(attrs, [:list_id, :person_id, :seq])
- |> validate_required([:list_id, :person_id, :seq])
+ |> cast(attrs, [:list_cid, :list_id, :person_id, :seq])
+ |> validate_required([:list_cid, :person_id, :seq])
end
@doc """
@@ -26,11 +28,11 @@ defmodule App.ListItems do
and `seq` is the String of ids currently visible in the interface.
e.g: "1,2,3,42,71,93". so we can easily determine the precise position of an `item_id`.
"""
- def create_list_items_seq(list_id, person_id, seq) do
- # IO.inspect("create_list_itemix ms_seq(list_id: #{list_id}, person_id: #{person_id}, seq: #{seq})")
+ def create_list_items_seq(list_cid, person_id, seq) do
+ # IO.inspect("create_list_item_seq(list_id: #{list_cid}, person_id: #{person_id}, seq: #{seq})")
%ListItems{}
|> changeset(%{
- list_id: list_id,
+ list_cid: list_cid,
person_id: person_id,
seq: seq
})
@@ -41,21 +43,19 @@ defmodule App.ListItems do
"""
SELECT li.seq
FROM list_items li
- WHERE li.list_id = $1
- ORDER BY li.inserted_at DESC
+ WHERE li.list_cid = $1
+ ORDER BY li.id DESC
LIMIT 1
"""
end
@doc """
- `get_list_items/2` retrieves the *latest* `list_items` record for a given `list_id`.
+ `get_list_items/2` retrieves the *latest* `list_items` record for a given `list_cid`.
"""
- def get_list_items(list_id) do
- # IO.inspect("get_list_items(list_id: #{list_id})")
-
- sql = list_items_seq_sql()
-
- result = Ecto.Adapters.SQL.query!(Repo, sql, [list_id])
+ def get_list_items(list_cid) do
+ # dbg(list_cid)
+ # dbg(list_items_seq_sql())
+ result = Ecto.Adapters.SQL.query!(Repo, list_items_seq_sql(), [list_cid])
# dbg(result.rows)
if is_nil(result.rows) or result.rows == [] do
[]
@@ -67,12 +67,30 @@ defmodule App.ListItems do
@doc """
`add_list_item/3` adds an `item` to a `list` for the given `person_id`.
"""
- def add_list_item(item_id, list_id, person_id) do
+ def add_list_item(item_cid, list_cid, person_id) do
# Get latest list_items.seq for this list.id and person_id combo.
- prev_seq = get_list_items(list_id)
+ prev_seq = get_list_items(list_cid)
+ # dbg(prev_seq)
# Add the `item.id` to the sequence
- seq = [item_id | prev_seq] |> Enum.join(",")
- create_list_items_seq(list_id, person_id, seq)
+ seq = [item_cid | prev_seq] |> Enum.join(",")
+ # dbg(seq)
+ create_list_items_seq(list_cid, person_id, seq)
+ end
+
+ # feel free to refactor this to use pattern matching:
+ def add_papertrail_item_to_all_list(tuple) do
+ # extract the item from the tuple:
+ try do
+ {:ok, %{model: item}} = tuple
+ all_list = App.List.get_all_list_for_person(item.person_id)
+ App.ListItems.add_list_item(item.cid, all_list.cid, item.person_id)
+ rescue
+ e ->
+ Logger.error(Exception.format(:error, e, __STACKTRACE__))
+ end
+
+ # return the original tuple as expected downstream:
+ tuple
end
@doc """
@@ -81,16 +99,23 @@ defmodule App.ListItems do
"""
def add_all_items_to_all_list_for_person_id(person_id) do
all_list = App.List.get_all_list_for_person(person_id)
+ # dbg(all_list)
all_items = App.Item.all_items_for_person(person_id)
+ # dbg(all_items)
# The previous sequence of items if there is any:
- prev_seq = get_list_items(all_list.id)
+ prev_seq = get_list_items(all_list.cid)
# Add add each `item.id` to the sequence of item ids:
seq =
Enum.reduce(all_items, prev_seq, fn i, acc ->
- [i.id | acc]
+ if Enum.member?(acc, i.cid) do
+ acc
+ else
+ [i.cid | acc]
+ end
end)
|> Enum.join(",")
+ # |> dbg()
- create_list_items_seq(all_list.id, person_id, seq)
+ create_list_items_seq(all_list.cid, person_id, seq)
end
end
diff --git a/lib/app_web/live/app_live.html.heex b/lib/app_web/live/app_live.html.heex
index 373d12d1..c051470b 100644
--- a/lib/app_web/live/app_live.html.heex
+++ b/lib/app_web/live/app_live.html.heex
@@ -184,6 +184,7 @@
type="checkbox"
checked
phx-value-id={item.id}
+ phx-value-cid={item.cid}
phx-click="toggle"
class="flex-none p-4 m-2 form-checkbox text-slate-400 cursor-not-allowed"
/>
@@ -227,6 +228,7 @@
@@ -425,6 +429,7 @@
class="w-full flex-auto text-slate-800 m-2"
phx-click="edit-item"
phx-value-id={item.id}
+ phx-value-cid={item.cid}
>
<%= item.text %>
@@ -432,7 +437,7 @@
<%= item.id %>
- <%= item.position %>
+ <%= item.cid %>
<% end %>
@@ -445,6 +450,7 @@