-
Notifications
You must be signed in to change notification settings - Fork 2
/
frontend.html
294 lines (261 loc) · 8.8 KB
/
frontend.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"
integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7"
crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/2.0.1/fetch.min.js"></script>
<script src="https://raw.githubusercontent.com/csnover/js-iso8601/master/iso8601.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Nikls is kinda like scred</title>
<script>
'use strict';
var backend = "/v0/";
var acceptJson = new Headers({"Accept": "application/json"});
var sendJson = new Headers({"Content-Type": "application/json"});
var getOpts = {method: "GET", headers: acceptJson, credentials: "same-origin"};
var accountNames = [];
function space() {
return document.createTextNode(" ");
}
function formatBalance(b) {
var el = document.createElement("button");
el.className = "btn";
if (b.balance < 0) {
el.className += " btn-danger";
} else if (b.balance > 0 ) {
el.className += " btn-success";
}
el.onclick = function() { narrow(b.account) };
var amount = b.balance/100.0;
el.textContent = b.account + " " + (amount>0 ? "+" : "") + amount;
return el;
}
function formatBalanceSmall(b) {
var el = document.createElement("span");
el.className = b.balance >= 0 ? "label-success" : "label-danger";
el.className += " label";
var amount = b.balance/100.0;
el.textContent = b.account + " " + (amount>0 ? "+" : "") + amount;
return el;
}
function formatTime(t) {
function pad(n) {
if (n<10) {
return "0"+n;
}
return ""+n;
}
var d;
if (typeof t === "undefined") {
d = new Date();
} else {
d = new Date(t);
}
return d.getFullYear() + "-" + pad(d.getMonth()+1) + "-" + pad(d.getDate()) +
" " + pad(d.getHours()) + ":" + pad(d.getMinutes());
}
function transactionAction(t, action) {
fetch(backend+'transaction/'+t.id+'/'+action, {method: "POST", credentials: "same-origin"})
.catch(function(err) {
console.log("ERROR", err);
document.getElementById("status").textContent = err;
})
.then(function(_) {
update();
});
}
function cancelLink(t) {
var a = document.createElement("a");
var action = t.cancelled ? "uncancel" : "cancel";
a.onclick = function() { transactionAction(t, action); };
a.textContent = action;
return a;
}
function formatTransaction(t) {
var el = document.createElement(t.cancelled ? "s" : "span");
el.appendChild(document.createTextNode(formatTime(t.time)))
el.appendChild(space());
var balances = t.positive.concat(t.negative);
balances.sort(function(a, b) { return b.balance - a.balance; });
balances.forEach(function(b) {
el.appendChild(formatBalanceSmall(b));
el.appendChild(space());
});
el.appendChild(space());
el.appendChild(cancelLink(t));
el.appendChild(document.createElement("br"));
el.appendChild(document.createTextNode(t.description));
return el;
}
function narrow(user) {
window.location.hash=user;
update();
}
function update() {
// XXX where should this go?
document.getElementById("time").defaultValue = formatTime();
var user = window.location.hash.substr(1);
var source;
if (user == '') {
source = backend+'transaction';
} else if (user == 'all') {
source = backend+'transaction?all';
} else {
source = backend+'account/'+user+'/transactions';
}
console.log("source", source)
fetch(source, getOpts)
.then(function(r) { return r.json(); })
.then(function(ts) {
var title = document.getElementById("transactions-title");
if (user == '') {
title.innerHTML = "Transactions";
} else {
title.innerHTML = "Transactions for " + user +
' <button onclick="narrow(\'\')" class="btn btn-primary btn-xs">Everybody</a>';
console.log(title.innerHTML);
}
var ul = document.getElementById("transactions");
while (ul.firstChild) {
ul.removeChild(ul.firstChild);
}
console.log(ts);
ts.reverse();
ts.forEach(function(t) {
var li = document.createElement("li");
li.className = "list-group-item";
li.appendChild(formatTransaction(t));
ul.appendChild(li);
});
});
fetch(backend+'balances', getOpts)
.then(function(r) { return r.json(); })
.then(function(bs) {
var balances = document.getElementById("balances");
while (balances.firstChild) {
balances.removeChild(balances.firstChild);
}
accountNames = bs.map(function(b) { return b.account; });
bs.forEach(function(b) {
balances.appendChild(formatBalance(b));
balances.appendChild(space());
});
});
}
function verifyAccounts(accounts) {
var unknown = accounts.filter(
function (acc) { return !accountNames.includes(acc); });
if (unknown.length === 0) {
return true;
}
return confirm("Unknown users:\n"+unknown.join("\n")+"\n"+
"Do you want to create them?");
}
function serializeBalances(obj) {
return Object.keys(obj).map(function(k) {
return {account: k,
balance: obj[k]}});
}
function nameList(str) {
return str.split(/ *,? +/)
.map(function(s) {return s.toLowerCase();})
.filter(function(s) {return s !== ""});
}
function add() {
var cents = Math.round(document.getElementById("sum").value*100);
var description = document.getElementById("description").value;
var payers = nameList(document.getElementById("payer").value);
var shared = nameList(document.getElementById("shared").value);
var time = document.getElementById("time").value;
if (time != "") {
time = Date.parse(time);
if (isNaN(time)) {
alert("Invalid time");
return false;
}
} else {
time = (new Date()).getTime();
}
console.log("time", time);
if (!verifyAccounts(shared.concat(payers))) {
return false;
}
var transaction = {time: time,
description: description,
sum: cents,
payers: payers,
shared_by: shared}
console.log("transaction", transaction);
fetch(backend+'transaction', {method: "POST",
credentials: "same-origin",
headers: sendJson,
body: JSON.stringify(transaction)})
.then(function(r) {
return r.text();
})
.then(function(status) {
console.log("STATUS", status);
document.getElementById("status").textContent = status;
document.getElementById("addform").reset();
update();
})
.catch(function(err) {
console.log("ERROR", err);
document.getElementById("status").textContent = err;
});
return false;
}
window.onload = update;
</script>
</head>
<body>
<div class="container">
<h1>Nikls</h1>
<form id="addform" class="form-horizontal" onsubmit="return add()">
<div class="form-group">
<div class="col-sm-12">
<label for="description" class="control-label">Description</label>
<input class="form-control" type="text" id="description"
placeholder="Description" required />
</div>
</div>
<div class="form-group">
<div class="col-sm-2">
<label for="sum" class="control-label">Sum</label>
<input class="form-control" type="text" pattern="[0-9.]*" id="sum"
placeholder="0.00" required />
</div>
<div class="col-sm-2">
<label for="payer" class="control-label">Payer(s)</label>
<input class="form-control" type="text" id="payer"
placeholder="name" required />
</div>
<div class="col-sm-6">
<label for="shared" class="control-label">Shared by</label>
<input class="form-control" type="text" id="shared"
placeholder="name1 name2 name3" required />
</div>
<div class="col-sm-2">
<label for="time" class="control-label">Time</label>
<input class="form-control" id="time"
placeholder="YYYY-MM-DD HH:MM" />
</div>
</div>
<div class="form-group">
<div class="col-sm-12">
<input type="submit" value="Add"
class="col-sm-12 btn btn-success btn-block" />
</div>
</div>
</form>
<p>Status: <span id="status"></span></p>
<h3>Balances</h3>
<p id="balances"></p>
<h3 id="transactions-title">Transactions</h3>
<ul id="transactions" class="list-group"></ul>
</div>
</body>
</html>