147 lines
4.4 KiB
JavaScript
147 lines
4.4 KiB
JavaScript
/* Skuld — the transaction list: filter by year, account and type, newest first.
|
|
* A generated transfer_in is marked and can only be viewed and deleted together
|
|
* with its transfer_out (docs/spec.md section 7.4). */
|
|
|
|
(function () {
|
|
"use strict";
|
|
|
|
var rows = document.querySelector("[data-rows]");
|
|
var list = document.querySelector("[data-list]");
|
|
var empty = document.querySelector("[data-empty]");
|
|
var yearInput = document.querySelector('[name="year"]');
|
|
var marketSelect = document.querySelector('[name="market_id"]');
|
|
var typeSelect = document.querySelector('[name="type"]');
|
|
|
|
function fillTypes() {
|
|
Object.keys(Skuld.TYPE_LABELS).forEach(function (type) {
|
|
var option = document.createElement("option");
|
|
option.value = type;
|
|
option.textContent = Skuld.typeLabel(type);
|
|
typeSelect.appendChild(option);
|
|
});
|
|
}
|
|
|
|
function fillMarkets(markets) {
|
|
markets.forEach(function (market) {
|
|
var option = document.createElement("option");
|
|
option.value = String(market.id);
|
|
option.textContent = market.name;
|
|
marketSelect.appendChild(option);
|
|
});
|
|
}
|
|
|
|
function query() {
|
|
var parts = [];
|
|
if (yearInput.value.trim()) {
|
|
parts.push("year=" + encodeURIComponent(yearInput.value.trim()));
|
|
}
|
|
if (marketSelect.value) {
|
|
parts.push("market_id=" + encodeURIComponent(marketSelect.value));
|
|
}
|
|
if (typeSelect.value) {
|
|
parts.push("type=" + encodeURIComponent(typeSelect.value));
|
|
}
|
|
return "/api/transactions" + (parts.length ? "?" + parts.join("&") : "");
|
|
}
|
|
|
|
function amount(sats, ore) {
|
|
if (sats) {
|
|
return Skuld.formatSats(sats) + " BTC";
|
|
}
|
|
if (ore) {
|
|
return Skuld.formatKroner(ore);
|
|
}
|
|
return "";
|
|
}
|
|
|
|
function cell(row, text, className) {
|
|
var td = document.createElement("td");
|
|
td.textContent = text;
|
|
if (className) {
|
|
td.className = className;
|
|
}
|
|
row.appendChild(td);
|
|
return td;
|
|
}
|
|
|
|
function remove(transaction) {
|
|
var question = transaction.type === "transfer_out"
|
|
? "Slette overføringa og den laga overføringa inn?"
|
|
: "Slette transaksjonen?";
|
|
if (!window.confirm(question)) {
|
|
return;
|
|
}
|
|
Skuld.api("DELETE", "/api/transactions/" + transaction.id)
|
|
.then(load)
|
|
.catch(Skuld.showError);
|
|
}
|
|
|
|
function actions(transaction) {
|
|
var td = document.createElement("td");
|
|
var link = document.createElement("a");
|
|
link.href = "/transaksjon?id=" + transaction.id;
|
|
link.textContent = transaction.generated ? "Sjå" : "Endre";
|
|
td.appendChild(link);
|
|
if (!transaction.generated) {
|
|
var button = document.createElement("button");
|
|
button.type = "button";
|
|
button.className = "button-danger";
|
|
button.textContent = "Slett";
|
|
button.addEventListener("click", function () {
|
|
remove(transaction);
|
|
});
|
|
td.appendChild(document.createTextNode(" "));
|
|
td.appendChild(button);
|
|
}
|
|
return td;
|
|
}
|
|
|
|
function render(transactions) {
|
|
rows.textContent = "";
|
|
if (!transactions.length) {
|
|
empty.hidden = false;
|
|
list.hidden = true;
|
|
return;
|
|
}
|
|
empty.hidden = true;
|
|
list.hidden = false;
|
|
transactions.forEach(function (transaction) {
|
|
var row = document.createElement("tr");
|
|
cell(row, Skuld.formatDate(transaction.date), "");
|
|
cell(
|
|
row,
|
|
Skuld.typeLabel(transaction.type)
|
|
+ (transaction.generated ? " (laga automatisk)" : ""),
|
|
""
|
|
);
|
|
cell(row, transaction.market_name, "");
|
|
cell(row, transaction.to_market_name || "", "");
|
|
cell(row, amount(transaction.leg_in_sats, transaction.leg_in_ore), "numeric");
|
|
cell(row, amount(transaction.leg_out_sats, transaction.leg_out_ore), "numeric");
|
|
cell(row, amount(transaction.fee_sats, transaction.fee_ore), "numeric");
|
|
cell(row, Skuld.formatKroner(transaction.cost_ore), "numeric");
|
|
cell(row, transaction.taxable ? "Ja" : "Nei", "");
|
|
row.appendChild(actions(transaction));
|
|
rows.appendChild(row);
|
|
});
|
|
}
|
|
|
|
function load() {
|
|
Skuld.clearErrors();
|
|
return Skuld.api("GET", query()).then(render);
|
|
}
|
|
|
|
Skuld.on("transactions", function () {
|
|
fillTypes();
|
|
[yearInput, marketSelect, typeSelect].forEach(function (control) {
|
|
control.addEventListener("change", function () {
|
|
load().catch(Skuld.showError);
|
|
});
|
|
});
|
|
return Skuld.api("GET", "/api/markets").then(function (markets) {
|
|
fillMarkets(markets);
|
|
return load();
|
|
});
|
|
});
|
|
})();
|