-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathindex.html
475 lines (430 loc) · 15.8 KB
/
index.html
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Expense Tracker</title>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/date-fns/dist/date-fns.min.js"></script>
<style>
:root {
--primary-color: #3498db;
--secondary-color: #2ecc71;
--background-color: #f5f5f5;
--text-color: #333;
}
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: Arial, sans-serif;
line-height: 1.6;
color: var(--text-color);
background-color: var(--background-color);
}
.container {
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
header {
background-color: var(--primary-color);
color: white;
text-align: center;
padding: 1rem;
}
h1, h2 {
margin-bottom: 1rem;
}
form {
background-color: white;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
margin-bottom: 20px;
}
input, select, button {
display: block;
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ddd;
border-radius: 4px;
}
button {
background-color: var(--secondary-color);
color: white;
border: none;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #27ae60;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
background-color: white;
}
th, td {
padding: 12px;
text-align: left;
border-bottom: 1px solid #ddd;
}
th {
background-color: var(--primary-color);
color: white;
}
.charts {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.chart {
width: calc(50% - 10px);
margin-bottom: 20px;
background-color: white;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
@media (max-width: 768px) {
.chart {
width: 100%;
}
}
</style>
</head>
<body>
<header>
<h1>Expense Tracker</h1>
</header>
<div class="container">
<form id="expense-form">
<h2>Add/Edit Expense</h2>
<input type="hidden" id="expense-id">
<input type="date" id="expense-date" required>
<input type="number" id="expense-amount" placeholder="Amount" step="0.01" required>
<input type="text" id="expense-description" placeholder="Description" required>
<select id="expense-category" required></select>
<label>
<input type="checkbox" id="expense-recurring">
Recurring Expense
</label>
<button type="submit">Save Expense</button>
</form>
<div>
<h2>Expenses</h2>
<input type="date" id="filter-start-date">
<input type="date" id="filter-end-date">
<button id="filter-button">Filter</button>
<table id="expenses-table">
<thead>
<tr>
<th>Date</th>
<th>Amount</th>
<th>Description</th>
<th>Category</th>
<th>Recurring</th>
<th>Actions</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
<div class="charts">
<div class="chart">
<canvas id="category-chart"></canvas>
</div>
<div class="chart">
<canvas id="monthly-chart"></canvas>
</div>
<div class="chart">
<canvas id="trend-chart"></canvas>
</div>
</div>
<div>
<h2>Categories</h2>
<form id="category-form">
<input type="text" id="category-name" placeholder="Category Name" required>
<button type="submit">Add Category</button>
</form>
<ul id="categories-list"></ul>
</div>
</div>
<script>
const API_URL = 'http://localhost:7896';
let expenses = [];
let categories = [];
// Fetch expenses
async function fetchExpenses() {
try {
const response = await axios.get(`${API_URL}/expenses`);
expenses = response.data;
renderExpensesTable();
updateCharts();
} catch (error) {
console.error('Error fetching expenses:', error);
}
}
// Fetch categories
async function fetchCategories() {
try {
const response = await axios.get(`${API_URL}/categories`);
categories = response.data;
renderCategoriesSelect();
renderCategoriesList();
} catch (error) {
console.error('Error fetching categories:', error);
}
}
// Render expenses table
function renderExpensesTable() {
const tbody = document.querySelector('#expenses-table tbody');
tbody.innerHTML = '';
expenses.forEach(expense => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${expense.date}</td>
<td>$${expense.amount.toFixed(2)}</td>
<td>${expense.description}</td>
<td>${getCategoryName(expense.category_id)}</td>
<td>${expense.is_recurring ? 'Yes' : 'No'}</td>
<td>
<button onclick="editExpense(${expense.id})">Edit</button>
<button onclick="deleteExpense(${expense.id})">Delete</button>
</td>
`;
tbody.appendChild(tr);
});
}
// Render categories select
function renderCategoriesSelect() {
const select = document.getElementById('expense-category');
select.innerHTML = '';
categories.forEach(category => {
const option = document.createElement('option');
option.value = category.id;
option.textContent = category.name;
select.appendChild(option);
});
}
// Render categories list
function renderCategoriesList() {
const ul = document.getElementById('categories-list');
ul.innerHTML = '';
categories.forEach(category => {
const li = document.createElement('li');
li.textContent = category.name;
ul.appendChild(li);
});
}
// Get category name by id
function getCategoryName(id) {
const category = categories.find(c => c.id === id);
return category ? category.name : 'Unknown';
}
// Add or edit expense
document.getElementById('expense-form').addEventListener('submit', async (e) => {
e.preventDefault();
const id = document.getElementById('expense-id').value;
const expense = {
date: document.getElementById('expense-date').value,
amount: parseFloat(document.getElementById('expense-amount').value),
description: document.getElementById('expense-description').value,
category_id: parseInt(document.getElementById('expense-category').value),
is_recurring: document.getElementById('expense-recurring').checked
};
try {
if (id) {
await axios.put(`${API_URL}/expenses/${id}`, expense);
} else {
await axios.post(`${API_URL}/expenses`, expense);
}
document.getElementById('expense-form').reset();
document.getElementById('expense-id').value = '';
fetchExpenses();
} catch (error) {
console.error('Error saving expense:', error);
}
});
// Edit expense
function editExpense(id) {
const expense = expenses.find(e => e.id === id);
document.getElementById('expense-id').value = expense.id;
document.getElementById('expense-date').value = expense.date;
document.getElementById('expense-amount').value = expense.amount;
document.getElementById('expense-description').value = expense.description;
document.getElementById('expense-category').value = expense.category_id;
document.getElementById('expense-recurring').checked = expense.is_recurring;
}
// Delete expense
async function deleteExpense(id) {
if (confirm('Are you sure you want to delete this expense?')) {
try {
await axios.delete(`${API_URL}/expenses/${id}`);
fetchExpenses();
} catch (error) {
console.error('Error deleting expense:', error);
}
}
}
// Add category
document.getElementById('category-form').addEventListener('submit', async (e) => {
e.preventDefault();
const name = document.getElementById('category-name').value;
try {
await axios.post(`${API_URL}/categories`, { name });
document.getElementById('category-form').reset();
fetchCategories();
} catch (error) {
console.error('Error adding category:', error);
}
});
// Update charts
function updateCharts() {
updateCategoryChart();
updateMonthlyChart();
updateTrendChart();
}
// Update category chart
function updateCategoryChart() {
const ctx = document.getElementById('category-chart').getContext('2d');
const categoryTotals = {};
expenses.forEach(expense => {
const categoryName = getCategoryName(expense.category_id);
categoryTotals[categoryName] = (categoryTotals[categoryName] || 0) + expense.amount;
});
new Chart(ctx, {
type: 'pie',
data: {
labels: Object.keys(categoryTotals),
datasets: [{
data: Object.values(categoryTotals),
backgroundColor: [
'#FF6384', '#36A2EB', '#FFCE56', '#4BC0C0', '#9966FF', '#FF9F40'
]
}]
},
options: {
responsive: true,
title: {
display: true,
text: 'Expenses by Category'
}
}
});
}
// Update monthly chart
function updateMonthlyChart() {
const ctx = document.getElementById('monthly-chart').getContext('2d');
const monthlyTotals = {};
expenses.forEach(expense => {
const month = expense.date.substring(0, 7); // YYYY-MM
monthlyTotals[month] = (monthlyTotals[month] || 0) + expense.amount;
});
const sortedMonths = Object.keys(monthlyTotals).sort();
new Chart(ctx, {
type: 'bar',
data: {
labels: sortedMonths,
datasets: [{
label: 'Monthly Spending',
data: sortedMonths.map(month => monthlyTotals[month]),
backgroundColor: '#36A2EB'
}]
},
options: {
responsive: true,
title: {
display: true,
text: 'Monthly Spending'
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
}
// Update trend chart
function updateTrendChart() {
const ctx = document.getElementById('trend-chart').getContext('2d');
const dailyTotals = {};
expenses.forEach(expense => {
dailyTotals[expense.date] = (dailyTotals[expense.date] || 0) + expense.amount;
});
const sortedDates = Object.keys(dailyTotals).sort();
new Chart(ctx, {
type: 'line',
data: {
labels: sortedDates,
datasets: [{
label: 'Daily Spending',
data: sortedDates.map(date => dailyTotals[date]),
borderColor: '#4BC0C0',
fill: false
}]
},
options: {
responsive: true,
title: {
display: true,
text: 'Spending Trend'
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
}
// Filter expenses
document.getElementById('filter-button').addEventListener('click', () => {
const startDate = document.getElementById('filter-start-date').value;
const endDate = document.getElementById('filter-end-date').value;
if (startDate && endDate) {
const filteredExpenses = expenses.filter(expense => {
return expense.date >= startDate && expense.date <= endDate;
});
renderFilteredExpenses(filteredExpenses);
} else {
renderExpensesTable();
}
});
// Render filtered expenses
function renderFilteredExpenses(filteredExpenses) {
const tbody = document.querySelector('#expenses-table tbody');
tbody.innerHTML = '';
filteredExpenses.forEach(expense => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${expense.date}</td>
<td>$${expense.amount.toFixed(2)}</td>
<td>${expense.description}</td>
<td>${getCategoryName(expense.category_id)}</td>
<td>${expense.is_recurring ? 'Yes' : 'No'}</td>
<td>
<button onclick="editExpense(${expense.id})">Edit</button>
<button onclick="deleteExpense(${expense.id})">Delete</button>
</td>
`;
tbody.appendChild(tr);
});
}
// Initialize
fetchExpenses();
fetchCategories();
</script>
</body>
</html>