-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
138 lines (123 loc) · 5.66 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
document.addEventListener('DOMContentLoaded', () => {
const expenseForm = document.getElementById('add-expense-form');
const customCategoryDiv = document.getElementById('custom-category-div');
const categorySelect = document.getElementById('category');
// Show custom category input if 'Custom' is selected
categorySelect.addEventListener('change', () => {
if (categorySelect.value === 'Custom') {
customCategoryDiv.style.display = 'block';
} else {
customCategoryDiv.style.display = 'none';
}
});
// Add a new expense on submit form
expenseForm.addEventListener('submit', (e) => {
e.preventDefault();
const amount = document.getElementById('amount').value;
const date = document.getElementById('date').value;
const category = categorySelect.value === 'Custom' ? document.getElementById('custom-category').value : categorySelect.value;
const description = document.getElementById('description').value;
const expense = { amount, date, category, description };
addExpense(expense);
expenseForm.reset();
customCategoryDiv.style.display = 'none';
updateUI();
});
updateUI();
});
let expenses = JSON.parse(localStorage.getItem('expenses')) || [];
// Add a new expense to the list and save to localStorage
function addExpense(expense) {
expenses.push(expense);
localStorage.setItem('expenses', JSON.stringify(expenses));
updateUI();
}
// Delete an expense from the list and update localStorage
function deleteExpense(index) {
expenses.splice(index, 1);
localStorage.setItem('expenses', JSON.stringify(expenses));
updateUI();
}
// Edit an existing expense in the list and update localStorage
function editExpense(index, updatedExpense) {
expenses[index] = updatedExpense;
localStorage.setItem('expenses', JSON.stringify(expenses));
updateUI();
}
// Update the UI with the latest expense data and redraw the chart
function updateUI() {
const totalExpenses = expenses.reduce((acc, expense) => acc + parseFloat(expense.amount), 0);
document.getElementById('total-expenses').textContent = ` ₹${totalExpenses.toFixed(2)}`;
const initialBudget = 1000; // Example initial budget
document.getElementById('remaining-budget').textContent = ` ₹${(initialBudget - totalExpenses).toFixed(2)}`;
const ctx = document.getElementById('expenseChart').getContext('2d');
const categories = [...new Set(expenses.map(expense => expense.category))];
const categoryTotals = categories.map(category => {
return expenses.filter(expense => expense.category === category)
.reduce((acc, expense) => acc + parseFloat(expense.amount), 0);
});
new Chart(ctx, {
type: 'pie',
data: {
labels: categories,
datasets: [{
label: 'Expenses',
data: categoryTotals,
backgroundColor: ['#FF6384', '#36A2EB', '#FFCE56', '#4BC0C0', '#9966FF'],
}]
},
options: {
responsive: true
}
});
// Render the expense list with edit and delete buttons
const expenseList = document.getElementById('expense-list');
expenseList.innerHTML = '';
expenses.forEach((expense, index) => {
const expenseItem = document.createElement('div');
expenseItem.className = 'bg-white p-4 rounded shadow-md mb-2';
expenseItem.innerHTML = `
<div class="flex justify-between items-center">
<div>
<div class="font-bold">${expense.description}</div>
<div>${expense.category} - ₹${expense.amount} on ${expense.date}</div>
</div>
<div>
<button class="bg-yellow-500 text-white p-1 rounded mr-2" onclick="showEditForm(${index})">Edit</button>
<button class="bg-red-500 text-white p-1 rounded" onclick="deleteExpense(${index})">Delete</button>
</div>
</div>
`;
expenseList.appendChild(expenseItem);
});
}
// Show the edit form with existing expense data
function showEditForm(index) {
const expense = expenses[index];
document.getElementById('amount').value = expense.amount;
document.getElementById('date').value = expense.date;
document.getElementById('category').value = expense.category;
document.getElementById('description').value = expense.description;
const expenseForm = document.getElementById('add-expense-form');
const customCategoryDiv = document.getElementById('custom-category-div');
if (expense.category === 'Custom') {
customCategoryDiv.style.display = 'block';
document.getElementById('custom-category').value = expense.category;
} else {
customCategoryDiv.style.display = 'none';
}
// Update the form submission to edit the expense
expenseForm.onsubmit = (e) => {
e.preventDefault();
const updatedExpense = {
amount: document.getElementById('amount').value,
date: document.getElementById('date').value,
category: document.getElementById('category').value === 'Custom' ? document.getElementById('custom-category').value : document.getElementById('category').value,
description: document.getElementById('description').value,
};
editExpense(index, updatedExpense);
expenseForm.onsubmit = null; // Reset to default behavior
expenseForm.reset();
customCategoryDiv.style.display = 'none';
};
}