static/js/common.js (view raw)
1document.addEventListener('DOMContentLoaded', function () {
2 const navObject = document.getElementsByTagName("nav")[0];
3 for (const page of navPages) {
4 const a = document.createElement("a");
5 a.innerText = page.name;
6 a.href = page.href;
7 navObject.appendChild(a)
8 }
9});
10
11// Global constants
12const navPages = [
13 { name: "Home", href: "/" },
14 { name: "Bookmakers", href: "/bookmakers" },
15 { name: "Accounts", href: "/accounts" },
16 { name: "Records", href: "/records" },
17];
18
19const currency = "€";
20const locale = "it-IT";
21
22// Cell formatters
23function formatValue(v) {
24 return (v / 100).toFixed(2);
25}
26
27function formatCash(v) {
28 return formatValue(v) + currency;
29}
30
31function formatDate(dateString) {
32 return (new Date(dateString)).toLocaleString(locale);
33}
34
35function formatBoolean(value, id) {
36 const input = document.createElement("input");
37 input.type = "checkbox";
38 if (value) input.setAttribute("checked", "");
39 input.disabled = true;
40 //input.setAttribute("data-id", id);
41 //input.onchange = undefined;
42 return input.outerHTML;
43}
44
45// Input components
46function newInputText(label, value, name) {
47 const l = document.createElement("label");
48 const input = document.createElement("input");
49 input.className = name;
50 input.type = "text";
51 input.placeholder = label;
52 input.value = value ?? "";
53 l.innerHTML += label + "<br />";
54 l.appendChild(input);
55 return l;
56}
57
58function newInputCheckbox(label, value, name) {
59 const l = document.createElement("label");
60 const input = document.createElement("input");
61 input.className = name;
62 input.type = "checkbox";
63 if (value) input.setAttribute("checked", "");
64 l.appendChild(input);
65 l.innerHTML += label;
66 return l;
67}
68
69// Functions
70function getQueryStringID() {
71 return Number(new URLSearchParams(window.location.search).get("id") ?? 0);
72}
73
74async function handleFetchResult(res) {
75 if (!res.ok) {
76 console.error(await res.text())
77 return
78 }
79
80 return await res.json();
81}
82
83async function myFetch(url) {
84 res = await fetch(url);
85 return await handleFetchResult(res);
86}
87
88async function myFetchPOST(url, body) {
89 const res = await fetch(url, {
90 method: 'POST',
91 headers: { 'Content-Type': 'application/json' },
92 body: body ? JSON.stringify(body) : undefined,
93 });
94 return await handleFetchResult(res);
95}
96
97// API calls
98async function getBookmakers() {
99 return await myFetch('/api/bookmakers');
100}
101
102async function getAccounts() {
103 return await myFetch('/api/accounts');
104}
105
106async function getRecords() {
107 return await myFetch('/api/records');
108}
109
110async function getBookmaker(id) {
111 return await myFetch(`/api/bookmakers/${id}`);
112}
113
114async function getAccount(id) {
115 return await myFetch(`/api/accounts/${id}`);
116}
117
118async function getRecord(id) {
119 return await myFetch(`/api/records/${id}`);
120}
121
122async function saveBookmaker(payload) {
123 return await myFetchPOST("/api/bookmakers", payload);
124}
125
126async function saveAccount(payload) {
127 return await myFetchPOST("/api/accounts", payload);
128}
129
130async function saveRecord(payload) {
131 return await myFetchPOST("/api/records", payload);
132}