forked from neeraj542/Personal-Finance-Tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
304 lines (243 loc) · 10 KB
/
script.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
// const arrow = document.querySelector('.arrow');
// arrow.addEventListener('click', (event) => {
// event.preventDefault();
// const targetElement = document.querySelector(event.target.getAttribute('#tracker'));
// targetElement.scrollIntoView({ behavior: 'smooth' });
// });
// Get the table-part element and the transaction-table
const tablePart = document.querySelector('.table-part');
const transactionTable = document.getElementById('transaction-table');
// Function to check the number of entries and apply scrollbar if needed
function checkTableScroll() {
const rowCount = transactionTable.rows.length - 1; // Exclude the header row
const maxRowCount = 4; // Set the desired maximum number of entries
if (rowCount > maxRowCount) {
tablePart.classList.add('scrollable');
} else {
tablePart.classList.remove('scrollable');
}
}
// Call the function initially and whenever there is a change in the table
checkTableScroll();
// Add an event listener for changes in the table
const observer = new MutationObserver(checkTableScroll);
observer.observe(transactionTable, {
childList: true,
subtree: true,
});
// Initialize an empty array to store the transactions
let transactions = [];
// Variable to store the current transaction being edited
let editedTransaction = null;
// Function to add a new transaction
function addTransaction() {
const descriptionInput = document.getElementById('description');
const amountInput = document.getElementById('amount');
const typeInput = document.getElementById('type');
const dateInput = document.getElementById('date');
const description = descriptionInput.value;
const amount = parseFloat(amountInput.value);
const type = typeInput.value;
const chosenDate = new Date(dateInput.value);
// Clear the input fields
descriptionInput.value = '';
amountInput.value = '';
dateInput.value = '';
// Validate the input
if (description.trim() === '' || isNaN(amount) || isNaN(chosenDate)) {
return;
}
// Create a new transaction object
const transaction = {
primeId: chosenDate.getTime(),
description: description,
amount: amount,
type: type
};
// Add the transaction to the array
transactions.push(transaction);
// Update the balance
updateBalance();
// Update the transaction table
updateTransactionTable();
}
// Function to delete a transaction
function deleteTransaction(primeId) {
// Find the index of the transaction with the given primeId
const index = transactions.findIndex(transaction => transaction.primeId === primeId);
// Remove the transaction from the array
if (index > -1) {
transactions.splice(index, 1);
}
// Update the balance
updateBalance();
// Update the transaction table
updateTransactionTable();
}
// Function to edit a transaction
function editTransaction(primeId) {
// Find the transaction with the given primeId
const transaction = transactions.find(transaction => transaction.primeId === primeId);
// Populate the input fields with the transaction details for editing
document.getElementById('description').value = transaction.description;
document.getElementById('amount').value = transaction.amount;
document.getElementById('type').value = transaction.type;
// Store the current transaction being edited
editedTransaction = transaction;
// Show the Save button and hide the Add Transaction button
document.getElementById('add-transaction-btn').style.display = 'none';
document.getElementById('save-transaction-btn').style.display = 'inline-block';
// Set the date input value to the chosen date
const dateInput = document.getElementById('date');
const chosenDate = new Date(transaction.primeId);
const formattedDate = formatDate(chosenDate);
dateInput.value = formattedDate;
}
// Function to save the edited transaction
function saveTransaction() {
if (!editedTransaction) {
return;
}
const descriptionInput = document.getElementById('description');
const amountInput = document.getElementById('amount');
const typeInput = document.getElementById('type');
const dateInput = document.getElementById('date');
const description = descriptionInput.value;
const amount = parseFloat(amountInput.value);
const type = typeInput.value;
const chosenDate = new Date(dateInput.value);
// Validate the input
if (description.trim() === '' || isNaN(amount) || isNaN(chosenDate)) {
return;
}
// Update the transaction details
editedTransaction.description = description;
editedTransaction.amount = amount;
editedTransaction.type = type;
editedTransaction.primeId = chosenDate.getTime();
// Clear the input fields
descriptionInput.value = '';
amountInput.value = '';
dateInput.value = '';
// Clear the edited transaction
editedTransaction = null;
// Update the balance
updateBalance();
// Update the transaction table
updateTransactionTable();
// Show the Add Transaction button and hide the Save button
document.getElementById('add-transaction-btn').style.display = 'inline-block';
document.getElementById('save-transaction-btn').style.display = 'none';
}
// Function to update the balance
function updateBalance() {
const balanceElement = document.getElementById('balance');
let balance = 0.00;
// Calculate the total balance
transactions.forEach(transaction => {
if (transaction.type === 'income') {
balance += transaction.amount;
} else if (transaction.type === 'expense') {
balance -= transaction.amount;
}
});
// Format the balance with currency symbol
const currencySelect = document.getElementById('currency');
const currencyCode = currencySelect.value;
const formattedBalance = formatCurrency(balance, currencyCode);
// Update the balance display
balanceElement.textContent = formattedBalance;
// Check if the balance is negative or positive
if (balance < 0) {
balanceElement.classList.remove('positive-balance');
balanceElement.classList.add('negative-balance');
} else {
balanceElement.classList.remove('negative-balance');
balanceElement.classList.add('positive-balance');
}
}
window.addEventListener('DOMContentLoaded', () => {
// Set initial balance value
let balance = 0.00;
updateBalance(balance); // Update the balance display
// Other code for adding transactions, updating balance, etc.
// Function to update the balance display
function updateBalance(balance) {
const balanceElement = document.getElementById('balance');
balanceElement.textContent = balance.toFixed(2); // Format balance with 2 decimal places
}
});
// Function to format currency based on the selected currency code
function formatCurrency(amount, currencyCode) {
// Define currency symbols and decimal separators for different currency codes
const currencySymbols = {
USD: '$',
EUR: '€',
INR: '₹'
};
const decimalSeparators = {
USD: '.',
EUR: ',',
INR: '.'
};
// Get the currency symbol and decimal separator based on the currency code
const symbol = currencySymbols[currencyCode] || '';
const decimalSeparator = decimalSeparators[currencyCode] || '.';
// Format the amount with currency symbol and decimal separator
const formattedAmount = symbol + amount.toFixed(2).replace('.', decimalSeparator);
return formattedAmount;
}
// Function to format date as DD/MM/YYYY
function formatDate(date) {
const day = String(date.getDate()).padStart(2, '0');
const month = String(date.getMonth() + 1).padStart(2, '0');
const year = date.getFullYear();
return `${day}/${month}/${year}`;
}
// Function to update the transaction table
function updateTransactionTable() {
const transactionTable = document.getElementById('transaction-table');
// Clear the existing table rows
while (transactionTable.rows.length > 1) {
transactionTable.deleteRow(1);
}
// Add new rows to the table
transactions.forEach(transaction => {
const newRow = transactionTable.insertRow();
const dateCell = newRow.insertCell();
const date = new Date(transaction.primeId);
dateCell.textContent = formatDate(date);
const descriptionCell = newRow.insertCell();
descriptionCell.textContent = transaction.description;
const amountCell = newRow.insertCell();
const currencySelect = document.getElementById('currency');
const currencyCode = currencySelect.value;
const formattedAmount = formatCurrency(transaction.amount, currencyCode);
amountCell.textContent = formattedAmount;
const typeCell = newRow.insertCell();
typeCell.textContent = transaction.type;
const actionCell = newRow.insertCell();
const editButton = document.createElement('button');
editButton.textContent = 'Edit';
editButton.classList.add('edit-button');
editButton.addEventListener('click', () => editTransaction(transaction.primeId));
actionCell.appendChild(editButton);
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
deleteButton.classList.add('delete-button');
deleteButton.addEventListener('click', () => deleteTransaction(transaction.primeId));
actionCell.appendChild(deleteButton);
const saveButton = document.createElement('button');
saveButton.textContent = 'Save';
saveButton.classList.add('save-button');
saveButton.addEventListener('click', () => saveTransaction(transaction.primeId));
actionCell.appendChild(saveButton);
});
}
// Event listener for the Add Transaction button
document.getElementById('add-transaction-btn').addEventListener('click', addTransaction);
// Event listener for the Save Transaction button
document.getElementById('save-transaction-btn').addEventListener('click', saveTransaction);
// Initial update of the balance and transaction table
updateBalance();
updateTransactionTable();