static/js/accounts-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 getAccount(id);
18 document.getElementById("main-container").appendChild(loadAccount(record));
19}
20
21function loadAccount(account) {
22 const div = document.createElement("div");
23 div.setAttribute("data-type", "account");
24 div.setAttribute("data-id", id);
25 div.classList.add("account");
26
27 // account.name
28 div.appendChild(newInputText("Name", account?.name, "account-name"));
29
30 return div;
31}
32
33function getInputValueFromNode(node, name) {
34 const element = node.getElementsByClassName(name)[0];
35 return element.type === "checkbox" ? element.checked : element.value;
36}
37
38function buildAccountObject() {
39 const node = document.getElementsByClassName("account")[0];
40 return {
41 id: +node.getAttribute("data-id"),
42 name: getInputValueFromNode(node, "account-name"),
43 }
44}
45
46async function submit() {
47 if (await saveAccount(buildAccountObject())) {
48 location.href = "/accounts"
49 }
50}
51
52async function remove() {
53 if (await myConfirm(deleteAccount, id)) {
54 location.href = "/accounts"
55 }
56}
57