-
Notifications
You must be signed in to change notification settings - Fork 0
/
budget.js
213 lines (171 loc) · 5.87 KB
/
budget.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
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
// SELECT ELEMENTS
const balanceEl = document.querySelector(".balance .value");
const incomeTotalEl = document.querySelector(".income-total");
const outcomeTotalEl = document.querySelector(".outcome-total");
const incomeEl = document.querySelector("#income");
const expenseEl = document.querySelector("#expense");
const allEl = document.querySelector("#all");
const incomeList = document.querySelector("#income .list");
const expenseList = document.querySelector("#expense .list");
const allList = document.querySelector("#all .list");
// SELECT BTNS
const expenseBtn = document.querySelector(".tab1");
const incomeBtn = document.querySelector(".tab2");
const allBtn = document.querySelector(".tab3");
// INPUT BTS
const addExpense = document.querySelector(".add-expense");
const expenseTitle = document.getElementById("expense-title-input");
const expenseAmount = document.getElementById("expense-amount-input");
const addIncome = document.querySelector(".add-income");
const incomeTitle = document.getElementById("income-title-input");
const incomeAmount = document.getElementById("income-amount-input");
// VARIABLES
let ENTRY_LIST;
let balance = 0, income = 0, outcome = 0;
const DELETE = "delete", EDIT = "edit";
// LOOK IF THERE IS SAVED DATA IN LOCALSTORAGE
ENTRY_LIST = JSON.parse(localStorage.getItem("entry_list")) || [];
updateUI();
// EVENT LISTENERS
expenseBtn.addEventListener("click", function(){
show(expenseEl);
hide( [incomeEl, allEl] );
active( expenseBtn );
inactive( [incomeBtn, allBtn] );
})
incomeBtn.addEventListener("click", function(){
show(incomeEl);
hide( [expenseEl, allEl] );
active( incomeBtn );
inactive( [expenseBtn, allBtn] );
})
allBtn.addEventListener("click", function(){
show(allEl);
hide( [incomeEl, expenseEl] );
active( allBtn );
inactive( [incomeBtn, expenseBtn] );
})
addExpense.addEventListener("click", function(){
// IF ONE OF THE INPUTS IS EMPTY => EXIT
if(!expenseTitle.value || !expenseAmount.value ) return;
// SAVE THE ENTRY TO ENTRY_LIST
let expense = {
type : "expense",
title : expenseTitle.value,
amount : parseInt(expenseAmount.value)
}
ENTRY_LIST.push(expense);
updateUI();
clearInput( [expenseTitle, expenseAmount] )
})
addIncome.addEventListener("click", function(){
// IF ONE OF THE INPUTS IS EMPTY => EXIT
if(!incomeTitle.value || !incomeAmount.value ) return;
// SAVE THE ENTRY TO ENTRY_LIST
let income = {
type : "income",
title : incomeTitle.value,
amount : parseInt(incomeAmount.value)
}
ENTRY_LIST.push(income);
updateUI();
clearInput( [incomeTitle, incomeAmount] )
})
incomeList.addEventListener("click", deleteOrEdit);
expenseList.addEventListener("click", deleteOrEdit);
allList.addEventListener("click", deleteOrEdit);
// HELPERS
function deleteOrEdit(event){
const targetBtn = event.target;
const entry = targetBtn.parentNode;
if( targetBtn.id == DELETE ){
deleteEntry(entry);
}else if(targetBtn.id == EDIT ){
editEntry(entry);
}
}
function deleteEntry(entry){
ENTRY_LIST.splice( entry.id, 1);
updateUI();
}
function editEntry(entry){
console.log(entry)
let ENTRY = ENTRY_LIST[entry.id];
if(ENTRY.type == "income"){
incomeAmount.value = ENTRY.amount;
incomeTitle.value = ENTRY.title;
}else if(ENTRY.type == "expense"){
expenseAmount.value = ENTRY.amount;
expenseTitle.value = ENTRY.title;
}
deleteEntry(entry);
}
function updateUI(){
income = calculateTotal("income", ENTRY_LIST);
outcome = calculateTotal("expense", ENTRY_LIST);
balance = Math.abs(calculateBalance(income, outcome));
// DETERMINE SIGN OF BALANCE
let sign = (income >= outcome) ? "₹" : "-₹";
// UPDATE UI
balanceEl.innerHTML = `<small>${sign}</small>${balance}`;
outcomeTotalEl.innerHTML = `<small>₹</small>${outcome}`;
incomeTotalEl.innerHTML = `<small>₹</small>${income}`;
clearElement( [expenseList, incomeList, allList] );
ENTRY_LIST.forEach( (entry, index) => {
if( entry.type == "expense" ){
showEntry(expenseList, entry.type, entry.title, entry.amount, index)
}else if( entry.type == "income" ){
showEntry(incomeList, entry.type, entry.title, entry.amount, index)
}
showEntry(allList, entry.type, entry.title, entry.amount, index)
});
updateChart(income, outcome);
localStorage.setItem("entry_list", JSON.stringify(ENTRY_LIST));
}
function showEntry(list, type, title, amount, id){
const entry = ` <li id = "${id}" class="${type}">
<div class="entry">${title}: ₹${amount}</div>
<div id="edit"></div>
<div id="delete"></div>
</li>`;
const position = "afterbegin";
list.insertAdjacentHTML(position, entry);
}
function clearElement(elements){
elements.forEach( element => {
element.innerHTML = "";
})
}
function calculateTotal(type, list){
let sum = 0;
list.forEach( entry => {
if( entry.type == type ){
sum += entry.amount;
}
})
return sum;
}
function calculateBalance(income, outcome){
return income - outcome;
}
function clearInput(inputs){
inputs.forEach( input => {
input.value = "";
})
}
function show(element){
element.classList.remove("hide");
}
function hide( elements ){
elements.forEach( element => {
element.classList.add("hide");
})
}
function active(element){
element.classList.add("active");
}
function inactive( elements ){
elements.forEach( element => {
element.classList.remove("active");
})
}