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.exchange
25 div.appendChild(newInputCheckbox("Exchange", bookmaker?.exchange, "bookmaker-exchange"));
26
27 // bookmaker.default_commission
28 div.appendChild(newInputText("Commission", bookmaker?.default_commission, "bookmaker-default_commission"));
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 buildBookmakerObject() {
39 const node = document.getElementsByClassName("bookmaker")[0];
40 return {
41 id: +node.getAttribute("data-id"),
42 name: getInputValueFromNode(node, "bookmaker-name"),
43 exchange: getInputValueFromNode(node, "bookmaker-exchange"),
44 default_commission: +getInputValueFromNode(node, "bookmaker-default_commission"),
45 }
46}
47
48async function submit() {
49 if (await saveBookmaker(buildBookmakerObject())) {
50 location.href = "/bookmakers"
51 }
52}
53