-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanageaccount.html
241 lines (213 loc) · 8.42 KB
/
manageaccount.html
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
<!DOCTYPE html>
<html>
<head>
<title>Account Data</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<nav class="navbar">
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a href="adminhome.html">Home</a>
<a href="createaccount.html">Create User Account</a>
<a href="manageaccount.html">User Account Management</a>
<a href="itemsales.html">Item Sales Performance</a>
<a href="salesperformance.html">Salesman Sales Performance</a>
<a href="updatepassword.html">Update Password</a>
<a href="userhome.html">Switch to User Panel</a>
</div>
<span class="open" onclick="openNav()">  ☰  
<div class="logo">
<a href="">
<img src="assets/sale-sign.png" alt="SDS_logo">
<span>  SALES PILOT  </span>
<img src="assets/cashier-machine.png" alt="SDS_logo">
</a>
</div>
</span>
<a class="logout" href="#" onclick="logout()">Log Out  </a>
</nav>
<form id="searchForm">
<table class="search-func">
<caption>User Account Management</caption>
<tr>
<td>
</td>
<td>
<form id="searchForm">
<input type="text" id="accountIdInput" placeholder="Enter Account ID">
<button class="search-acc" type="submit">Search</button>
</form>
</td>
<td></td>
</tr>
</table>
</form>
<table id="accountTable">
<thead>
<tr class="acc-title">
<th style="width: 15%;">Account ID</th>
<th style="width: 40%">Name</th>
<th style="width: 20%">Email</th>
<th style="width: 12%">Status</th>
<th style="width: 13%">Action</th>
</tr>
</thead>
<tbody></tbody>
</table>
<script>
// Open Side Navigation Bar
function openNav() {
document.getElementById("mySidenav").style.width = "280px";
}
// Close Side Navigation Bar
function closeNav() {
document.getElementById("mySidenav").style.width = "0";
}
// Logout and clear data from local storage
function logout() {
localStorage.removeItem("token");
window.location.href = "login.html";
}
const token = localStorage.getItem("token");
if (!token) {
window.location.href = "login.html";
}
window.addEventListener('DOMContentLoaded', (event) => {
const apiUrl = 'http://localhost:3000/api/account/';
const searchForm = document.getElementById('searchForm');
const accountIdInput = document.getElementById('accountIdInput');
const table = document.getElementById('accountTable');
searchForm.addEventListener('submit', (event) => {
event.preventDefault();
const accountId = accountIdInput.value.trim();
if (accountId !== '') {
clearTableRows();
const searchUrl = apiUrl + accountId;
makeRequest(searchUrl);
} else {
makeRequest(apiUrl); // Load all data when the search input is empty
location.reload();
}
});
function makeRequest(url) {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status === 200) {
const data = JSON.parse(this.responseText);
updateTable(data);
} else if (this.status === 404){
clearTableRows();
displayErrorMessage();
} else{
clearTableRows();
displayNoResultsMessage();
}
}
};
xhr.open('GET', url, true);
xhr.setRequestHeader('Authorization', `Bearer ${token}`); // Add authorization header
xhr.send();
}
function displayNoResultsMessage() {
alert('No search results found.');
location.reload();
}
function displayErrorMessage() {
alert('An error occurred while fetching data.');
location.reload();
}
function clearTableRows() {
const rows = table.getElementsByTagName('tr');
while (rows.length > 1) {
table.deleteRow(1);
}
}
function updateTable(data) {
if (data || data.length) {
if (data.length) {
data.forEach((row) => {
const newRow = table.insertRow();
//newRow.insertCell().textContent = row.account_id;
const AccID = newRow.insertCell();
AccID.textContent = row.account_id;
AccID.classList.add('acc-id');
newRow.insertCell().textContent = row.name;
//newRow.insertCell().textContent = row.email;
const emailCol = newRow.insertCell();
emailCol.textContent = row.email;
emailCol.classList.add('email');
//newRow.insertCell().textContent = row.status;
const statusCol = newRow.insertCell();
statusCol.textContent = row.status;
statusCol.classList.add('status');
const updateButton = document.createElement('button');
updateButton.textContent = 'Update';
updateButton.classList.add('update-button'); // (For CSS purpose) Add class to the update button
updateButton.addEventListener('click', () => {
const accountId = row.account_id;
const status = row.status === 0 ? 1 : 0;
updateAccount(accountId, status);
});
//newRow.insertCell().appendChild(updateButton);
const updateBtn = newRow.insertCell();
updateBtn.appendChild(updateButton);
});
} else {
const newRow = table.insertRow();
//newRow.insertCell().textContent = data.account_id;
const AccID = newRow.insertCell();
AccID.textContent = data.account_id;
AccID.classList.add('acc-id');
newRow.insertCell().textContent = data.name;
//newRow.insertCell().textContent = data.email;
const emailCol = newRow.insertCell();
emailCol.textContent = data.email;
emailCol.classList.add('email');
//newRow.insertCell().textContent = data.status;
const statusCol = newRow.insertCell();
statusCol.textContent = data.status;
statusCol.classList.add('status');
const updateButton = document.createElement('button');
updateButton.textContent = 'Update';
updateButton.classList.add('update-button'); // (For CSS purpose) Add class to the update button
updateButton.addEventListener('click', () => {
const accountId = data.account_id;
const status = data.status === 0 ? 1 : 0;
updateAccount(accountId, status);
});
//newRow.insertCell().appendChild(updateButton);
const updateBtn = newRow.insertCell();
updateBtn.appendChild(updateButton);
}
}
}
function updateAccount(id, status) {
const apiUrl = `http://localhost:3000/api/account/${id}`;
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status === 200) {
const updatedAccount = JSON.parse(this.responseText);
console.log(updatedAccount); // Do something with the updated account data
location.reload(); // Refresh the page after the update
} else {
const error = JSON.parse(this.responseText);
console.error(error); // Handle the error response
}
}
};
xhr.open('PUT', apiUrl, true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.setRequestHeader('Authorization', `Bearer ${token}`); // Add authorization header
xhr.send(JSON.stringify({ status }));
// Update the status in the table immediately
const statusCell = newRow.cells[3];
statusCell.textContent = status;
}
makeRequest(apiUrl);
});
</script>
</body>
</html>