static/js/bookmakers-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 getBookmaker(id);
12 document.getElementById("main-container").appendChild(loadBookmaker(record));
13}
14
15function loadBookmaker(bookmaker) {
16 const div = document.createElement("div");
17 div.setAttribute("data-type", "bookmaker");
18 div.setAttribute("data-id", id);
19 div.classList.add("bookmaker");
20
21 // bookmaker.name
22 div.appendChild(newInputText("Name", bookmaker?.name, "bookmaker-name"));
23
24 // bookmaker.default_commission
25 div.appendChild(newInputText("Commission", bookmaker?.default_commission, "bookmaker-default_commission"));
26
27 return div;
28}
29
30function getInputValueFromNode(node, name) {
31 const element = node.getElementsByClassName(name)[0];
32 return element.type === "checkbox" ? element.checked : element.value;
33}
34
35function buildBookmakerObject() {
36 const node = document.getElementsByClassName("bookmaker")[0];
37 return {
38 id: +node.getAttribute("data-id"),
39 name: getInputValueFromNode(node, "bookmaker-name"),
40 default_commission: +getInputValueFromNode(node, "bookmaker-default_commission"),
41 }
42}
43
44async function submit() {
45 if (await saveBookmaker(buildBookmakerObject())) {
46 location.href = "/bookmakers"
47 }
48}
49