all repos — piggy @ 83d9cee1226a9df3b762f3507bbb619f6004b5e9

Dead simple finance manager in Go, HTML and JS.

static/js/records.js (view raw)

  1document.addEventListener('DOMContentLoaded', function () {
  2    loadRecords();
  3
  4    document.getElementById('new-record').addEventListener('click', createNewRecord);
  5});
  6
  7const casino = {
  8    type: 'Arbitraggio',
  9    description: 'Prova',
 10    entries: [
 11        {
 12            bookmaker_id: 1,
 13            account_id: 1,
 14            amount: 97500,
 15            refund: 0,
 16            bonus: 0,
 17            commission: 0,
 18            sub_entries: [
 19                {
 20                    description: "Punta",
 21                    odds: 200,
 22                    won: false,
 23                    date: new Date().toISOString(),
 24                }
 25            ]
 26        },
 27        {
 28            bookmaker_id: 2,
 29            account_id: 2,
 30            amount: 100000,
 31            refund: 0,
 32            bonus: 0,
 33            commission: 0,
 34            sub_entries: [
 35                {
 36                    description: "Banca",
 37                    odds: 195,
 38                    won: true,
 39                    date: new Date().toISOString(),
 40                }
 41            ]
 42        },
 43    ]
 44};
 45
 46const bank = {
 47    type: 'Bancata',
 48    description: 'Prova',
 49    entries: [
 50        {
 51            bookmaker_id: 1,
 52            account_id: 1,
 53            amount: 3000,
 54            refund: 0,
 55            bonus: 0,
 56            commission: 0,
 57            sub_entries: [
 58                {
 59                    description: "Punta",
 60                    odds: 133,
 61                    won: true,
 62                    date: new Date().toISOString(),
 63                }
 64            ]
 65        },
 66        {
 67            bookmaker_id: 3,
 68            account_id: 2,
 69            amount: 3057,
 70            refund: 0,
 71            bonus: 0,
 72            commission: 450,
 73            sub_entries: [
 74                {
 75                    description: "Banca",
 76                    odds: 135,
 77                    won: false,
 78                    date: new Date().toISOString(),
 79                }
 80            ]
 81        },
 82    ]
 83};
 84
 85function loadRecords() {
 86    getRecords().then(records => {
 87            const header = document.getElementById('records-header');
 88            const table = document.getElementById('records-table');
 89            header.innerHTML = '';
 90            table.innerHTML = '';
 91
 92            const tr = document.createElement('tr');
 93            const headers = ["Created", "Done", "Type", "Description", "Date", "Value"];
 94
 95            for (const header of headers) {
 96                const td = document.createElement('td');
 97                td.innerText = header;
 98                tr.appendChild(td);
 99            }
100            header.appendChild(tr);
101
102            for (const record of records) {
103                const tr = document.createElement('tr');
104
105                const fields = [
106                    formatDate(record.created_at),
107                    formatDone(record.done, record.id),
108                    record.type,
109                    record.description,
110                    formatDate(record.date),
111                    formatCash(record.value),
112                ];
113
114                for (const field of fields) {
115                    const td = document.createElement('td');
116                    td.innerHTML = field;
117                    tr.appendChild(td);
118                }
119                table.appendChild(tr);
120            }
121        });
122}
123
124async function createNewRecord() {
125    await myFetchPOST("/api/records", casino);
126    await myFetchPOST("/api/records", bank)
127    loadRecords();
128}