all repos — piggy @ 46e32843c33c2a0a8c7cfb71a71b4456234057b0

Dead simple finance manager in Go, HTML and JS.

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
43async function handleFetchResult(res) {
44    if (!res.ok) {
45        console.error(await res.text())
46        return
47    }
48
49    return await res.json();
50}
51
52async function myFetch(url) {
53    res = await fetch(url);
54    return await handleFetchResult(res);
55}
56
57async function myFetchPOST(url, body) {
58    const res = await fetch(url, {
59        method: 'POST',
60        headers: { 'Content-Type': 'application/json' },
61        body: body ? JSON.stringify(body) : undefined,
62    });
63    return await handleFetchResult(res);
64}
65
66async function getRecords() {
67    return await myFetch('/api/records');
68}