Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Lists #165

Merged
merged 59 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
498a231
Create lists table and list schema
SimonLab Oct 5, 2022
13e3c6b
Add list controller
SimonLab Oct 5, 2022
3c3e83c
Add item endpoint
SimonLab Oct 5, 2022
ffe8d64
Display lists under tags
SimonLab Oct 7, 2022
74e73ab
Insert or get person
SimonLab Oct 7, 2022
a3269b7
Add tests for lists
SimonLab Oct 7, 2022
68f5562
Add item controller test for lists
SimonLab Oct 8, 2022
c11fb5c
tidy BUILDIT.md
nelsonic Nov 8, 2022
bd65456
remove Person & Profile code #118
nelsonic Sep 8, 2023
42392dc
revert changes in BUILDT.md https://github.com/dwyl/mvp/pull/165#issu…
nelsonic Sep 8, 2023
6a233f8
merge main https://github.com/dwyl/mvp/pull/165#issuecomment-1711355895
nelsonic Sep 8, 2023
13247c0
Create lists table and list schema
SimonLab Oct 5, 2022
ece1c48
Add list controller
SimonLab Oct 5, 2022
304c06c
Add item endpoint
SimonLab Oct 5, 2022
983b0b8
Display lists under tags
SimonLab Oct 7, 2022
6bf3fbe
Insert or get person
SimonLab Oct 7, 2022
f0c79b4
Add tests for lists
SimonLab Oct 7, 2022
59bcd31
Add item controller test for lists
SimonLab Oct 8, 2022
81f4e51
tidy BUILDIT.md
nelsonic Nov 8, 2022
4138a21
remove Person & Profile code #118
nelsonic Sep 8, 2023
3cd07e4
merge main https://github.com/dwyl/mvp/pull/165#issuecomment-1711355895
nelsonic Sep 8, 2023
d921a5c
merge main https://github.com/dwyl/mvp/pull/165#issuecomment-1711355895
nelsonic Sep 8, 2023
7a6f16e
fix merge conflicts & failing tests #165
nelsonic Sep 8, 2023
26b804c
chore: Renaming variables in `assets/js/app.js`. #416
LuchoTurtle Sep 9, 2023
aba670e
chore: Making all variables in app.js snake_case. #416
LuchoTurtle Sep 9, 2023
898b7f0
fix: Fix live_socket import. #416
LuchoTurtle Sep 9, 2023
b2996d4
fix merge conflicts & failing tests #165
nelsonic Sep 9, 2023
d7f46af
remove unused files #165
nelsonic Sep 9, 2023
e141c86
chore: Double checking `app.js` and `app_live.ex`. #416
LuchoTurtle Sep 9, 2023
c51a780
restore list controller, template and view files https://github.com/d…
nelsonic Sep 10, 2023
da1cc04
define "soft" delete_list/1 #165 #400
nelsonic Sep 10, 2023
8acf8c0
avoid modifying item.cid or list.cid when updating #418
nelsonic Sep 10, 2023
49f4042
avoid modifying item.cid or list.cid when updating #418
nelsonic Sep 10, 2023
2e6c4ee
rename functions for clarity #165
nelsonic Sep 10, 2023
7e306d6
update seeds.exs to use Item.create_item/1 instead of Repo.insert!/1 …
nelsonic Sep 10, 2023
b1f96ac
mix format #410
nelsonic Sep 10, 2023
f3c1d4a
fix: Fixing BUILDIT.md live socket variable. #416
LuchoTurtle Sep 10, 2023
baadd18
Applying TailwindUI Table layout to /lists page #400
nelsonic Sep 12, 2023
72dd787
#422 bump Useful library to 1.13 for Useful.remove_item_from_list/2 r…
nelsonic Sep 14, 2023
6109fb6
#422 bump Useful version to 1.13.1 reverses argument order of remove_…
nelsonic Sep 14, 2023
3a9a83d
#422 implement super simple remove_item_from_list/3 function using Us…
nelsonic Sep 14, 2023
f212cd5
#422 mix format 🙄
nelsonic Sep 14, 2023
f14cec9
#165 delete unused _BAK file #400
nelsonic Sep 15, 2023
18a776c
#400 tidy styling of /lists page as per: https://github.com/dwyl/mvp/…
nelsonic Sep 15, 2023
fb6a393
#400 fix test assertion (text) in test/app_web/controllers/list_contr…
nelsonic Sep 15, 2023
10a2a6a
add title_case/1 function for list.name
nelsonic Sep 15, 2023
d70ea74
Merge pull request #419 from dwyl/variable_consistency#416
nelsonic Sep 16, 2023
81c7227
#423 update items_with_timers/1 to accept list_cid as second param to…
nelsonic Sep 16, 2023
15af0f1
#423 mix format ... tidies items_with_timers/2 code & tests
nelsonic Sep 16, 2023
dd59426
#427 create move_item_from_lista_to_listb/4 & test
nelsonic Sep 16, 2023
2d12ffe
split nav from root.html to separate component for #424 #165
nelsonic Oct 29, 2023
284a496
render nav template in tag/index.html #424 #165
nelsonic Oct 29, 2023
31df47e
render list.name + list_url in nav menu #424 #165
nelsonic Nov 1, 2023
8a785e3
add lists to stats_live.ex so nav can render on stats page #145 #424
nelsonic Nov 1, 2023
cbd558d
differentiate custom lists from other links in main menu #145 #424
nelsonic Nov 1, 2023
f182747
Merge branch 'main' into items-list
nelsonic Nov 1, 2023
7f7c78b
mix format 🙄 #165
nelsonic Nov 1, 2023
a58edec
add item to custom list #425 for #165
nelsonic Nov 1, 2023
6aae78a
remove dbg() #165
nelsonic Nov 1, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions BUILDIT.md
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ defmodule App.TimerTest do
{:ok, timer} =
Timer.start(%{item_id: item.id, person_id: 1, start: seven_seconds_ago})

