From 9f83b86389f0f5b033184ac63535fe61159d04a3 Mon Sep 17 00:00:00 2001 From: ai-bot Date: Sat, 14 Nov 2026 16:18:15 +0000 Subject: [PATCH] Add static/markets.js for the account admin --- static/markets.js | 188 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 static/markets.js diff --git a/static/markets.js b/static/markets.js new file mode 100644 index 0000000..ce76ad3 --- /dev/null +++ b/static/markets.js @@ -0,0 +1,188 @@ +/* Skuld — account admin: name, kind, taxable flag, custodian, note, order and + * archive. An account with transactions cannot be deleted. */ + +(function () { + "use strict"; + + var KIND_LABELS = { + exchange: "Børs", + wallet: "Lommebok", + other: "Anna" + }; + + var form = document.querySelector("[data-form]"); + var formTitle = document.querySelector("[data-form-title]"); + var resetButton = document.querySelector("[data-reset]"); + var rows = document.querySelector("[data-rows]"); + var list = document.querySelector("[data-list]"); + var empty = document.querySelector("[data-empty]"); + var custodySelect = document.querySelector('[name="custody_market_id"]'); + + var accounts = []; + var editingId = null; + + function cell(row, text, className) { + var td = document.createElement("td"); + td.textContent = text; + if (className) { + td.className = className; + } + row.appendChild(td); + return td; + } + + function nameOf(id) { + var found = accounts.filter(function (account) { + return account.id === id; + })[0]; + return found ? found.name : ""; + } + + function fillCustody() { + var keep = custodySelect.value; + custodySelect.textContent = ""; + var none = document.createElement("option"); + none.value = ""; + none.textContent = "Ingen"; + custodySelect.appendChild(none); + accounts.forEach(function (account) { + if (account.id === editingId) { + return; + } + var option = document.createElement("option"); + option.value = String(account.id); + option.textContent = account.name; + custodySelect.appendChild(option); + }); + custodySelect.value = keep; + } + + function reset() { + editingId = null; + form.reset(); + Skuld.clearErrors(); + formTitle.textContent = "Ny konto"; + resetButton.hidden = true; + Skuld.setFieldChecked("taxable", true); + Skuld.setFieldValue("sort_order", "0"); + fillCustody(); + } + + function edit(account) { + editingId = account.id; + Skuld.clearErrors(); + formTitle.textContent = "Endre konto"; + resetButton.hidden = false; + Skuld.setFieldValue("name", account.name); + Skuld.setFieldValue("kind", account.kind); + Skuld.setFieldValue("note", account.note); + Skuld.setFieldValue("sort_order", String(account.sort_order)); + Skuld.setFieldChecked("taxable", Boolean(account.taxable)); + Skuld.setFieldChecked("archived", Boolean(account.archived)); + fillCustody(); + Skuld.setFieldValue( + "custody_market_id", + account.custody_market_id ? String(account.custody_market_id) : "" + ); + } + + function remove(account) { + if (!window.confirm("Slette kontoen " + account.name + "?")) { + return; + } + Skuld.api("DELETE", "/api/markets/" + account.id) + .then(function () { + if (editingId === account.id) { + reset(); + } + return load(); + }) + .catch(Skuld.showError); + } + + function render() { + rows.textContent = ""; + if (!accounts.length) { + empty.hidden = false; + list.hidden = true; + return; + } + empty.hidden = true; + list.hidden = false; + accounts.forEach(function (account) { + var row = document.createElement("tr"); + cell(row, account.name + (account.archived ? " (arkivert)" : ""), ""); + cell(row, KIND_LABELS[account.kind] || account.kind, ""); + cell(row, account.taxable ? "Ja" : "Nei", ""); + cell(row, account.custody_market_id ? nameOf(account.custody_market_id) : "", ""); + cell(row, Skuld.formatSats(account.sats), "numeric"); + cell(row, account.note, ""); + cell(row, String(account.sort_order), "numeric"); + + var actions = document.createElement("td"); + var editLink = document.createElement("button"); + editLink.type = "button"; + editLink.className = "button-secondary"; + editLink.textContent = "Endre"; + editLink.addEventListener("click", function () { + edit(account); + }); + actions.appendChild(editLink); + var removeButton = document.createElement("button"); + removeButton.type = "button"; + removeButton.className = "button-danger"; + removeButton.textContent = "Slett"; + removeButton.addEventListener("click", function () { + remove(account); + }); + actions.appendChild(document.createTextNode(" ")); + actions.appendChild(removeButton); + row.appendChild(actions); + rows.appendChild(row); + }); + } + + function payload() { + var custody = Skuld.fieldValue("custody_market_id"); + return { + name: Skuld.fieldValue("name"), + kind: Skuld.fieldValue("kind"), + taxable: Skuld.fieldChecked("taxable") ? 1 : 0, + archived: Skuld.fieldChecked("archived") ? 1 : 0, + custody_market_id: custody ? Number(custody) : null, + note: Skuld.fieldValue("note"), + sort_order: Number(Skuld.fieldValue("sort_order")) || 0 + }; + } + + function load() { + return Skuld.api("GET", "/api/markets").then(function (markets) { + accounts = markets; + render(); + fillCustody(); + return null; + }); + } + + form.addEventListener("submit", function (event) { + event.preventDefault(); + Skuld.clearErrors(); + var path = editingId ? "/api/markets/" + editingId : "/api/markets"; + var method = editingId ? "PATCH" : "POST"; + Skuld.api(method, path, payload()) + .then(function () { + reset(); + return load(); + }) + .catch(function (error) { + Skuld.showError(error); + }); + }); + + resetButton.addEventListener("click", reset); + + Skuld.on("markets", function () { + reset(); + return load(); + }); +})();