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
11const navPages = [
12 { name: "Home", href: "/" },
13 { name: "Bookmakers", href: "/bookmakers" },
14 { name: "Accounts", href: "/accounts" },
15 { name: "Records", href: "/records" },
16];
17
18const currency = "€";
19const locale = "it-IT";
20
21function formatValue(v) {
22 return (v / 100).toFixed(2);
23}
24
25function formatCash(v) {
26 return formatValue(v) + currency;
27}
28
29function formatDate(dateString) {
30 return (new Date(dateString)).toLocaleString(locale);
31}
32
33function formatDone(value, id) {
34 const input = document.createElement("input");
35 input.type = "checkbox";
36 input.checked = value;
37 input.disabled = true;
38 //input.setAttribute("data-id", id);
39 //input.onchange = undefined;
40 return input.outerHTML;
41}
42
43function newInputText(label, value, name) {
44 const l = document.createElement("label");
45 const input = document.createElement("input");
46 input.className = name;
47 input.type = "text";
48 input.placeholder = label;
49 input.value = value ?? "";
50 l.innerHTML += label + "<br />";
51 l.appendChild(input);
52 return l;
53}
54
55function newInputCheckbox(label, value, name) {
56 const l = document.createElement("label");
57 const input = document.createElement("input");
58 input.className = name;
59 input.type = "checkbox";
60 input.checked = value ?? false;
61 l.appendChild(input);
62 l.innerHTML += label;
63 return l;
64}
65
66async function handleFetchResult(res) {
67 if (!res.ok) {
68 console.error(await res.text())
69 return
70 }
71
72 return await res.json();
73}
74
75async function myFetch(url) {
76 res = await fetch(url);
77 return await handleFetchResult(res);
78}
79
80async function myFetchPOST(url, body) {
81 const res = await fetch(url, {
82 method: 'POST',
83 headers: { 'Content-Type': 'application/json' },
84 body: body ? JSON.stringify(body) : undefined,
85 });
86 return await handleFetchResult(res);
87}
88
89async function getRecords() {
90 return await myFetch('/api/records');
91}