-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransaction.html
More file actions
229 lines (209 loc) · 8.31 KB
/
transaction.html
File metadata and controls
229 lines (209 loc) · 8.31 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Transactions</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
}
h1, h2 {
color: #333;
}
form {
background: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
margin-bottom: 20px;
width: 300px;
}
select, input[type="date"], input[type="text"], input[type="number"] {
width: 100%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
button {
background-color: #28a745;
color: white;
padding: 10px;
border: none;
border-radius: 4px;
cursor: pointer;
width: 100%;
margin: 5px 0;
}
button:hover {
background-color: #218838;
}
.dropdown {
display: none;
margin: 10px 0;
}
.dropdown-option {
padding: 10px;
background-color: #f9f9f9;
border: 1px solid #ccc;
cursor: pointer;
margin: 2px 0;
}
.dropdown-option:hover {
background-color: #ddd;
}
</style>
</head>
<body>
<h1>Manage Transactions</h1>
<div>
<button id="income-button">Income</button>
<button id="expense-button">Expense</button>
<button id="savings-button">Savings</button>
</div>
<div class="dropdown" id="expense-dropdown">
<div class="dropdown-option" data-category="House Rent">House Rent</div>
<div class="dropdown-option" data-category="Fuel Cost">Fuel Cost</div>
<div class="dropdown-option" data-category="Public Transport Fee">Public Transport Fee</div>
<div class="dropdown-option" data-category="Travel Expenses">Travel Expenses</div>
<div class="dropdown-option" data-category="Clothing">Clothing</div>
<div class="dropdown-option" data-category="Medical">Medical</div>
<div class="dropdown-option" data-category="Grocery Shopping">Grocery Shopping</div>
<div class="dropdown-option" data-category="College Fees">College Fees</div>
</div>
<form id="transaction-form">
<input type="date" id="transaction-date" required>
<input type="text" id="transaction-category" placeholder="Category" required>
<input type="number" id="transaction-amount" placeholder="Amount" required>
<input type="text" id="transaction-description" placeholder="Description">
<button type="submit">Add Transaction</button>
</form>
<h2>Transaction List</h2>
<ul id="transaction-list"></ul>
<script>
const incomeButton = document.getElementById('income-button');
const expenseButton = document.getElementById('expense-button');
const savingsButton = document.getElementById('savings-button');
const expenseDropdown = document.getElementById('expense-dropdown');
const categoryInput = document.getElementById('transaction-category');
incomeButton.addEventListener('click', () => {
categoryInput.value = 'Income';
expenseDropdown.style.display = 'none';
});
savingsButton.addEventListener('click', () => {
categoryInput.value = 'Savings';
expenseDropdown.style.display = 'none';
});
expenseButton.addEventListener('click', () => {
expenseDropdown.style.display = 'block';
categoryInput.value = ''; // Clear the category input when Expense is clicked
});
document.querySelectorAll('.dropdown-option').forEach(option => {
option.addEventListener('click', () => {
categoryInput.value = option.getAttribute('data-category');
expenseDropdown.style.display = 'none';
});
});
document.getElementById('transaction-form').addEventListener('submit', async (e) => {
e.preventDefault();
const date = document.getElementById('transaction-date').value;
const category = categoryInput.value;
const amount = document.getElementById('transaction-amount').value;
const description = document.getElementById('transaction-description').value;
// Determine the transaction type based on the button clicked
let type;
if (category === 'Income') {
type = 'Income';
} else if (category === 'Savings') {
type = 'Savings';
} else {
type = 'Expense';
}
try {
const response = await fetch('http://localhost:5500/api/transactions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${localStorage.getItem('token')}`
},
body: JSON.stringify({ date, type, amount, category, description })
});
if (response.ok) {
alert('Transaction added successfully');
loadTransactions(); // Refresh the transaction list
} else {
const errorData = await response.json();
console.error('Error response:', errorData);
alert('Error: ' + errorData.message);
}
} catch (error) {
console.error('Fetch error:', error);
alert('Error: ' + error.message);
}
});
async function loadTransactions() {
const response = await fetch('http://localhost:5500/api/transactions', {
headers: {
'Authorization': `Bearer ${localStorage.getItem('token')}`
}
});
if (response.ok) {
const transactions = await response.json();
const list = document.getElementById('transaction-list');
list.innerHTML = '';
transactions.forEach(transaction => {
const listItem = document.createElement('li');
listItem.textContent = `${transaction.date} - ${transaction.category} - ${transaction.amount} (${transaction.description})`;
list.appendChild(listItem);
});
} else {
const errorData = await response.json();
alert('Error: ' + errorData.message);
}
}
// Load transactions on page load
loadTransactions();
async function fetchAndDisplayTotals() {
try {
const [savingsResponse, incomeResponse, expensesResponse] = await Promise.all([
fetch('http://localhost:5500/api/transactions/total-savings', {
headers: {
'Authorization': `Bearer ${localStorage.getItem('token')}`
}
}),
fetch('http://localhost:5500/api/transactions/total-income', {
headers: {
'Authorization': `Bearer ${localStorage.getItem('token')}`
}
}),
fetch('http://localhost:5500/api/transactions/total-expenses', {
headers: {
'Authorization': `Bearer ${localStorage.getItem('token')}`
}
})
]);
const savingsData = await savingsResponse.json();
const incomeData = await incomeResponse.json();
const expensesData = await expensesResponse.json();
document.getElementById('total-savings').textContent = `Total Savings: $${savingsData.totalSavings}`;
document.getElementById('total-income').textContent = `Total Income: $${incomeData.totalIncome}`;
document.getElementById('total-expenses').textContent = `Total Expenses: $${expensesData.totalExpenses}`;
} catch (error) {
console.error('Error fetching totals:', error);
}
}
// Call the function to fetch and display totals
fetchAndDisplayTotals();
</script>
</body>
</html>