-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
156 lines (110 loc) · 4.15 KB
/
index.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
const form = document.querySelector(".add");
const incomeList = document.querySelector("ul.income-list");
const expenseList = document.querySelector("ul.expense-list");
const balance = document.getElementById("balance");
const income = document.getElementById("income");
const expense = document.getElementById("expense");
const sectionHistory = document.getElementById("history");
let transactions = localStorage.getItem("transactions") !== null ? JSON.parse(localStorage.getItem("transactions")) : [];
function updateStatistics(){
const updatedIncome = transactions
.filter(transaction => transaction.amount > 0)
.reduce((total, transaction)=> total += transaction.amount, 0);
//console.log(updatedIncome);
const updatedExpense = transactions
.filter(transaction => transaction.amount < 0)
.reduce((total, transaction) => total += Math.abs(transaction.amount), 0);
//console.log(updatedExpense);
updatedBalance = updatedIncome - updatedExpense;
balance.textContent = updatedBalance;
income.textContent = updatedIncome;
expense.textContent = updatedExpense;
}
function generateTemplate(id, source, amount, time){
return `<li data-id="${id}">
<p>
<span>${source}</span>
<span id="time">${time}</span>
</p>
$<span>${Math.abs(amount)}</span>
<i class="bi bi-trash delete"></i>
</li>`;
//The Math.abs() METHOD IS TO REMOVE '-' (MINUS) SYMBOL
}
function addTransactionDOM(id, source, amount, time){
if(amount > 0){
incomeList.innerHTML += generateTemplate(id, source, amount, time);
}else{
expenseList.innerHTML += generateTemplate(id, source, amount, time);
}
}
function addTransaction(source, amount){
const time = new Date();
const transaction = {
id: Math.floor(Math.random()*100000),
source: source,
amount: amount,
time: `${time.toLocaleTimeString()} ${time.toLocaleDateString()}`
};
transactions.push(transaction);
localStorage.setItem("transactions", JSON.stringify(transactions));
addTransactionDOM(transaction.id, source, amount, transaction.time );
}
form.addEventListener("submit", event =>{
event.preventDefault();
stateHistory();
if(form.source.value.trim() === "" || form.amount.value === ""){
return alert("Please add proper values!");
}
addTransaction(form.source.value.trim(), Number(form.amount.value));
//this reset method is when someone fill the form, and after the event it will be clear
updateStatistics();
form.reset();
});
function stateHistory(){
if(transactions.length === 0){
sectionHistory.classList.toggle("hide");
}
} ;
function getTransaction(){
transactions.forEach(transaction => {
if(transaction.amount > 0){
incomeList.innerHTML += generateTemplate(transaction.id, transaction.source, transaction.amount, transaction.time);
}else{
expenseList.innerHTML += generateTemplate(transaction.id, transaction.source, transaction.amount, transaction.time);
}
});
};
function deleteTransaction(id){
//console.log(id);
transactions = transactions.filter(transaction =>{
//console.log(transaction.id, id);
return transaction.id !== id;
});
console.log(transactions);
//we are overwriting into the same transaction array
localStorage.setItem("transactions", JSON.stringify(transactions));
stateHistory();
}
incomeList.addEventListener("click", event =>{
if(event.target.classList.contains("delete")){
event.target.parentElement.remove();
deleteTransaction(Number(event.target.parentElement.dataset.id));
updateStatistics();
; }
});
expenseList.addEventListener("click", event =>{
if(event.target.classList.contains("delete")){
//console.log(event.target);
event.target.parentElement.remove();
deleteTransaction(Number(event.target.parentElement.dataset.id));
updateStatistics();
}
});
//to bundle functions. Bundle all the functions called in the page
function init(){
updateStatistics();
getTransaction();
stateHistory();
}
init();