mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-12-05 14:36:36 +01:00
feat(transactions): update saved transactions in place instead of reloading lists
Adding or editing a transaction used to fire "updated", which re-rendered the whole list. Saves now send "transactions_changed" with the saved ids. Lists with the transaction_mover behavior ask their own endpoint for just those rows (?only=<id>), and the server answers with where each row goes: the row after it and, for lists split by date, its divider. Rows it doesn't return have left the list and are removed. Covers the monthly overview, all transactions (a row joining or leaving the current page still reloads it), and the installment, recurring and calendar lists. Pages without a mover fall back to "updated".
This commit is contained in:
1 parent
6d56958deb
commit
0244238354
17 files changed
+478
-142
No files matched your search
@@ -57,6 +57,144 @@
|
||||
trigger updated on window
|
||||
end
|
||||
end
|
||||
|
||||
-- Saved transactions only get moved in place by a list with
|
||||
-- transaction_mover, everywhere else the page reloads like before
|
||||
on transactions_changed from window
|
||||
if no <[data-moves-transactions]/> then trigger updated on window end
|
||||
end
|
||||
|
||||
-- Puts added or edited transactions in place without reloading the list.
|
||||
--
|
||||
-- The add and edit endpoints answer with `transactions_changed` and the ids
|
||||
-- they saved. The list asks its own endpoint (hx-get, with its hx-include
|
||||
-- values) for just those rows with ?only=. The server answers with each row
|
||||
-- it still shows plus the row that comes after it (and for a list split by
|
||||
-- date, the row's divider and the divider after that), so where a row goes is
|
||||
-- always decided by the server's ordering. Rows it doesn't answer with have
|
||||
-- left the list (filter, month) and go away.
|
||||
-- See apps/transactions/utils/changed_rows.py
|
||||
--
|
||||
-- With data-moves-transactions="page" the list is one page of many: a row
|
||||
-- that joins or leaves it shifts every page after it, so that reloads instead.
|
||||
behavior transaction_mover
|
||||
on transactions_changed from window
|
||||
set ids to event.detail.ids
|
||||
if me.matches('#transactions-list')
|
||||
set list to me
|
||||
else
|
||||
set list to the first <#transactions-list/> in me
|
||||
end
|
||||
if no ids or no list then exit end
|
||||
if no <.transaction/> in list
|
||||
-- the empty state is server-rendered
|
||||
trigger updated on window
|
||||
exit
|
||||
end
|
||||
|
||||
make a URLSearchParams called params
|
||||
if my @hx-include
|
||||
repeat for source in document.querySelectorAll(my @hx-include)
|
||||
if source.matches('form')
|
||||
make a FormData from source called fields
|
||||
repeat for pair in Array.from(fields.entries())
|
||||
call params.append(pair[0], pair[1])
|
||||
end
|
||||
else if source.name
|
||||
call params.set(source.name, source.value)
|
||||
end
|
||||
end
|
||||
end
|
||||
repeat for id in ids
|
||||
call params.append('only', id)
|
||||
end
|
||||
call fetch(`${my @hx-get}?${params}`, {headers: {'HX-Request': 'true'}})
|
||||
set res to it
|
||||
if not res.ok
|
||||
trigger updated on window
|
||||
exit
|
||||
end
|
||||
call res.text()
|
||||
set markup to it
|
||||
|
||||
make a <template/> called holder
|
||||
set holder.innerHTML to markup
|
||||
set entries to Array.from(holder.content.children)
|
||||
|
||||
set shown to []
|
||||
repeat for row in <.transaction[data-transaction-id]/> in list
|
||||
if ids.includes(the @data-transaction-id of row) then append row to shown end
|
||||
end
|
||||
|
||||
if my @data-moves-transactions is 'page'
|
||||
set shownIds to shown.map(\ r -> r.dataset.transactionId).sort().join()
|
||||
set answeredIds to entries.map(\ e -> e.content.querySelector('.transaction').dataset.transactionId).sort().join()
|
||||
if shownIds is not answeredIds
|
||||
trigger updated on window
|
||||
exit
|
||||
end
|
||||
end
|
||||
|
||||
repeat for row in shown
|
||||
remove row
|
||||
end
|
||||
|
||||
-- last first, so the row each one goes before is already in place
|
||||
repeat for entry in entries.reverse()
|
||||
set node to entry.content.firstElementChild
|
||||
set nextRow to null
|
||||
if entry.dataset.nextId
|
||||
set nextRow to list.querySelector(`.transaction[data-transaction-id="${entry.dataset.nextId}"]`)
|
||||
end
|
||||
|
||||
if node.matches('.transaction')
|
||||
-- a list without dividers
|
||||
if nextRow
|
||||
call nextRow.before(node)
|
||||
else
|
||||
set rows to list.querySelectorAll('.transaction')
|
||||
if rows.length
|
||||
call rows[rows.length - 1].after(node)
|
||||
else
|
||||
call list.prepend(node)
|
||||
end
|
||||
end
|
||||
else
|
||||
set existing to list.querySelector(`#${CSS.escape(node.id)}`)
|
||||
if existing
|
||||
set node to node.querySelector('.transaction')
|
||||
if nextRow and existing.contains(nextRow)
|
||||
call nextRow.before(node)
|
||||
else
|
||||
call existing.querySelector('.transactions-divider-collapse > .flex').append(node)
|
||||
end
|
||||
else
|
||||
set nextDivider to null
|
||||
if entry.dataset.nextGroup
|
||||
set nextDivider to list.querySelector(`#${CSS.escape(entry.dataset.nextGroup)}`)
|
||||
end
|
||||
set dividers to list.querySelectorAll(':scope > .transactions-divider')
|
||||
if nextDivider
|
||||
call nextDivider.before(node)
|
||||
else if dividers.length
|
||||
call dividers[dividers.length - 1].after(node)
|
||||
else
|
||||
call list.prepend(node)
|
||||
end
|
||||
end
|
||||
end
|
||||
call htmx.process(node)
|
||||
call _hyperscript.processNode(node)
|
||||
end
|
||||
|
||||
-- dividers left empty collapse themselves
|
||||
trigger transactions_removed on window
|
||||
send change to list -- the selection action bar recounts what's left
|
||||
if no <.transaction/> in list
|
||||
trigger updated on window
|
||||
else
|
||||
trigger selective_update on window -- totals and summaries
|
||||
end
|
||||
end
|
||||
|
||||
behavior transaction_divider
|
||||
|
||||
Reference in new issue
Block a user