mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-11-29 20:02:51 +01:00
feat(transactions): render the transaction picker as a mini transaction
TransactionSelect options now look like a compact version of the transaction item: paid state, type colour, date, description, category, tags, entities, amount and account. The search endpoint returns that data as JSON, and pre-selected options carry it in data-data so edit forms render the same way.
This commit is contained in:
1 parent
34f6066187
commit
aa3ffe9b3e
5 files changed
+173
-7
No files matched your search
@@ -1,3 +1,5 @@
|
||||
import json
|
||||
|
||||
from django.forms import SelectMultiple, widgets
|
||||
from django.urls import reverse
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
@@ -142,6 +144,7 @@ class TransactionSelect(TomSelect):
|
||||
|
||||
def build_attrs(self, base_attrs, extra_attrs=None):
|
||||
attrs = super().build_attrs(base_attrs, extra_attrs)
|
||||
attrs["data-render"] = "transaction"
|
||||
|
||||
if self.load_income and self.load_expense:
|
||||
attrs["data-load"] = reverse("transactions_search")
|
||||
@@ -155,3 +158,13 @@ class TransactionSelect(TomSelect):
|
||||
)
|
||||
|
||||
return attrs
|
||||
|
||||
def create_option(self, name, value, *args, **kwargs):
|
||||
option = super().create_option(name, value, *args, **kwargs)
|
||||
instance = getattr(value, "instance", None)
|
||||
if instance is not None:
|
||||
from apps.transactions.utils.select_search import transaction_option
|
||||
|
||||
# tom-select reads this to render the selected transaction like the search results
|
||||
option["attrs"]["data-data"] = json.dumps(transaction_option(instance))
|
||||
return option
|
||||
@@ -0,0 +1,30 @@
|
||||
from apps.transactions.templatetags.currency_display import currency_display
|
||||
from django.template.defaultfilters import date as date_filter
|
||||
|
||||
|
||||
def transaction_option(t):
|
||||
"""Data used by the transaction picker (tom-select) to render a mini transaction."""
|
||||
currency = t.account.currency
|
||||
return {
|
||||
"value": str(t.id),
|
||||
"text": str(t),
|
||||
"description": t.description,
|
||||
"date": date_filter(t.date, "SHORT_DATE_FORMAT"),
|
||||
"type": t.type,
|
||||
"is_paid": t.is_paid,
|
||||
"amount": currency_display(
|
||||
t.amount,
|
||||
currency.prefix,
|
||||
currency.suffix,
|
||||
currency.decimal_places,
|
||||
string=True,
|
||||
),
|
||||
"account": (
|
||||
f"{t.account.group.name} • {t.account.name}"
|
||||
if t.account.group
|
||||
else t.account.name
|
||||
),
|
||||
"category": t.category.name if t.category else "",
|
||||
"tags": [tag.name for tag in t.tags.all()],
|
||||
"entities": [entity.name for entity in t.entities.all()],
|
||||
}
|
||||
@@ -20,6 +20,7 @@ from apps.transactions.utils.calculations import (
|
||||
calculate_percentage_distribution,
|
||||
)
|
||||
from apps.transactions.utils.default_ordering import default_order
|
||||
from apps.transactions.utils.select_search import transaction_option
|
||||
from dateutil.relativedelta import relativedelta
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required
|
||||
@@ -993,7 +994,8 @@ def get_recent_transactions(request, filter_type=None):
|
||||
output_field=IntegerField(),
|
||||
)
|
||||
)
|
||||
.select_related("account", "category")
|
||||
.select_related("account__currency", "account__group", "category")
|
||||
.prefetch_related("tags", "entities")
|
||||
.order_by("date_order", "date", "id")
|
||||
)
|
||||
|
||||
@@ -1013,9 +1015,4 @@ def get_recent_transactions(request, filter_type=None):
|
||||
| Q(category__name__icontains=search_term)
|
||||
)
|
||||
|
||||
# Prepare data for JSON response
|
||||
data = []
|
||||
for t in queryset:
|
||||
data.append({"text": str(t), "value": str(t.id)})
|
||||
|
||||
return JsonResponse(data, safe=False)
|
||||
return JsonResponse([transaction_option(t) for t in queryset], safe=False)
|
||||
@@ -3,6 +3,31 @@ import * as Popper from "@popperjs/core";
|
||||
import '../styles/_tom-select.scss'
|
||||
|
||||
|
||||
// Mini version of cotton/transaction/item.html, used by TransactionSelect
|
||||
function renderTransaction(data, escape, compact) {
|
||||
if (!data.date) {
|
||||
return `<div>${escape(data.text)}</div>`;
|
||||
}
|
||||
const kind = data.type === 'EX' ? 'expense' : 'income';
|
||||
const meta = [
|
||||
data.category && `<span><i class="fa-solid fa-icons fa-fw"></i>${escape(data.category)}</span>`,
|
||||
data.tags && data.tags.length && `<span><i class="fa-solid fa-hashtag fa-fw"></i>${escape(data.tags.join(', '))}</span>`,
|
||||
data.entities && data.entities.length && `<span><i class="fa-solid fa-user-group fa-fw"></i>${escape(data.entities.join(', '))}</span>`,
|
||||
].filter(Boolean).join('');
|
||||
return `<div class="ts-tx ts-tx-${kind}${compact ? ' ts-tx-compact' : ''}">
|
||||
<i class="ts-tx-paid fa-regular ${data.is_paid ? 'fa-circle-check' : 'fa-circle'}"></i>
|
||||
<div class="ts-tx-main">
|
||||
<div class="ts-tx-date"><i class="fa-solid fa-calendar fa-fw"></i>${escape(data.date)}</div>
|
||||
<div class="ts-tx-desc">${escape(data.description || '')}</div>
|
||||
${meta && !compact ? `<div class="ts-tx-meta">${meta}</div>` : ''}
|
||||
</div>
|
||||
<div class="ts-tx-side">
|
||||
<div class="ts-tx-amount text-${kind}">${escape(data.amount)}</div>
|
||||
<div class="ts-tx-account">${escape(data.account)}</div>
|
||||
</div>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
window.TomSelect = function createDynamicTomSelect(element) {
|
||||
const schedulePopperUpdate = function (instance) {
|
||||
// Wait for TomSelect DOM updates before recalculating dropdown position.
|
||||
@@ -98,6 +123,12 @@ window.TomSelect = function createDynamicTomSelect(element) {
|
||||
};
|
||||
}
|
||||
|
||||
if (element.dataset.render === 'transaction') {
|
||||
config.dataAttr = 'data'; // dataset key of the data-data attribute
|
||||
config.render.option = (data, escape) => renderTransaction(data, escape, false);
|
||||
config.render.item = (data, escape) => renderTransaction(data, escape, true);
|
||||
}
|
||||
|
||||
if (element.dataset.load) {
|
||||
config.load = function (query, callback) {
|
||||
let url = element.dataset.load + '?q=' + encodeURIComponent(query);
|
||||
|
||||
@@ -399,3 +399,98 @@
|
||||
.tomselect-checkbox {
|
||||
margin-right: 0.5rem;
|
||||
}
|
||||
|
||||
/* Mini transaction (TransactionSelect) */
|
||||
.ts-tx {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
width: 100%;
|
||||
min-width: 0;
|
||||
padding: 0.375rem 0.5rem;
|
||||
border-left: 4px solid var(--color-success);
|
||||
border-radius: var(--radius-field, 0.25rem);
|
||||
background-color: var(--color-base-100);
|
||||
font-family: var(--font-mono, ui-monospace, monospace);
|
||||
font-size: 0.8125rem;
|
||||
line-height: 1.25rem;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.ts-tx-expense {
|
||||
border-left-color: var(--color-error);
|
||||
}
|
||||
|
||||
.ts-tx-paid {
|
||||
color: color-mix(in oklab, var(--color-base-content) 50%, transparent);
|
||||
font-size: 1.125rem;
|
||||
}
|
||||
|
||||
.ts-tx-main,
|
||||
.ts-tx-side {
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.ts-tx-main {
|
||||
flex: 1 1 auto;
|
||||
}
|
||||
|
||||
.ts-tx-side {
|
||||
flex: 0 1 auto;
|
||||
max-width: 45%;
|
||||
text-align: right;
|
||||
}
|
||||
|
||||
.ts-tx-date,
|
||||
.ts-tx-meta,
|
||||
.ts-tx-account {
|
||||
color: color-mix(in oklab, var(--color-base-content) 70%, transparent);
|
||||
font-size: 0.75rem;
|
||||
}
|
||||
|
||||
.ts-tx i.fa-fw {
|
||||
margin-right: 0.25rem;
|
||||
font-size: 0.625rem;
|
||||
}
|
||||
|
||||
.ts-tx-desc {
|
||||
color: var(--color-base-content);
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
|
||||
.ts-tx-meta {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
column-gap: 0.75rem;
|
||||
}
|
||||
|
||||
.ts-tx-amount {
|
||||
font-weight: 500;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.ts-tx-desc,
|
||||
.ts-tx-account {
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
/* Selected value inside the control: no card chrome, leave room for the clear button and arrow */
|
||||
.ts-control .ts-tx-compact {
|
||||
padding: 0 1.5rem 0 0.5rem;
|
||||
background: transparent;
|
||||
}
|
||||
|
||||
.ts-dropdown .option:has(.ts-tx) {
|
||||
padding: 0.25rem 0.375rem;
|
||||
}
|
||||
|
||||
.ts-dropdown .active .ts-tx {
|
||||
background-color: transparent;
|
||||
}
|
||||
|
||||
.ts-control > .item:has(.ts-tx) {
|
||||
flex: 1 1 auto;
|
||||
min-width: 0;
|
||||
}
|
||||
Reference in new issue
Block a user