-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
215 lines (178 loc) · 6.75 KB
/
Copy pathscript.js
File metadata and controls
215 lines (178 loc) · 6.75 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
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
import { db, auth } from "./firebase.js";
import { doc, setDoc } from "https://www.gstatic.com/firebasejs/10.12.0/firebase-firestore.js";
import { saveSavingsGoal } from "./firebase.js";
const saveBudgetAndCategories = async (uid, budget, categoryAmounts) => {
try {
const prefsRef = doc(db, "userPreferences", uid);
await setDoc(prefsRef, {
budget,
categoryAmounts,
}, { merge: true });
console.log("✅ Budget & categories saved.");
} catch (error) {
console.error("❌ Error saving budget and categories:", error);
}
};
// fill missing days in savingsHistory (kept inside firebase.js, so removed here)
document.getElementById("generate-budget-btn").addEventListener("click", async () => {
const checkboxes = document.querySelectorAll('input[type="checkbox"]:checked');
const selectedCategories = Array.from(checkboxes).map(cb => cb.value);
const categoryAmounts = {};
selectedCategories.forEach(cat => {
const inputId = document.querySelector(`input[data-target][value="${cat}"]`).dataset.target;
const amountInput = document.getElementById(inputId);
const amount = parseFloat(amountInput.value) || 0;
categoryAmounts[cat] = amount;
});
const savingsGoal = parseFloat(document.getElementById("savingsGoalInput").value) || 0;
const user = auth.currentUser;
if (!user) {
alert("Please log in first!");
return;
}
const userDocRef = doc(db, "userPreferences", user.uid);
const totalExpenses = Object.values(categoryAmounts).reduce((sum, amt) => sum + amt, 0);
try {
await setDoc(userDocRef, {
selectedCategories,
categoryAmounts,
savingsGoal,
budget: totalBudget,
expenses: totalExpenses
}, { merge: true });
// Save savings goal + history after budget data is saved
await saveSavingsGoal(savingsGoal, user.uid);
console.log("🔥 User data saved to Firestore with savings goal + history:");
console.log("Categories:", selectedCategories);
console.log("Amounts:", categoryAmounts);
console.log("Savings Goal: ₹", savingsGoal);
console.log("Total Budget: ₹", totalBudget);
alert("✅ Budget and savings goal saved! Redirecting to dashboard...");
window.location.href = "dashboard.html";
} catch (err) {
console.error("❌ Error saving data:", err);
alert("Error saving budget or savings goal. Try again!");
}
});
// Checkbox toggle input
document.querySelectorAll(".category-check").forEach(checkbox => {
checkbox.addEventListener("change", () => {
const inputId = checkbox.dataset.target;
const input = document.getElementById(inputId);
input.disabled = !checkbox.checked;
if (!checkbox.checked) input.value = "";
});
});
// Total amount calculator
const amountInputs = document.querySelectorAll(".amount-input");
const totalAmountEl = document.getElementById("total-amount");
amountInputs.forEach(input => {
input.addEventListener("input", () => {
let total = 0;
amountInputs.forEach(inp => {
if (!inp.disabled && inp.value) {
total += parseFloat(inp.value);
}
});
totalAmountEl.textContent = total.toFixed(2);
});
});
// Save goal button
document.getElementById("saveGoalBtn")?.addEventListener("click", async () => {
const goalInput = parseFloat(document.getElementById("savingsGoalInput").value);
const confirmation = document.getElementById("goalConfirmation");
const user = auth.currentUser;
if (!goalInput || goalInput <= 0) {
confirmation.textContent = "Please enter a valid amount!";
confirmation.style.color = "red";
confirmation.style.display = "block";
return;
}
if (!user) {
alert("Please log in first!");
return;
}
try {
await saveSavingsGoal(goalInput, user.uid);
confirmation.textContent = `₹${goalInput} goal saved! 🔥`;
confirmation.style.color = "green";
confirmation.style.display = "block";
console.log("✅ Savings goal + history updated!");
} catch (err) {
console.error("❌ Error saving goal:", err);
confirmation.textContent = "Error saving goal. Try again!";
confirmation.style.color = "red";
confirmation.style.display = "block";
}
});
let totalBudget = 0;
// Handle budget save
document.getElementById("saveBudgetBtn").addEventListener("click", async () => {
const budgetInput = parseFloat(document.getElementById("totalBudgetInput").value);
const confirmation = document.getElementById("budgetConfirmation");
const user = auth.currentUser;
if (!budgetInput || budgetInput <= 0) {
confirmation.innerText = "Please enter a valid budget amount.";
confirmation.style.color = "red";
confirmation.style.display = "block";
return;
}
if (!user) {
alert("Please log in first!");
return;
}
totalBudget = budgetInput;
confirmation.innerText = `Total Budget set to ₹${totalBudget}`;
confirmation.style.color = "green";
confirmation.style.display = "block";
try {
await setDoc(doc(db, "userPreferences", user.uid), {
budget: totalBudget,
}, { merge: true });
console.log("✅ Budget saved in Firestore: ₹" + totalBudget);
} catch (err) {
console.error("❌ Error saving budget:", err);
confirmation.innerText = "Error saving budget. Try again!";
confirmation.style.color = "red";
confirmation.style.display = "block";
}
const categoryAmounts = {};
document.querySelectorAll('input[type="checkbox"]:checked').forEach(cat => {
const inputId = cat.dataset.target;
const amount = parseFloat(document.getElementById(inputId).value) || 0;
categoryAmounts[cat.value] = amount;
});
await saveBudgetAndCategories(user.uid, totalBudget, categoryAmounts);
updateRemainingAmount();
});
document.getElementById("saveBudgetBtn").addEventListener("click", async () => {
const budgetInput = parseFloat(document.getElementById("totalBudgetInput").value);
const confirmation = document.getElementById("budgetConfirmation");
const user = auth.currentUser;
if (!budgetInput || budgetInput <= 0) {
confirmation.innerText = "Please enter a valid budget amount.";
confirmation.style.color = "red";
confirmation.style.display = "block";
return;
}
if (!user) {
alert("Please log in first!");
return;
}
totalBudget = budgetInput;
confirmation.innerText = `Total Budget set to ₹${totalBudget}`;
confirmation.style.color = "green";
confirmation.style.display = "block";
try {
await setDoc(doc(db, "userPreferences", user.uid), {
budget: totalBudget,
}, { merge: true });
console.log("✅ Budget saved in Firestore: ₹" + totalBudget);
} catch (err) {
console.error("❌ Error saving budget:", err);
confirmation.innerText = "Error saving budget. Try again!";
confirmation.style.color = "red";
confirmation.style.display = "block";
}
updateRemainingAmount();
});