-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
353 lines (307 loc) · 10.9 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
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
import { sendData, getEmailFromUser, retriveDataFromDatabase } from "./save.js";
const financeText = document.querySelector("#expenseText"),
financeDate = document.querySelector("#expenseDate"),
financeAmount = document.querySelector("#expenseAmount"),
financeBtn = document.querySelector("#addExpenseBtn"),
financeMessage = document.querySelector("#messageDiv"),
financeTable = document.querySelector("#expenseTable"),
expenseWarning = document.querySelector("#warning"),
grandTotal = document.querySelector("#financeTotal"),
settingsModal = document.querySelector(".settings-modal"),
settingBtn = document.getElementById("setting"),
currencySelect = document.getElementById("currency");
let currency = localStorage.getItem("currency") || "₦";
let autoSave = localStorage.getItem("autoSave");
autoSave = JSON.parse(autoSave);
let loggedIn = false;
// Call the update function when your page loads
window.onload = function () {
const userEmail = localStorage.getItem("email") || getEmailFromUser();
localStorage.setItem("email", userEmail);
if (userEmail) {
retriveDataFromDatabase(userEmail);
currencySelect.value = currency;
} else {
updateTableFromLocalStorage();
}
};
if (localStorage.getItem("email")) {
loggedIn = true;
} else {
loggedIn = false;
}
financeDate.valueAsDate = new Date();
settingBtn.addEventListener(
"click",
() => (settingsModal.style.display = "block")
);
function processAutoSave() {
let email = localStorage.getItem("email") || getEmailFromUser();
if (autoSave) {
sendData(email);
}
}
window.chooseCurrency = function () {
currency = currencySelect.value;
localStorage.setItem("currency", currency);
processAutoSave();
updateTableFromLocalStorage();
updateBudget();
const message = document.getElementById("setting-messages");
message.textContent = "Settings Saved";
setTimeout(() => {
message.textContent = "";
}, 3000);
};
window.closeSettingModal = function () {
settingsModal.style.display = "none";
};
window.resetData = function () {
localStorage.removeItem("income");
localStorage.removeItem("rowId");
localStorage.removeItem("financeData");
localStorage.removeItem("spending");
const url = window.location.href;
window.open(url, "_blank");
};
// Function to update the table from local storage
function updateTableFromLocalStorage() {
const financeData = localStorage.getItem("financeData");
const dataArray = JSON.parse(financeData);
if (!dataArray) {
return;
}
if (dataArray.length > 0) {
const rowCount = financeTable.rows.length;
// Remove all rows except the first one (headers)
if (rowCount > 1) {
for (let i = rowCount - 1; i >= 1; i--) {
financeTable.deleteRow(i);
}
console.log("executed");
}
let financeCell3;
let totalVal = 0;
dataArray.forEach((data, index) => {
const financeRow = financeTable.insertRow();
financeRow.setAttribute("id", data.rowId);
const financeCell = financeRow.insertCell();
const financeCell2 = financeRow.insertCell(1);
financeCell3 = financeRow.insertCell(2);
const financeCell4 = financeRow.insertCell(3);
// Create a delete button
const delBtn = document.createElement("button");
delBtn.innerHTML = "X";
delBtn.id = "delete";
// Populate the table cells with data from local storage
financeCell.innerHTML = data.name;
financeCell2.innerHTML = data.date;
financeCell3.innerHTML = `${currency}${formatNumber(data.amount)}`;
financeCell4.appendChild(delBtn);
// Update totalVal
totalVal += parseInt(data.amount);
});
//Clearing the recent values
financeText.value = "";
financeAmount.value = "";
grandTotal.innerHTML = "";
financeMessage.remove();
//Bringing back the focus to the input
financeText.focus();
expenseWarning.innerHTML = "";
document.getElementById("clear").style.display = "block";
financeCell3.id = "itemAmount";
localStorage.setItem("spending", totalVal);
processAutoSave();
addDailySpending(totalVal);
grandTotal.classList.add("activeTotal");
grandTotal.innerHTML = `Spending Total: ${currency}${formatNumberWithAlpha(
totalVal
)}`;
// Attach a click event handler for delete buttons using event delegation
financeTable.addEventListener("click", function (event) {
if (event.target && event.target.id === "delete") {
const rowToDelete = event.target.closest("tr");
console.log(rowToDelete);
const rowId = event.target.closest("tr").id;
console.log(rowId);
rowToDelete.remove();
// Find the index of the data entry to delete
// Find the object in dataArray with the matching rowId
const objectToDelete = dataArray.find(
(item) => parseInt(item.rowId) === parseInt(rowId)
);
console.log(objectToDelete);
if (objectToDelete) {
// Remove the object from dataArray
const dataIndex = dataArray.indexOf(objectToDelete);
dataArray.splice(dataIndex, 1);
// Update localStorage and recalculate totalVal
localStorage.setItem("financeData", JSON.stringify(dataArray));
processAutoSave();
totalVal = dataArray.reduce(
(total, item) => total + parseInt(item.amount),
0
);
updateTableFromLocalStorage();
}
}
});
} else {
grandTotal.innerHTML = "";
totalVal = 0;
localStorage.setItem("spending", totalVal);
processAutoSave();
addDailySpending(totalVal);
}
}
//The output
const financeOutput = () => {
if (financeText.value.length === 0) {
financeText.focus();
expenseWarning.innerHTML = "* Name is required";
} else if (financeAmount.value.length === 0) {
financeAmount.focus();
} else {
// Function to save finance data to local storage
let rowIdCounter = localStorage.getItem("rowId") || 1; // Initialize the row ID counter
// Function to save finance data to local storage
async function saveToLocalStorage() {
const financeData = localStorage.getItem("financeData");
financeAmount.value = parseFloat(financeAmount.value.replace(/,/g, ""));
const newData = {
name: financeText.value,
date: financeDate.value,
amount: financeAmount.value,
rowId: rowIdCounter, // Assign the row ID to the new data
};
// Check if there's existing data in local storage
let dataArray = financeData ? JSON.parse(financeData) : [];
// Add the new data to the array
dataArray.push(newData);
// Store the updated data in local storage
localStorage.setItem("financeData", JSON.stringify(dataArray));
// Increment the row ID counter for the next row
rowIdCounter++;
localStorage.setItem("rowId", rowIdCounter);
processAutoSave();
}
saveToLocalStorage().then(updateTableFromLocalStorage());
}
};
financeAmount.addEventListener("keypress", function (e) {
if (e.key === "Enter") financeOutput();
});
financeText.addEventListener("keypress", function (e) {
if (e.key === "Enter") financeOutput();
}); // save the value if the enter key is pressed
financeBtn.addEventListener("click", financeOutput); //Invoke the output
var income = localStorage.getItem("income") || 0;
var spending = localStorage.getItem("spending") || 0;
var chart = null;
var ctx = document.getElementById("budgetChart").getContext("2d");
function updateBudget(trigger) {
if (trigger) {
localStorage.removeItem("income");
}
income =
localStorage.getItem("income") ||
parseFloat(prompt("How much do you earn last month?"));
document.getElementById(
"total-income"
).textContent = `${currency}${formatNumber(income)}`;
if (income) {
localStorage.setItem("income", income);
processAutoSave();
updateChart();
} else {
updateBudget(true);
}
}
function addDailySpending(amount) {
var dailySpending = parseFloat(amount);
spending = localStorage.getItem("spending") || dailySpending;
updateChart();
}
function updateChart() {
if (chart) {
chart.destroy(); // Clear the previous chart instance
}
var remainingBalance = income - spending;
var data = {
labels: ["Spending", "Remaining"],
datasets: [
{
data: [income - remainingBalance, remainingBalance],
backgroundColor: ["red", "green"],
},
],
};
chart = new Chart(ctx, {
type: "pie",
data: data,
});
}
document.getElementById("update").addEventListener("click", updateBudget);
// Format numbers with an alphabet representation
function formatNumberWithAlpha(number) {
number = Number(number);
// Check if the number is in billions, millions, or thousands range
if (number >= 1e9) {
return (number / 1e9).toFixed(1).replace(/\d(?=(\d{3})+\.)/g, "$&,") + "B";
} else if (number >= 1e6) {
return (number / 1e6).toFixed(1).replace(/\d(?=(\d{3})+\.)/g, "$&,") + "M";
} else if (number >= 1e3) {
return (number / 1e3).toFixed(1).replace(/\d(?=(\d{3})+\.)/g, "$&,") + "K";
} else {
return number.toFixed(0).replace(/\d(?=(\d{3})+\.)/g, "$&,");
}
}
// Format numbers to include the appropriate commas
function formatNumber(number) {
return parseInt(number).toLocaleString();
}
// Format input numbers with comma separation
window.formatInput = function (id) {
const inputElement = document.getElementById(id);
const inputValue = inputElement.value.replace(/,/g, "");
if (inputValue === "") {
// If the input is empty, do nothing
return;
}
// Parse the input value as a number
const numberValue = parseInt(inputValue);
if (!isNaN(numberValue)) {
inputElement.value = numberValue.toLocaleString();
}
};
const changeEmail = document.getElementById("change-email");
const emailField = document.getElementById("email");
let changeDetailsIsClicked = false;
changeEmail.addEventListener("click", editDetails);
emailField.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
editDetails();
}
});
function editDetails() {
const email = localStorage.getItem("email");
if (changeDetailsIsClicked) {
if (emailField.value !== email) {
localStorage.setItem("email", emailField.value);
alert("Your email has been updated");
} else {
alert("This is your current email.");
return;
}
emailField.style.display = "none";
changeEmail.textContent = "Edit Details";
changeDetailsIsClicked = false;
} else {
emailField.style.display = "block";
emailField.value = email;
changeEmail.textContent = "Save Details";
changeDetailsIsClicked = true;
}
}
export { updateBudget, updateTableFromLocalStorage };