all repos — piggy @ 693d77e1f7f4d3cc8f8da2e000d408ec3b1950ac

Dead simple finance manager in Go, HTML and JS.

static/js/accounts-edit.js (view raw)

 1document.addEventListener('DOMContentLoaded', function () {
 2    handleID();
 3
 4    document.getElementById('save').addEventListener('click', submit);
 5});
 6
 7let id;
 8
 9async function handleID() {
10    id = getQueryStringID();
11    const record = id === 0 ? null : await getAccount(id);
12    document.getElementById("main-container").appendChild(loadAccount(record));
13}
14
15function loadAccount(account) {
16    const div = document.createElement("div");
17    div.setAttribute("data-type", "account");
18    div.setAttribute("data-id", id);
19    div.classList.add("account");
20
21    // account.name
22    div.appendChild(newInputText("Name", account?.name, "account-name"));
23
24    return div;
25}
26
27function getInputValueFromNode(node, name) {
28    const element = node.getElementsByClassName(name)[0];
29    return element.type === "checkbox" ? element.checked : element.value;
30}
31
32function buildAccountObject() {
33    const node = document.getElementsByClassName("account")[0];
34    return {
35        id: +node.getAttribute("data-id"),
36        name: getInputValueFromNode(node, "account-name"),
37    }
38}
39
40async function submit() {
41    if (await saveAccount(buildAccountObject())) {
42        location.href = "/accounts"
43    }
44}
45