all repos — piggy @ bbb48c59b31feefe1c469fb20c41527184610062

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
 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 myConfirm(f, id) {
 75    if (confirm("Are you sure?")) return await f(id);
 76    return false;
 77}
 78
 79async function handleFetchResult(res) {
 80    if (!res.ok) {
 81        console.error(await res.text())
 82        return
 83    }
 84
 85    return await res.json();
 86}
 87
 88async function myFetch(url) {
 89    res = await fetch(url);
 90    return await handleFetchResult(res);
 91}
 92
 93async function myFetchPOST(url, body) {
 94    const res = await fetch(url, {
 95        method: 'POST',
 96        headers: { 'Content-Type': 'application/json' },
 97        body: body ? JSON.stringify(body) : undefined,
 98    });
 99    return await handleFetchResult(res);
100}
101
102async function myFetchDELETE(url) {
103    const res = await fetch(url, { method: 'DELETE' });
104    return await handleFetchResult(res);
105}
106
107// API calls
108async function getBookmakers() {
109    return await myFetch('/api/bookmakers');
110}
111
112async function getBookmaker(id) {
113    return await myFetch(`/api/bookmakers/${id}`);
114}
115
116async function saveBookmaker(payload) {
117    return await myFetchPOST("/api/bookmakers", payload);
118}
119
120async function deleteBookmaker(id) {
121    return await myFetchDELETE(`/api/bookmakers/${id}`);
122}
123
124
125async function getAccounts() {
126    return await myFetch('/api/accounts');
127}
128
129async function getAccount(id) {
130    return await myFetch(`/api/accounts/${id}`);
131}
132
133async function saveAccount(payload) {
134    return await myFetchPOST("/api/accounts", payload);
135}
136
137async function deleteAccount(id) {
138    return await myFetchDELETE(`/api/accounts/${id}`);
139}
140
141
142async function getRecords() {
143    return await myFetch('/api/records');
144}
145
146async function getRecord(id) {
147    return await myFetch(`/api/records/${id}`);
148}
149
150async function saveRecord(payload) {
151    return await myFetchPOST("/api/records", payload);
152}
153
154async function deleteRecord(id) {
155    return await myFetchDELETE(`/api/records/${id}`);
156}