# stop the timer based on it's item_id
# stop the timer based on it's item_id
Timer.stop_timer_for_item_id(item.id)

stopped_timer = Timer.get_timer!(timer.id)
Expand Down Expand Up @@ -3165,10 +3165,10 @@ everytime `Start/Resume` or `Stop` is called.
person_id = get_person_id(socket.assigns)
items = Item.items_with_timers(person_id)

isEditingItem = socket.assigns.editing
is_editing_item = socket.assigns.editing

# If the item is being edited, we update the timer list of the item being edited.
if isEditingItem do
if is_editing_item do
case payload do
{:start, item_id} ->
timers_list_changeset = Timer.list_timers_changesets(item_id)
Expand Down Expand Up @@ -5469,13 +5469,13 @@ to the `LiveView` server during the mounting phase.

Open `assets/js/app.js`
and locate the
`let liveSocket = new LiveSocket()` variable.
`let live_socket = new LiveSocket()` variable.
We are going to be changing the `params` attribute.
Change it to the following:

```js
params: {
_csrf_token: csrfToken,
_csrf_token: csrf_token,
hours_offset_fromUTC: -new Date().getTimezoneOffset()/60
}
```
Expand Down Expand Up @@ -5927,4 +5927,4 @@ please let us know by starring the repo on GitHub! ⭐

<br />

