-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
216 lines (189 loc) · 6.31 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
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
class UI {
constructor() {
this.budgetFeedback = document.querySelector(".budget-feedback");
this.expenseFeedback = document.querySelector(".expense-feedback");
this.budgetForm = document.getElementById("budget-form");
this.budgetInput = document.getElementById("budget-input");
this.budgetAmount = document.getElementById("budget-amount");
this.expenseAmount = document.getElementById("expense-amount");
this.balance = document.getElementById("balance");
this.balanceAmount = document.getElementById("balance-amount");
this.expenseForm = document.getElementById("expense-form");
this.expenseInput = document.getElementById("expense-input");
this.amountInput = document.getElementById("amount-input");
this.expenseList = document.getElementById("expense-list");
this.itemList = [];
this.itemID = 0;
}
//submit budget method
submitBudgetForm (){
const value = this.budgetInput.value;
if (value === "" || value < 0 ) {
this.budgetFeedback.classList.add("showItem");
this.budgetFeedback.innerHTML = `<p>value cannot be empty or negative</p>`;
const self = this;
// console.log(this);
setTimeout(function(){
self.budgetFeedback.classList.remove('showItem');
}, 4000);
}
//Passes the value
else {
this.budgetAmount.textContent = value;
this.budgetInput.value = '';
this.showBalance();
}
}
//Show Balance
showBalance(){
// console.log(`hey I am getting a hold of 'this' keyword`);
// Calculate expenses
const expense = this.totalExpense();
// Grabs the value in the budget
const total = parseInt(this.budgetAmount.textContent) - expense;
this.balanceAmount.textContent= total;
if (total < 0) {
this.balance.classList.remove('showGreen', 'showBlack');
this.balance.classList.add('showRed');
}
else if (total > 0) {
this.balance.classList.remove('showRed', 'showBlack');
this.balance.classList.add('showGreen');
}
else if (total === 0) {
this.balance.classList.remove('showRed', 'showGreeb');
this.balance.classList.add('showBlack');
}
}
// Submit Expense Form
submitExpenseForm(){
const expenseValue = this.expenseInput.value;
const amountValue = this.amountInput.value;
if (expenseValue === '' || amountValue ==='' || amountValue < 0)
{
this.expenseFeedback.classList.add('showItem');
this.expenseFeedback.innerHTML = `<p> values cannot be empty or negative</p>`
const self = this;
setTimeout(function(){
self.expenseFeedback.classList.remove('showItem');
}, 4000);
}
else {
let amount = parseInt(amountValue);
// clears the value
this.expenseInput.value = "";
this.amountInput.value = "";
let expense = {
id: this.itemID,
title: expenseValue,
amount: amount,
}
this.itemID++;
this.itemList.push(expense);
this.addExpense(expense);
this.showBalance();
// show balance
}
}
// Add Expense
addExpense(expense){
const div = document.createElement('div');
div.classList.add('expense');
div.innerHTML =
`<div class="expense-item d-flex justify-content-between align-items-baseline">
<h6 class="expense-title mb-0 text-uppercase list-item">${expense.title}</h6>
<h5 class="expense-amount mb-0 list-item">${expense.amount}</h5>
<div class="expense-icons list-item">
<a href="#" class="edit-icon mx-2" data-id="${expense.id}">
<i class="fas fa-edit"></i>
</a>
<a href="#" class="delete-icon" data-id="${expense.id}">
<i class="fas fa-trash"></i>
</a>
</div>
</div>
`
;
this.expenseList.appendChild(div);
}
// Total expense
// If there is more than one item, we want to start calculating
totalExpense(){
let total = 0;
if(this.itemList.length > 0){
total = this.itemList.reduce(function(acc, curr){
console.log(`total is ${acc} and the current value is ${curr.amount}`);
acc += curr.amount
return acc;
}, 0)
// console.log(this.itemList);
}
this.expenseAmount.textContent = total;
return total;
}
// edit expense
editExpense(element) {
let id = parseInt(element.dataset.id)
let parent = element.parentElement.parentElement.parentElement;
// remove-whatever parent we are getting back will be removed from the dom
this.expenseList.removeChild(parent);
// remove from the list
let expense = this.itemList.filter(function(item){
return item.id === id;
})
// show value
this.expenseInput.value = expense[0].title;
this.amountInput.value = expense[0]. amount;
// remove from the list
let tempList = this.itemList.filter(function(item){
return item.id !==id;
})
this.itemList = tempList;
this.showBalance();
}
// delete expense
deleteExpense(element) {
let id = parseInt(element.dataset.id)
let parent = element.parentElement.parentElement.parentElement;
// remove-whatever parent we are getting back will be removed from the dom
this.expenseList.removeChild(parent);
// remove from the list
// remove from the list
let tempList = this.itemList.filter(function(item){
return item.id !==id;
})
this.itemList = tempList;
this.showBalance();
}
}
function eventListeners(){
const budgetForm = document.getElementById('budget-form');
const expenseForm = document.getElementById('expense-form');
const expenseList = document.getElementById('expense-list');
//new instance of UI class
const ui = new UI()
// budget form submit
budgetForm.addEventListener('submit', function(event){
event.preventDefault();
ui.submitBudgetForm();
});
// expense form submit
expenseForm.addEventListener('submit', function(event){
event.preventDefault();
ui.submitExpenseForm();
});
// expense list click
expenseList.addEventListener('click', function(event){
// event propogation
if (event.target.parentElement.classList.contains('edit-icon')){
ui.editExpense(event.target.parentElement);
}
else if (event.target.parentElement.classList.contains('delete-icon')){
ui.deleteExpense(event.target.parentElement);
}
});
}
document.addEventListener('DOMContentLoaded', function(){
eventListeners();
})
//Constructor functions are available everywhere; always declare it first and then make use of it.