all repos — piggy @ 0.0.2

Dead simple finance manager in Go, HTML and JS.

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

 1document.addEventListener('DOMContentLoaded', function () {
 2    handleID();
 3
 4    document.getElementById('save').addEventListener('click', submit);
 5    
 6    if (id === 0) {
 7        document.getElementById('delete').style.display = "none";
 8    } else {
 9        document.getElementById('delete').addEventListener('click', remove);
10    }
11});
12
13let id;
14
15async function handleID() {
16    id = getQueryStringID();
17    const record = id === 0 ? null : await getBookmaker(id);
18    document.getElementById("main-container").appendChild(loadBookmaker(record));
19}
20
21function loadBookmaker(bookmaker) {
22    const div = document.createElement("div");
23    div.setAttribute("data-type", "bookmaker");
24    div.setAttribute("data-id", id);
25    div.classList.add("bookmaker");
26
27    // bookmaker.name
28    div.appendChild(newInputText("Name", bookmaker?.name, "bookmaker-name"));
29
30    // bookmaker.default_commission
31    div.appendChild(newInputText("Commission", bookmaker?.default_commission, "bookmaker-default_commission"));
32
33    return div;
34}
35
36function getInputValueFromNode(node, name) {
37    const element = node.getElementsByClassName(name)[0];
38    return element.type === "checkbox" ? element.checked : element.value;
39}
40
41function buildBookmakerObject() {
42    const node = document.getElementsByClassName("bookmaker")[0];
43    return {
44        id: +node.getAttribute("data-id"),
45        name: getInputValueFromNode(node, "bookmaker-name"),
46        default_commission: +getInputValueFromNode(node, "bookmaker-default_commission"),
47    }
48}
49
50async function submit() {
51    if (await saveBookmaker(buildBookmakerObject())) {
52        location.href = "/bookmakers"
53    }
54}
55
56async function remove() {
57    if (await myConfirm(deleteBookmaker, id)) {
58        location.href = "/bookmakers"
59    }
60}