-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
158 lines (123 loc) · 4.88 KB
/
script.js
File metadata and controls
158 lines (123 loc) · 4.88 KB
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
document.addEventListener("DOMContentLoaded", () => {
const typeSelect = document.getElementById("type-select");
const categorySelect = document.getElementById("category-select");
const incomeCategories = ["Salary", "Freelance", "Investment", "Gift", "Interest"];
const expenseCategories = ["Food", "Transport", "Bills", "Rent", "Shopping", "Entertainment"];
;
typeSelect.addEventListener("change", () => {
let categories = typeSelect.value === "income" ? incomeCategories : expenseCategories;
categorySelect.innerHTML = "";
const defaultOption = document.createElement("option");
defaultOption.value = "";
defaultOption.disabled = true;
defaultOption.selected = true;
defaultOption.textContent = "Select Category";
categorySelect.appendChild(defaultOption);
categories.forEach(cat => {
const option = document.createElement("option");
option.value = cat;
option.textContent = cat;
categorySelect.appendChild(option);
});
});
let transactions = JSON.parse(localStorage.getItem("transactions")) || [];
document.getElementById("transaction-form").addEventListener("submit", function(e) {
e.preventDefault();
const amount = parseFloat(document.querySelector('input[type="number"]').value);
const type = typeSelect.value.toLowerCase();
const category = categorySelect.value;
const date = document.querySelector('input[type="date"]').value;
const note = document.querySelector('input[type="text"]').value;
if (!amount || !type || !category || !date) {
alert("Please fill all required fields.");
return;
}
transactions.push({ amount, type, category, date, note });
saveToLocal();
updateDashboardCards();
displayTransactions();
renderCategorySummaryList();
this.reset();
// Reset category dropdown to placeholder after form reset
categorySelect.innerHTML = "";
const defaultOption = document.createElement("option");
defaultOption.value = "";
defaultOption.disabled = true;
defaultOption.selected = true;
defaultOption.textContent = "Select Category";
categorySelect.appendChild(defaultOption);
});
function saveToLocal() {
localStorage.setItem("transactions", JSON.stringify(transactions));
}
function displayTransactions() {
const list = document.getElementById("transaction-list");
if (!list) return;
list.innerHTML = "";
transactions.forEach((t, i) => {
const item = document.createElement("li");
item.className = "list-group-item d-flex justify-content-between align-items-start";
item.innerHTML = `
<div class="ms-2 me-auto">
<div class="fw-bold">₹${t.amount} – ${t.category}</div>
<small>${t.date} • ${t.note || "No note"}</small>
</div>
<div>
<button class="btn btn-sm btn-outline-danger" onclick="deleteTransaction(${i})">❌</button>
</div>
`;
list.appendChild(item);
});
}
window.deleteTransaction = function(index) {
if (confirm("Are you sure you want to delete this transaction?")) {
transactions.splice(index, 1);
saveToLocal();
displayTransactions();
updateDashboardCards();
renderCategorySummaryList();
}
};
function updateDashboardCards() {
let totalIncome = 0;
let totalExpense = 0;
transactions.forEach(t => {
if (t.type === "income") totalIncome += t.amount;
else if (t.type === "expense") totalExpense += t.amount;
});
document.getElementById("total-income").textContent = `₹${totalIncome}`;
document.getElementById("total-expense").textContent = `₹${totalExpense}`;
document.getElementById("total-balance").textContent = `₹${totalIncome - totalExpense}`;
}
// NEW: Render expense overview as a list with percentage
function renderCategorySummaryList() {
const listContainer = document.getElementById("expense-summary-list");
if (!listContainer) return;
const categoryTotals = {};
let totalExpense = 0;
transactions.forEach(t => {
if (t.type === "expense") {
categoryTotals[t.category] = (categoryTotals[t.category] || 0) + t.amount;
totalExpense += t.amount;
}
});
listContainer.innerHTML = "";
if (totalExpense === 0) {
listContainer.innerHTML = '<li class="list-group-item text-muted">No expenses added yet.</li>';
return;
}
Object.entries(categoryTotals).forEach(([category, amount]) => {
const percent = ((amount / totalExpense) * 100).toFixed(1);
const li = document.createElement("li");
li.className = "list-group-item d-flex justify-content-between align-items-center";
li.innerHTML = `
<span>${category}</span>
<span>₹${amount.toFixed(2)} (${percent}%)</span>
`;
listContainer.appendChild(li);
});
}
displayTransactions();
updateDashboardCards();
renderCategorySummaryList();
});