-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
74 lines (64 loc) · 2.41 KB
/
app.js
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
Vue.filter('currency', function (value) {
if (typeof value !== "number") {
return value;
}
var formatter = new Intl.NumberFormat('pt-BR', {
style: 'currency',
currency: 'BRL',
minimumFractionDigits: 2
});
return formatter.format(value);
});
Vue.component('delta-indicator', {
props: ['amount'],
template: `<span>
<span v-if="amount == 0" class="delta-indicator-no-change">(=)</span>
<span v-if="amount < 0" class="delta-indicator-decrease">(-)</span>
<span v-if="amount > 0" class="delta-indicator-increase">(+)</span>
</span>`
});
fetch('data.json').then(resp => {
resp.json().then(data => {
// aggregate data with deltas and "previous balance"
let previousMonth = null;
for (const month of data.months) {
const transactions = month.transactions;
let income = 0;
let expenses = 0;
for (const transaction of transactions) {
if (transaction.amount > 0) {
income += transaction.amount;
} else {
expenses += transaction.amount;
}
}
month.income = income;
month.expenses = expenses;
if (previousMonth) {
month.balance = previousMonth.balance + month.income + month.expenses;
month.previousBalance = previousMonth.balance;
month.previousBalanceDelta = previousMonth.balanceDelta;
month.incomeDelta = month.income - previousMonth.income;
month.expensesDelta = -(month.expenses - previousMonth.expenses);
month.balanceDelta = month.balance - previousMonth.balance;
} else {
month.balance = data.initialBalance + month.income + month.expenses;
month.previousBalance = data.initialBalance;
month.previousBalanceDelta = 0;
month.incomeDelta = 0;
month.expensesDelta = 0;
month.balanceDelta = month.balance - data.initialBalance;
}
previousMonth = month;
}
// default value for expansion toggle state
for (const month of data.months) {
month.expanded = false;
}
new Vue({
el: '#app',
data: data
});
document.documentElement.className = '';
});
});