mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-12-05 14:36:36 +01:00
Merge pull request #708 from eitchtee/feat/selective-transaction-updates
feat(transactions): update saved transactions in place instead of reloading lists
This commit is contained in:
17 files changed
+478
-142
No files matched your search
@@ -7,6 +7,7 @@ from django.views.decorators.http import require_http_methods
|
||||
|
||||
from apps.calendar_view.utils.calendar import get_transactions_by_day
|
||||
from apps.transactions.models import Transaction
|
||||
from apps.transactions.utils.changed_rows import changed_rows
|
||||
|
||||
|
||||
@login_required
|
||||
@@ -73,6 +74,9 @@ def calendar_transactions_list(request, day: int, month: int, year: int):
|
||||
"id",
|
||||
)
|
||||
|
||||
if "only" in request.GET:
|
||||
return changed_rows(request, transactions, grouped=False)
|
||||
|
||||
return render(
|
||||
request,
|
||||
"calendar_view/fragments/list_transactions.html",
|
||||
|
||||
@@ -20,6 +20,7 @@ from apps.transactions.utils.calculations import (
|
||||
calculate_percentage_distribution,
|
||||
calculate_account_totals,
|
||||
)
|
||||
from apps.transactions.utils.changed_rows import LIST_PREFETCH, changed_rows
|
||||
from apps.transactions.utils.default_ordering import default_order
|
||||
|
||||
|
||||
@@ -83,23 +84,7 @@ def transactions_list(request, month: int, year: int):
|
||||
transactions_filtered = f.qs.filter(
|
||||
reference_date__year=year,
|
||||
reference_date__month=month,
|
||||
).collapse_transfers().prefetch_related(
|
||||
"account",
|
||||
"account__group",
|
||||
"category",
|
||||
"tags",
|
||||
"account__exchange_currency",
|
||||
"account__currency",
|
||||
"installment_plan",
|
||||
"entities",
|
||||
"dca_expense_entries",
|
||||
"sub_transactions",
|
||||
"dca_income_entries",
|
||||
"linked_transaction__account__currency",
|
||||
"linked_transaction__account__group",
|
||||
"linked_transaction__category",
|
||||
"linked_transaction__tags",
|
||||
)
|
||||
).collapse_transfers()
|
||||
|
||||
# Late transactions: date < today and is_paid = False (only shown for default ordering)
|
||||
late_transactions = None
|
||||
@@ -116,6 +101,13 @@ def transactions_list(request, month: int, year: int):
|
||||
|
||||
transactions_filtered = default_order(transactions_filtered, order=order)
|
||||
|
||||
if "only" in request.GET:
|
||||
return changed_rows(request, transactions_filtered, late=late_transactions)
|
||||
|
||||
if late_transactions is not None:
|
||||
late_transactions = late_transactions.prefetch_related(*LIST_PREFETCH)
|
||||
transactions_filtered = transactions_filtered.prefetch_related(*LIST_PREFETCH)
|
||||
|
||||
return render(
|
||||
request,
|
||||
"monthly_overview/fragments/list.html",
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
import json
|
||||
from datetime import date
|
||||
from decimal import Decimal
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.test import TestCase, override_settings
|
||||
from django.urls import reverse
|
||||
|
||||
from apps.accounts.models import Account, AccountGroup
|
||||
from apps.currencies.models import Currency
|
||||
from apps.transactions.models import Transaction
|
||||
|
||||
|
||||
@override_settings(
|
||||
STORAGES={
|
||||
"default": {"BACKEND": "django.core.files.storage.FileSystemStorage"},
|
||||
"staticfiles": {
|
||||
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage"
|
||||
},
|
||||
},
|
||||
WHITENOISE_AUTOREFRESH=True,
|
||||
)
|
||||
class ChangedRowsTests(TestCase):
|
||||
"""The lists' ?only= mode, used to move saved rows in place."""
|
||||
|
||||
def setUp(self):
|
||||
user = get_user_model().objects.create_user(
|
||||
email="test@test.com", password="testpass123"
|
||||
)
|
||||
self.client.login(username="test@test.com", password="testpass123")
|
||||
currency = Currency.objects.create(code="USD", name="US Dollar")
|
||||
self.account = Account.objects.create(
|
||||
name="Account",
|
||||
group=AccountGroup.objects.create(name="Group"),
|
||||
currency=currency,
|
||||
)
|
||||
self.rows = [
|
||||
Transaction.objects.create(
|
||||
account=self.account,
|
||||
type=Transaction.Type.EXPENSE,
|
||||
is_paid=True,
|
||||
date=date(2025, 12, 10),
|
||||
reference_date=date(2025, 12, 1),
|
||||
amount=Decimal("10"),
|
||||
description=f"row {i}",
|
||||
owner=user,
|
||||
)
|
||||
for i in range(3)
|
||||
]
|
||||
|
||||
def _only(self, *ids):
|
||||
return self.client.get(
|
||||
reverse("monthly_transactions_list", kwargs={"month": 12, "year": 2025}),
|
||||
{"order": "older", "only": [str(i) for i in ids]},
|
||||
HTTP_HX_REQUEST="true",
|
||||
)
|
||||
|
||||
def test_row_comes_with_the_row_after_it(self):
|
||||
first, middle, last = self.rows
|
||||
response = self._only(middle.pk)
|
||||
self.assertContains(response, f'data-next-id="{last.pk}"')
|
||||
self.assertContains(response, f'data-transaction-id="{middle.pk}"')
|
||||
self.assertNotContains(response, f'data-transaction-id="{first.pk}"')
|
||||
|
||||
def test_last_row_has_nothing_after_it(self):
|
||||
response = self._only(self.rows[-1].pk)
|
||||
self.assertContains(response, 'data-next-id="" data-next-group=""')
|
||||
|
||||
def test_row_that_left_the_month_is_not_returned(self):
|
||||
moved = self.rows[1]
|
||||
moved.reference_date = date(2026, 1, 1)
|
||||
moved.save()
|
||||
response = self._only(moved.pk)
|
||||
self.assertNotContains(response, "<template")
|
||||
|
||||
def test_edit_announces_the_saved_ids(self):
|
||||
transaction = self.rows[0]
|
||||
response = self.client.post(
|
||||
reverse("transaction_edit", kwargs={"transaction_id": transaction.pk}),
|
||||
{
|
||||
"account": self.account.pk,
|
||||
"type": Transaction.Type.EXPENSE,
|
||||
"is_paid": True,
|
||||
"date": "2025-12-11",
|
||||
"reference_date": "2025-12",
|
||||
"amount": "12",
|
||||
"description": "edited",
|
||||
"submit": "",
|
||||
},
|
||||
HTTP_HX_REQUEST="true",
|
||||
)
|
||||
self.assertEqual(response.status_code, 204)
|
||||
trigger = json.loads(response.headers["HX-Trigger"])
|
||||
self.assertEqual(trigger["transactions_changed"], {"ids": [str(transaction.pk)]})
|
||||
self.assertNotIn("updated", trigger)
|
||||
|
||||
def test_list_without_dividers_answers_bare_rows(self):
|
||||
middle = self.rows[1]
|
||||
response = self.client.get(
|
||||
reverse(
|
||||
"calendar_transactions_list",
|
||||
kwargs={"day": 10, "month": 12, "year": 2025},
|
||||
),
|
||||
{"only": [str(middle.pk)]},
|
||||
HTTP_HX_REQUEST="true",
|
||||
)
|
||||
self.assertContains(response, f'data-next-id="{self.rows[2].pk}"')
|
||||
self.assertContains(response, f'data-transaction-id="{middle.pk}"')
|
||||
self.assertNotContains(response, "transactions-divider")
|
||||
|
||||
def test_paginated_list_only_looks_at_the_requested_page(self):
|
||||
# 100 earlier rows fill page 1, pushing self.rows to page 2
|
||||
first = self.rows[0]
|
||||
for _ in range(100):
|
||||
Transaction.objects.create(
|
||||
account=self.account,
|
||||
type=Transaction.Type.EXPENSE,
|
||||
is_paid=True,
|
||||
date=date(2025, 12, 1),
|
||||
reference_date=date(2025, 12, 1),
|
||||
amount=Decimal("1"),
|
||||
owner=first.owner,
|
||||
)
|
||||
|
||||
def only_on(page):
|
||||
return self.client.get(
|
||||
reverse("transactions_all_list"),
|
||||
{"order": "older", "page": page, "only": [str(first.pk)]},
|
||||
HTTP_HX_REQUEST="true",
|
||||
)
|
||||
|
||||
self.assertNotContains(only_on("1"), "<template")
|
||||
self.assertContains(only_on("2"), f'data-next-id="{self.rows[1].pk}"')
|
||||
@@ -0,0 +1,95 @@
|
||||
from django.core.paginator import Paginator
|
||||
from django.shortcuts import render
|
||||
from django.utils.text import slugify
|
||||
|
||||
from apps.common.templatetags.natural import naturaldate
|
||||
from apps.transactions.models import Transaction
|
||||
|
||||
# What a transaction row (cotton/transaction/item.html) reads
|
||||
LIST_PREFETCH = (
|
||||
"account",
|
||||
"account__group",
|
||||
"category",
|
||||
"tags",
|
||||
"account__exchange_currency",
|
||||
"account__currency",
|
||||
"installment_plan",
|
||||
"entities",
|
||||
"dca_expense_entries",
|
||||
"sub_transactions",
|
||||
"dca_income_entries",
|
||||
"linked_transaction__account__currency",
|
||||
"linked_transaction__account__group",
|
||||
"linked_transaction__category",
|
||||
"linked_transaction__tags",
|
||||
)
|
||||
|
||||
|
||||
def changed_rows(
|
||||
request, rows, late=None, grouped=True, paginate_by=None, page=None
|
||||
):
|
||||
"""Answers a list's ``?only=<id>`` request: renders just those rows the list
|
||||
still shows, each with the row (and, for a list split by date, the divider)
|
||||
that comes after it. The client moves them into place and drops the rows
|
||||
that aren't listed any more.
|
||||
See transaction_mover in templates/includes/scripts/hyperscript/transactions.html
|
||||
|
||||
``rows`` and ``late`` are the list's own ordered querysets, so where a row
|
||||
goes is always decided by the same ordering the full list uses."""
|
||||
wanted = {int(pk) for pk in request.GET.getlist("only") if pk.isdigit()}
|
||||
combined = Transaction.transfers_combined()
|
||||
fields = ("id", "date", "type", "linked_transaction_id")
|
||||
|
||||
listed = rows.values_list(*fields)
|
||||
if paginate_by:
|
||||
listed = Paginator(listed, paginate_by).get_page(page).object_list
|
||||
|
||||
# [slug, label, rows] in display order, grouped like the list templates
|
||||
groups = []
|
||||
if late is not None:
|
||||
late_rows = list(late.values_list(*fields))
|
||||
if late_rows:
|
||||
groups.append(["late-transactions", None, late_rows])
|
||||
for row in listed:
|
||||
slug, label = "", ""
|
||||
if grouped:
|
||||
label = naturaldate(row[1])
|
||||
slug = slugify(label)
|
||||
if groups and groups[-1][0] == slug:
|
||||
groups[-1][2].append(row)
|
||||
else:
|
||||
groups.append([slug, label, [row]])
|
||||
|
||||
def row_id(pk, _date, type_, linked_id):
|
||||
# A combined transfer is shown, and identified, by its "from" side
|
||||
if combined and linked_id and type_ == Transaction.Type.INCOME:
|
||||
return linked_id
|
||||
return pk
|
||||
|
||||
changed = []
|
||||
for i, (slug, label, group_rows) in enumerate(groups):
|
||||
for j, row in enumerate(group_rows):
|
||||
if row[0] in wanted:
|
||||
after = group_rows[j + 1] if j + 1 < len(group_rows) else None
|
||||
changed.append(
|
||||
{
|
||||
"pk": row[0],
|
||||
"slug": slug,
|
||||
"label": label,
|
||||
"late": label is None,
|
||||
"next_id": row_id(*after) if after else "",
|
||||
"next_group": groups[i + 1][0] if i + 1 < len(groups) else "",
|
||||
}
|
||||
)
|
||||
|
||||
found = Transaction.objects.prefetch_related(*LIST_PREFETCH).in_bulk(
|
||||
[c["pk"] for c in changed]
|
||||
)
|
||||
for c in changed:
|
||||
c["transactions"] = [found[c["pk"]]]
|
||||
|
||||
return render(
|
||||
request,
|
||||
"transactions/fragments/changed_rows.html",
|
||||
{"changed": changed},
|
||||
)
|
||||
@@ -9,6 +9,7 @@ from django.views.decorators.http import require_http_methods
|
||||
from apps.common.decorators.htmx import only_htmx
|
||||
from apps.transactions.forms import InstallmentPlanForm
|
||||
from apps.transactions.models import InstallmentPlan
|
||||
from apps.transactions.utils.changed_rows import changed_rows
|
||||
|
||||
|
||||
@login_required
|
||||
@@ -69,6 +70,9 @@ def installment_plan_transactions(request, installment_plan_id):
|
||||
installment_plan = get_object_or_404(InstallmentPlan.objects, id=installment_plan_id)
|
||||
transactions = installment_plan.transactions.prefetch_related("sub_transactions").order_by("reference_date", "id")
|
||||
|
||||
if "only" in request.GET:
|
||||
return changed_rows(request, transactions, grouped=False)
|
||||
|
||||
return render(
|
||||
request,
|
||||
"installment_plans/fragments/list_transactions.html",
|
||||
|
||||
@@ -12,6 +12,7 @@ from apps.common.decorators.htmx import only_htmx
|
||||
from apps.transactions.forms import RecurringTransactionForm
|
||||
from apps.transactions.models import RecurringTransaction
|
||||
from apps.transactions.tasks import generate_recurring_transactions
|
||||
from apps.transactions.utils.changed_rows import changed_rows
|
||||
|
||||
|
||||
@login_required
|
||||
@@ -94,6 +95,9 @@ def recurring_transaction_transactions(request, recurring_transaction_id):
|
||||
"reference_date", "id"
|
||||
)
|
||||
|
||||
if "only" in request.GET:
|
||||
return changed_rows(request, transactions, grouped=False)
|
||||
|
||||
return render(
|
||||
request,
|
||||
"recurring_transactions/fragments/list_transactions.html",
|
||||
|
||||
@@ -19,6 +19,7 @@ from apps.transactions.utils.calculations import (
|
||||
calculate_currency_totals,
|
||||
calculate_percentage_distribution,
|
||||
)
|
||||
from apps.transactions.utils.changed_rows import LIST_PREFETCH, changed_rows
|
||||
from apps.transactions.utils.default_ordering import default_order
|
||||
from apps.transactions.utils.select_search import (
|
||||
search_transactions,
|
||||
@@ -42,6 +43,21 @@ def _get_accessible_transaction_or_404(transaction_id):
|
||||
return get_object_or_404(Transaction.objects, id=transaction_id)
|
||||
|
||||
|
||||
def _transactions_changed(*transactions, hide_offcanvas=True):
|
||||
"""HX-Trigger for saved transactions. Lists with transaction_mover move just
|
||||
these rows instead of re-rendering, other pages fall back to a full "updated".
|
||||
See templates/includes/scripts/hyperscript/transactions.html"""
|
||||
ids = set()
|
||||
for transaction in transactions:
|
||||
ids.add(str(transaction.pk))
|
||||
if transaction.linked_transaction_id:
|
||||
ids.add(str(transaction.linked_transaction_id))
|
||||
trigger = {"transactions_changed": {"ids": sorted(ids)}, "toasts": None}
|
||||
if hide_offcanvas:
|
||||
trigger["hide_offcanvas"] = None
|
||||
return json.dumps(trigger)
|
||||
|
||||
|
||||
def _get_accessible_attachment_or_404(attachment_id):
|
||||
attachment = get_object_or_404(
|
||||
TransactionAttachment.objects.select_related("transaction"),
|
||||
@@ -159,7 +175,7 @@ def transaction_add(request):
|
||||
if "submit" in request.POST:
|
||||
return HttpResponse(
|
||||
status=204,
|
||||
headers={"HX-Trigger": "updated, hide_offcanvas"},
|
||||
headers={"HX-Trigger": _transactions_changed(saved_instance)},
|
||||
)
|
||||
elif "submit_and_another" in request.POST:
|
||||
form = TransactionForm(
|
||||
@@ -229,7 +245,9 @@ def transaction_add(request):
|
||||
{"form": form},
|
||||
)
|
||||
if update:
|
||||
response["HX-Trigger"] = "updated"
|
||||
response["HX-Trigger"] = _transactions_changed(
|
||||
saved_instance, hide_offcanvas=False
|
||||
)
|
||||
|
||||
return response
|
||||
|
||||
@@ -367,12 +385,12 @@ def transaction_edit(request, transaction_id, **kwargs):
|
||||
if request.method == "POST":
|
||||
form = TransactionForm(request.POST, instance=transaction)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
saved_instance = form.save()
|
||||
messages.success(request, _("Transaction updated successfully"))
|
||||
|
||||
return HttpResponse(
|
||||
status=204,
|
||||
headers={"HX-Trigger": "updated, hide_offcanvas"},
|
||||
headers={"HX-Trigger": _transactions_changed(saved_instance)},
|
||||
)
|
||||
else:
|
||||
form = TransactionForm(instance=transaction)
|
||||
@@ -551,11 +569,11 @@ def transactions_transfer(request):
|
||||
if request.method == "POST":
|
||||
form = TransferForm(request.POST)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
saved = form.save()
|
||||
messages.success(request, _("Transfer added successfully"))
|
||||
return HttpResponse(
|
||||
status=204,
|
||||
headers={"HX-Trigger": "updated, hide_offcanvas"},
|
||||
headers={"HX-Trigger": _transactions_changed(*saved)},
|
||||
)
|
||||
else:
|
||||
form = TransferForm(
|
||||
@@ -581,11 +599,11 @@ def transaction_transfer_edit(request, transaction_id):
|
||||
if request.method == "POST":
|
||||
form = TransferForm(request.POST, instance=transaction)
|
||||
if form.is_valid():
|
||||
form.save()
|
||||
saved = form.save()
|
||||
messages.success(request, _("Transfer updated successfully"))
|
||||
return HttpResponse(
|
||||
status=204,
|
||||
headers={"HX-Trigger": "updated, hide_offcanvas"},
|
||||
headers={"HX-Trigger": _transactions_changed(*saved)},
|
||||
)
|
||||
else:
|
||||
form = TransferForm(instance=transaction)
|
||||
@@ -828,25 +846,7 @@ def transaction_all_list(request):
|
||||
|
||||
today = timezone.localdate(timezone.now())
|
||||
|
||||
transactions = Transaction.objects.prefetch_related(
|
||||
"account",
|
||||
"account__group",
|
||||
"category",
|
||||
"tags",
|
||||
"account__exchange_currency",
|
||||
"account__currency",
|
||||
"installment_plan",
|
||||
"entities",
|
||||
"dca_expense_entries",
|
||||
"sub_transactions",
|
||||
"dca_income_entries",
|
||||
"linked_transaction__account__currency",
|
||||
"linked_transaction__account__group",
|
||||
"linked_transaction__category",
|
||||
"linked_transaction__tags",
|
||||
).all()
|
||||
|
||||
f = TransactionsFilter(request.GET, queryset=transactions)
|
||||
f = TransactionsFilter(request.GET, queryset=Transaction.objects.all())
|
||||
|
||||
# Late transactions: date < today and is_paid = False (only shown for default ordering on first page)
|
||||
late_transactions = None
|
||||
@@ -867,6 +867,19 @@ def transaction_all_list(request):
|
||||
|
||||
main_transactions = default_order(main_transactions, order=order)
|
||||
|
||||
if "only" in request.GET:
|
||||
return changed_rows(
|
||||
request,
|
||||
main_transactions,
|
||||
late=late_transactions,
|
||||
paginate_by=100,
|
||||
page=page_number,
|
||||
)
|
||||
|
||||
if late_transactions is not None:
|
||||
late_transactions = late_transactions.prefetch_related(*LIST_PREFETCH)
|
||||
main_transactions = main_transactions.prefetch_related(*LIST_PREFETCH)
|
||||
|
||||
paginator = Paginator(main_transactions, 100)
|
||||
page_obj = paginator.get_page(page_number)
|
||||
|
||||
@@ -898,7 +911,7 @@ def transaction_all_summary(request):
|
||||
"dca_income_entries",
|
||||
).all()
|
||||
|
||||
f = TransactionsFilter(request.GET, queryset=transactions)
|
||||
f = TransactionsFilter(request.GET, queryset=Transaction.objects.all())
|
||||
|
||||
currency_data = calculate_currency_totals(f.qs.all(), ignore_empty=True)
|
||||
currency_percentages = calculate_percentage_distribution(currency_data)
|
||||
@@ -932,7 +945,7 @@ def transaction_all_account_summary(request):
|
||||
"dca_income_entries",
|
||||
).all()
|
||||
|
||||
f = TransactionsFilter(request.GET, queryset=transactions)
|
||||
f = TransactionsFilter(request.GET, queryset=Transaction.objects.all())
|
||||
|
||||
account_data = calculate_account_totals(transactions_queryset=f.qs.all())
|
||||
account_percentages = calculate_percentage_distribution(account_data)
|
||||
@@ -962,7 +975,7 @@ def transaction_all_currency_summary(request):
|
||||
"dca_income_entries",
|
||||
).all()
|
||||
|
||||
f = TransactionsFilter(request.GET, queryset=transactions)
|
||||
f = TransactionsFilter(request.GET, queryset=Transaction.objects.all())
|
||||
|
||||
currency_data = calculate_currency_totals(
|
||||
f.qs.exclude(account__in=request.user.untracked_accounts.all()),
|
||||
|
||||
@@ -5,7 +5,8 @@
|
||||
{% block title %}{% translate 'Transactions on' %} {{ date|date:"SHORT_DATE_FORMAT" }}{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<div hx-get="{% url 'calendar_transactions_list' day=date.day month=date.month year=date.year %}" hx-trigger="updated from:window" hx-target="closest .offcanvas" class="show-loading" id="transactions-list">
|
||||
<div hx-get="{% url 'calendar_transactions_list' day=date.day month=date.month year=date.year %}" hx-trigger="updated from:window" hx-target="closest .offcanvas" class="show-loading" id="transactions-list"
|
||||
data-moves-transactions _="install transaction_mover">
|
||||
{% for transaction in transactions %}
|
||||
<c-transaction.item :transaction="transaction"></c-transaction.item>
|
||||
{% empty %}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -5,7 +5,8 @@
|
||||
{% block title %}{% translate 'Installments' %}{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<div hx-get="{% url 'installment_plan_transactions' installment_plan_id=installment_plan.id %}" hx-trigger="updated from:window" hx-target="closest .offcanvas" class="show-loading" id="transactions-list">
|
||||
<div hx-get="{% url 'installment_plan_transactions' installment_plan_id=installment_plan.id %}" hx-trigger="updated from:window" hx-target="closest .offcanvas" class="show-loading" id="transactions-list"
|
||||
data-moves-transactions _="install transaction_mover">
|
||||
{% for transaction in transactions %}
|
||||
<c-transaction.item :transaction="transaction"></c-transaction.item>
|
||||
{% endfor %}
|
||||
|
||||
@@ -4,55 +4,11 @@
|
||||
|
||||
<div id="transactions-list">
|
||||
{% if late_transactions %}
|
||||
<div id="late-transactions" class="transactions-divider"
|
||||
_="install transaction_divider"
|
||||
x-data="{ open: sessionStorage.getItem('late-transactions') !== 'false' }"
|
||||
x-init="if (sessionStorage.getItem('late-transactions') === null) sessionStorage.setItem('late-transactions', 'true')">
|
||||
<div class="mt-3 mb-1 w-full border-b border-b-error/50 transactions-divider-title cursor-pointer">
|
||||
<a class="no-underline inline-block w-full text-error font-semibold"
|
||||
role="button"
|
||||
@click="open = !open; sessionStorage.setItem('late-transactions', open)"
|
||||
:aria-expanded="open">
|
||||
<i class="fa-solid fa-circle-exclamation me-1"></i>{% translate "late" %}
|
||||
</a>
|
||||
</div>
|
||||
<div class="transactions-divider-collapse overflow-visible isolation-auto"
|
||||
x-show="open"
|
||||
x-collapse>
|
||||
<div class="flex flex-col">
|
||||
{% for transaction in late_transactions %}
|
||||
<c-transaction.item
|
||||
:transaction="transaction"></c-transaction.item>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% include "transactions/fragments/_divider.html" with slug="late-transactions" late=True transactions=late_transactions %}
|
||||
{% endif %}
|
||||
|
||||
{% for x in transactions_by_date %}
|
||||
<div id="{{ x.grouper|slugify }}" class="transactions-divider"
|
||||
_="install transaction_divider"
|
||||
x-data="{ open: sessionStorage.getItem('{{ x.grouper|slugify }}') !== 'false' }"
|
||||
x-init="if (sessionStorage.getItem('{{ x.grouper|slugify }}') === null) sessionStorage.setItem('{{ x.grouper|slugify }}', 'true')">
|
||||
<div class="mt-3 mb-1 w-full border-b border-b-base-content/30 transactions-divider-title cursor-pointer">
|
||||
<a class="no-underline inline-block w-full"
|
||||
role="button"
|
||||
@click="open = !open; sessionStorage.setItem('{{ x.grouper|slugify }}', open)"
|
||||
:aria-expanded="open">
|
||||
{{ x.grouper }}
|
||||
</a>
|
||||
</div>
|
||||
<div class="transactions-divider-collapse overflow-visible isolation-auto"
|
||||
x-show="open"
|
||||
x-collapse>
|
||||
<div class="flex flex-col">
|
||||
{% for transaction in x.list %}
|
||||
<c-transaction.item
|
||||
:transaction="transaction"></c-transaction.item>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% include "transactions/fragments/_divider.html" with slug=x.grouper|slugify label=x.grouper transactions=x.list %}
|
||||
|
||||
{% empty %}
|
||||
{% if not late_transactions %}
|
||||
|
||||
@@ -291,6 +291,8 @@
|
||||
{# Transactions list#}
|
||||
<div id="transactions"
|
||||
class="show-loading"
|
||||
data-moves-transactions
|
||||
_="install transaction_mover"
|
||||
hx-get="{% url 'monthly_transactions_list' month=month year=year %}"
|
||||
hx-trigger="load, updated from:window, every 10m" hx-include="#filter, #order">
|
||||
</div>
|
||||
|
||||
@@ -5,7 +5,8 @@
|
||||
{% block title %}{% translate 'Transactions' %}{% endblock %}
|
||||
|
||||
{% block body %}
|
||||
<div hx-get="{% url 'recurring_transaction_transactions' recurring_transaction_id=recurring_transaction.id %}" hx-trigger="updated from:window" hx-target="closest .offcanvas" class="show-loading" id="transactions-list">
|
||||
<div hx-get="{% url 'recurring_transaction_transactions' recurring_transaction_id=recurring_transaction.id %}" hx-trigger="updated from:window" hx-target="closest .offcanvas" class="show-loading" id="transactions-list"
|
||||
data-moves-transactions _="install transaction_mover">
|
||||
{% for transaction in transactions %}
|
||||
<c-transaction.item :transaction="transaction"></c-transaction.item>
|
||||
{% endfor %}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
{% load i18n %}
|
||||
<div id="{{ slug }}" class="transactions-divider"
|
||||
_="install transaction_divider"
|
||||
x-data="{ open: sessionStorage.getItem('{{ slug }}') !== 'false' }"
|
||||
x-init="if (sessionStorage.getItem('{{ slug }}') === null) sessionStorage.setItem('{{ slug }}', 'true')">
|
||||
<div class="mt-3 mb-1 w-full border-b {% if late %}border-b-error/50{% else %}border-b-base-content/30{% endif %} transactions-divider-title cursor-pointer">
|
||||
<a class="no-underline inline-block w-full{% if late %} text-error font-semibold{% endif %}"
|
||||
role="button"
|
||||
@click="open = !open; sessionStorage.setItem('{{ slug }}', open)"
|
||||
:aria-expanded="open">
|
||||
{% if late %}<i class="fa-solid fa-circle-exclamation me-1"></i>{% translate "late" %}{% else %}{{ label }}{% endif %}
|
||||
</a>
|
||||
</div>
|
||||
<div class="transactions-divider-collapse overflow-visible isolation-auto"
|
||||
x-show="open"
|
||||
x-collapse>
|
||||
<div class="flex flex-col">
|
||||
{% for transaction in transactions %}
|
||||
<c-transaction.item
|
||||
:transaction="transaction"></c-transaction.item>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,10 @@
|
||||
{# Answer to a list's ?only= request, see apps/transactions/utils/changed_rows.py #}
|
||||
{% for c in changed %}
|
||||
<template data-next-id="{{ c.next_id }}" data-next-group="{{ c.next_group }}">
|
||||
{% if c.slug %}
|
||||
{% include "transactions/fragments/_divider.html" with slug=c.slug label=c.label late=c.late transactions=c.transactions %}
|
||||
{% else %}
|
||||
<c-transaction.item :transaction="c.transactions.0"></c-transaction.item>
|
||||
{% endif %}
|
||||
</template>
|
||||
{% endfor %}
|
||||
@@ -4,55 +4,11 @@
|
||||
|
||||
<div id="transactions-list" class="show-loading">
|
||||
{% if late_transactions %}
|
||||
<div id="late-transactions" class="transactions-divider"
|
||||
_="install transaction_divider"
|
||||
x-data="{ open: sessionStorage.getItem('late-transactions') !== 'false' }"
|
||||
x-init="if (sessionStorage.getItem('late-transactions') === null) sessionStorage.setItem('late-transactions', 'true')">
|
||||
<div class="mt-3 mb-1 w-full border-b border-b-error/50 transactions-divider-title cursor-pointer">
|
||||
<a class="no-underline inline-block w-full text-error font-semibold"
|
||||
role="button"
|
||||
@click="open = !open; sessionStorage.setItem('late-transactions', open)"
|
||||
:aria-expanded="open">
|
||||
<i class="fa-solid fa-circle-exclamation me-1"></i>{% translate "late" %}
|
||||
</a>
|
||||
</div>
|
||||
<div class="transactions-divider-collapse overflow-visible isolation-auto"
|
||||
x-show="open"
|
||||
x-collapse>
|
||||
<div class="flex flex-col">
|
||||
{% for transaction in late_transactions %}
|
||||
<c-transaction.item
|
||||
:transaction="transaction"></c-transaction.item>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% include "transactions/fragments/_divider.html" with slug="late-transactions" late=True transactions=late_transactions %}
|
||||
{% endif %}
|
||||
|
||||
{% for x in transactions_by_date %}
|
||||
<div id="{{ x.grouper|slugify }}" class="transactions-divider"
|
||||
_="install transaction_divider"
|
||||
x-data="{ open: sessionStorage.getItem('{{ x.grouper|slugify }}') !== 'false' }"
|
||||
x-init="if (sessionStorage.getItem('{{ x.grouper|slugify }}') === null) sessionStorage.setItem('{{ x.grouper|slugify }}', 'true')">
|
||||
<div class="mt-3 mb-1 w-full border-b border-b-base-content/30 transactions-divider-title cursor-pointer">
|
||||
<a class="no-underline inline-block w-full"
|
||||
role="button"
|
||||
@click="open = !open; sessionStorage.setItem('{{ x.grouper|slugify }}', open)"
|
||||
:aria-expanded="open">
|
||||
{{ x.grouper }}
|
||||
</a>
|
||||
</div>
|
||||
<div class="transactions-divider-collapse overflow-visible isolation-auto"
|
||||
x-show="open"
|
||||
x-collapse>
|
||||
<div class="flex flex-col">
|
||||
{% for transaction in x.list %}
|
||||
<c-transaction.item
|
||||
:transaction="transaction"></c-transaction.item>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% include "transactions/fragments/_divider.html" with slug=x.grouper|slugify label=x.grouper transactions=x.list %}
|
||||
|
||||
{% empty %}
|
||||
{% if not late_transactions %}
|
||||
|
||||
@@ -241,6 +241,8 @@
|
||||
<div id="transactions"
|
||||
class="show-loading"
|
||||
hx-get="{% url 'transactions_all_list' %}"
|
||||
data-moves-transactions="page"
|
||||
_="install transaction_mover"
|
||||
hx-trigger="load, updated from:window" hx-include="#filter, #page, #order">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user