[![HitCount](https://hits.dwyl.com/dwyl/app-mvp-build.svg)](https://hits.dwyl.com/dwyl/app-mvp)
[![HitCount](https://hits.dwyl.com/dwyl/app-mvp-build.svg)](https://hits.dwyl.com/dwyl/app-mvp)
66 changes: 33 additions & 33 deletions assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ window.addEventListener("phx:page-loading-stop", info => topbar.hide())
window.addEventListener("phx:highlight", (e) => {
document.querySelectorAll("[data-highlight]").forEach(el => {
if(el.id == e.detail.id) {
liveSocket.execJS(el, el.getAttribute("data-highlight"))
live_socket.execJS(el, el.getAttribute("data-highlight"))
}
})
})

// Item id of the destination in the DOM
let itemId_to;
// Item ID of the destination during drag over in the DOM
let item_id_destination;

let Hooks = {}
Hooks.Items = {
Expand All @@ -33,31 +33,31 @@ Hooks.Items = {
})

this.el.addEventListener("remove-highlight", e => {
hook.pushEventTo("#items", "removeHighlight", {id: e.detail.id})
hook.pushEventTo("#items", "remove_highlight", {id: e.detail.id})
// console.log('remove-highlight', e.detail.id)
})

this.el.addEventListener("dragoverItem", e => {
// console.log("dragoverItem", e.detail)
const currentItemId = e.detail.currentItem.id
const selectedItemId = e.detail.selectedItemId
if( currentItemId != selectedItemId) {
hook.pushEventTo("#items", "dragoverItem", {currentItemId: currentItemId, selectedItemId: selectedItemId})
itemId_to = e.detail.currentItem.dataset.id
this.el.addEventListener("dragover_item", e => {
// console.log("dragover_item", e.detail)
const current_item_id = e.detail.current_item.id
const selected_item_id = e.detail.selected_item_id
if( current_item_id != selected_item_id) {
hook.pushEventTo("#items", "dragover_item", {current_item_id: current_item_id, selected_item_id: selected_item_id})
item_id_destination = e.detail.current_item.dataset.id
}
})

this.el.addEventListener("update-indexes", e => {
this.el.addEventListener("update_indexes", e => {
const item_id = e.detail.fromItemId
const list_ids = get_list_item_cids()
console.log("update-indexes", e.detail, "list: ", list_ids)
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) {
if(item_id && item_id_destination && item_id != item_id_destination) {
hook.pushEventTo("#items", "update_list_seq",
{seq: list_ids})
}

itemId_to = null;
item_id_destination = null;
})
}
}
Expand All @@ -78,33 +78,33 @@ function get_list_item_cids() {
window.addEventListener("phx:remove-highlight", (e) => {
document.querySelectorAll("[data-highlight]").forEach(el => {
if(el.id == e.detail.id) {
liveSocket.execJS(el, el.getAttribute("data-remove-highlight"))
live_socket.execJS(el, el.getAttribute("data-remove-highlight"))
}
})
})

window.addEventListener("phx:dragover-item", (e) => {
console.log("phx:dragover-item", e.detail)
const selectedItem = document.querySelector(`#${e.detail.selected_item_id}`)
const currentItem = document.querySelector(`#${e.detail.current_item_id}`)
const selected_item = document.querySelector(`#${e.detail.selected_item_id}`)
const current_item = document.querySelector(`#${e.detail.current_item_id}`)

const items = document.querySelector('#items')
const listItems = [...document.querySelectorAll('.item')]
const list_items = [...document.querySelectorAll('.item')]

if(listItems.indexOf(selectedItem) < listItems.indexOf(currentItem)){
items.insertBefore(selectedItem, currentItem.nextSibling)
if(list_items.indexOf(selected_item) < list_items.indexOf(current_item)){
items.insertBefore(selected_item, current_item.nextSibling)
}

if(listItems.indexOf(selectedItem) > listItems.indexOf(currentItem)){
items.insertBefore(selectedItem, currentItem)
if(list_items.indexOf(selected_item) > list_items.indexOf(current_item)){
items.insertBefore(selected_item, current_item)
}
})

// liveSocket related setup:
// live_socket related setup:

let csrfToken = document.querySelector("meta[name='csrf-token']").getAttribute("content")
let csrf_token = document.querySelector("meta[name='csrf-token']").getAttribute("content")

let liveSocket = new LiveSocket("/live", Socket, {
let live_socket = new LiveSocket("/live", Socket, {
hooks: Hooks,
dom:{
onBeforeElUpdated(from, to) {
Expand All @@ -114,17 +114,17 @@ let liveSocket = new LiveSocket("/live", Socket, {
}
},
params: {
_csrf_token: csrfToken,
_csrf_token: csrf_token,
hours_offset_fromUTC: -new Date().getTimezoneOffset()/60
}
})

// connect if there are any LiveViews on the page
liveSocket.connect()
live_socket.connect()

// expose liveSocket on window for web console debug logs and latency simulation:
// >> liveSocket.enableDebug()
// >> liveSocket.enableLatencySim(1000) // enabled for duration of browser session
// >> liveSocket.disableLatencySim()
window.liveSocket = liveSocket
// expose live_socket on window for web console debug logs and latency simulation:
// >> live_socket.enableDebug()
// >> live_socket.enableLatencySim(1000) // enabled for duration of browser session
// >> live_socket.disableLatencySim()
window.live_socket = live_socket

43 changes: 28 additions & 15 deletions lib/app/item.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ defmodule App.Item do
item
|> cast(attrs, [:person_id, :status, :text])
|> validate_required([:person_id])
|> App.Cid.put_cid()
end

# Update an item without changing the cid ref: #418
def changeset_update(item, attrs) do
item
|> cast(attrs, [:cid, :person_id, :status, :text])
|> validate_required([:cid, :text, :person_id])
end

@doc """
Expand Down Expand Up @@ -137,13 +143,13 @@ defmodule App.Item do
[%Item{}, ...]

"""
def list_items do
def get_items do
Item
|> where([i], is_nil(i.status) or i.status != 6)
|> Repo.all()
end

def list_person_items(person_id) do
def get_person_items(person_id) do
Item
|> where(person_id: ^person_id)
|> Repo.all()
Expand All @@ -164,7 +170,7 @@ defmodule App.Item do
"""
def update_item(%Item{} = item, attrs) do
item
|> Item.changeset(attrs)
|> Item.changeset_update(attrs)
|> PaperTrail.update(originator: %{id: Map.get(attrs, :person_id, 0)})
end

Expand All @@ -186,7 +192,7 @@ defmodule App.Item do

def delete_item(id) do
get_item!(id)
|> Item.changeset(%{status: 6})
|> Item.changeset_update(%{status: 6})
|> Repo.update()
end

Expand All @@ -201,21 +207,28 @@ defmodule App.Item do
# 👩‍💻 Feedback/Pairing/Refactoring Welcome! 🙏

@doc """
`items_with_timers/1` Returns a List of items with the latest associated timers.
This list is ordered with the position that is detailed inside the Items schema.
`items_with_timers/2` Returns a List of items with the latest associated timers.
The result set is ordered by the `list.seq`.
Accepts an optional second parameter `list_cid` which is the unique ID of the `list`
to retrieve `items` for.

## Examples

iex> items_with_timers()
[
%{text: "hello", person_id: 1, status: 2, start: 2022-07-14 09:35:18},
%{text: "world", person_id: 2, status: 7, start: 2022-07-15 04:20:42}
%{text: "hello", person_id: 0, status: 2, start: 2022-07-14 09:35:18},
%{text: "world", person_id: 0, status: 7, start: 2022-07-15 04:20:42}
]
"""
#
def items_with_timers(person_id \\ 0) do
all_list = App.List.get_all_list_for_person(person_id)
seq = App.List.get_list_seq(all_list)
def items_with_timers(person_id \\ 0, list_cid \\ nil) do
seq =
if is_nil(list_cid) do
App.List.get_all_list_for_person(person_id)
|> App.List.get_list_seq()
else
App.List.get_list_by_cid!(list_cid)
|> App.List.get_list_seq()
end

sql = """
SELECT i.id, i.cid, i.text, i.status, i.person_id, i.updated_at,
Expand All @@ -233,7 +246,7 @@ defmodule App.Item do
|> map_columns_to_values()

items_tags =
list_person_items(person_id)
get_person_items(person_id)
|> Enum.reduce(%{}, fn i, acc -> Map.put(acc, i.id, i) end)

accumulate_item_timers(values, seq)
Expand Down Expand Up @@ -378,7 +391,7 @@ defmodule App.Item do
This will not be needed once all records are transitioned.
"""
def update_all_items_cid do
items = list_items()
items = get_items()

Enum.each(items, fn i ->
# coveralls-ignore-start
Expand Down
Loading