-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
261 lines (225 loc) · 9.22 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
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
let expenses = [];
document.addEventListener('DOMContentLoaded', () => {
fetch('data.json')
.then(response => response.json())
.then(data => {
expenses = data;
renderExpenses();
createPaginationButtons();
calculateTotalAmount();
})
.catch(error => {
console.error('An error occurred while loading the data:', error);
});
document.querySelector('.button__search').addEventListener('click', filterExpenses);
});
function renderExpenses() {
const tableBody = document.querySelector('#expensesTable');
// tableBody.textContent = '';
expenses.forEach(expense => {
const row = document.createElement('tr');
row.classList.add('expense__item');
const nameCell = document.createElement('td');
nameCell.setAttribute('headers', 'name');
nameCell.classList.add('expenses-list__name');
nameCell.textContent = expense.name;
row.appendChild(nameCell);
const categoryCell = document.createElement('td');
categoryCell.setAttribute('headers', 'category');
categoryCell.classList.add('expenses-list__category');
categoryCell.textContent = expense.category;
row.appendChild(categoryCell);
const dateCell = document.createElement('td');
dateCell.setAttribute('headers', 'date');
dateCell.classList.add('expenses-list__date');
dateCell.textContent = expense.date;
row.appendChild(dateCell);
const priceCell = document.createElement('td');
priceCell.setAttribute('headers', 'price');
priceCell.classList.add('expenses-list__price');
const amountSpan = document.createElement('span');
amountSpan.classList.add('expenses-list__amount');
amountSpan.textContent = expense.price.amount;
priceCell.appendChild(amountSpan);
const currencySpan = document.createElement('span');
currencySpan.classList.add('expenses-list__currency');
currencySpan.textContent = expense.price.currency;
priceCell.appendChild(currencySpan);
row.appendChild(priceCell);
tableBody.appendChild(row);
});
}
// createPaginationButtons();
// window.addEventListener('DOMContentLoaded', () => {
// document.querySelector('.button__search').addEventListener('click', filterExpenses);
// calculateTotalAmount();
// });
document.querySelector('.button__search').addEventListener('click', filterExpenses);
calculateTotalAmount();
document.querySelector('.button__clear').addEventListener('click', clearForm);
//calculateTotalAmount
function calculateTotalAmount() {
const amountCells = document.querySelectorAll('.expenses-list__price .expenses-list__amount');
let totalAmount = 0;
amountCells.forEach(cell => {
const amount = parseFloat(cell.textContent);
if (!isNaN(amount) && cell.closest('.expense__item').style.display !== 'none') {
totalAmount += amount;
}
});
const totalAmountCell = document.getElementById('totalAmount');
totalAmountCell.textContent = Math.floor(totalAmount).toString();
}
//filterExpenses
function filterExpenses(event) {
event.preventDefault();
const selectedCategory = document.querySelector('.form__select').value;
const searchText = document.querySelector('.form__input').value.toLowerCase();
const expenseItems = document.querySelectorAll('.expense__item');
const startDate = document.querySelectorAll('.form__date')[0].value;
const endDate = document.querySelectorAll('.form__date')[1].value;
let matchingItems = 0;
expenseItems.forEach(item => {
const category = item.querySelector('.expenses-list__category').textContent;
const name = item.querySelector('.expenses-list__name').textContent.toLowerCase();
const date = item.querySelector('.expenses-list__date').textContent;
const isCategoryMatch = (selectedCategory === 'all' || selectedCategory === category);
const isNameMatch = name.includes(searchText);
if (isCategoryMatch && isNameMatch) {
item.style.display = 'none';
switch (true) {
case startDate != "" && endDate != "":
if (startDate <= date && endDate >= date) {
item.style.display = 'table-row';
matchingItems++;
}
break;
case startDate === "" && endDate != "":
if (endDate >= date) {
item.style.display = 'table-row';
matchingItems++;
}
break;
case startDate != "" && endDate === "":
if (startDate <= date) {
item.style.display = 'table-row';
matchingItems++;
}
break;
default:
item.style.display = 'table-row';
matchingItems++;
break;
}
} else {
item.style.display = 'none';
}
});
const noMatchingItems = matchingItems === 0;
const noexpense = document.querySelector('.expenses-list__noexpense');
const paginationButtons = document.querySelectorAll('.pagination__button');
if (startDate >= endDate && endDate != '') {
const tdElement = noexpense.querySelector('td');
tdElement.textContent = 'There is no such expense. The start date cannot be greater than the end date.'
}
if (noexpense) {
noexpense.style.display = noMatchingItems ? 'flex' : 'none';
paginationButtons.forEach(button => {
button.style.display = noMatchingItems ? 'none' : 'flex';
});
}
calculateTotalAmount()
createPaginationButtons();
}
function clearForm(event) {
event.preventDefault();
const inputElements = document.querySelectorAll('input');
inputElements.forEach((input) => {
input.value = '';
});
const selectElement = document.querySelector('select');
if (selectElement) {
const options = selectElement.querySelectorAll('option');
if (options.length > 0) {
selectElement.value = options[0].value;
}
}
createPaginationButtons();
filterExpenses(event);
}
//pagination
function createPaginationButtons() {
removePaginationButtons();
const expenseItems = document.querySelectorAll('.expense__item');
const selectedItems = []
expenseItems.forEach(item => {
if (getComputedStyle(item).display === 'table-row') {
selectedItems.push(item);
}
});
if (selectedItems !== []) {
const paginationContainer = document.querySelector('.pagination');
const itemsPerPage = 8;
const pageCount = Math.ceil(selectedItems.length / itemsPerPage);
for (let i = 1; i <= pageCount; i++) {
const button = document.createElement('button');
button.innerText = i;
button.classList.add('pagination__button');
button.addEventListener('click', () => goToPage(i, selectedItems));
paginationContainer.appendChild(button);
}
goToPage(1, selectedItems);
}
}
function removePaginationButtons() {
const paginationContainer = document.querySelector('.pagination');
paginationContainer.innerHTML = '';
}
function goToPage(pageNumber, expenseItems) {
if (expenseItems.length > 0) {
const paginationContainer = document.querySelector('.pagination');
const itemsPerPage = 8;
const startIndex = (pageNumber - 1) * itemsPerPage;
const endIndex = pageNumber * itemsPerPage;
expenseItems.forEach((item, index) => {
if (index >= startIndex && index < endIndex) {
item.style.display = 'table-row';
} else {
item.style.display = 'none';
}
});
calculateTotalAmount();
const buttons = paginationContainer.querySelectorAll('button');
buttons.forEach((button, index) => {
if (index === pageNumber - 1) {
button.classList.add('active-page');
} else {
button.classList.remove('active-page');
}
});
}
}
//navigation
const toggleButton = document.querySelector('.nav__toggle-button');
const navList = document.querySelector('.nav__list');
const toggleButtonBars = document.querySelectorAll('.nav__toggle-button-bar');
let currentColor = 'var(--text-color-primary-on-background))';
toggleButton.addEventListener('click', () => {
navList.classList.toggle('active');
toggleButtonBars.forEach(bar => {
bar.style.backgroundColor = currentColor;
});
currentColor = (currentColor === 'var(--text-color-primary-on-background)')
? 'var(--text-color-primary-on-background)'
: 'var(--secondary-color)';
});
function removeActiveClass() {
if (navList.classList.contains('active') && window.innerWidth > 767) {
navList.classList.remove('active');
currentColor = 'var(--text-color-primary-on-background)';
}
toggleButtonBars.forEach(bar => {
bar.style.backgroundColor = currentColor;
});
}
window.addEventListener('resize', removeActiveClass);