-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin-dashboard.js
More file actions
481 lines (402 loc) · 14.3 KB
/
Copy pathadmin-dashboard.js
File metadata and controls
481 lines (402 loc) · 14.3 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
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// admin-dashboard.js - Admin Panel Logic
// Check authentication and admin access
DevHubAPI.checkAuth();
const currentUser = DevHubAPI.getCurrentUser();
// Check if user is admin (must have 'admin' in email)
if (!currentUser.email.includes('admin')) {
alert('Access denied. Admin privileges required.');
window.location.href = 'index.html';
}
// ==================== TAB NAVIGATION ====================
const tabs = document.querySelectorAll('.nav-links a');
const tabContents = document.querySelectorAll('.tab-content');
tabs.forEach(tab => {
tab.addEventListener('click', (e) => {
e.preventDefault();
// Remove active class from all tabs
tabs.forEach(t => t.classList.remove('active'));
tabContents.forEach(tc => tc.classList.remove('active'));
// Add active class to clicked tab
tab.classList.add('active');
const tabName = tab.getAttribute('data-tab');
document.getElementById(tabName).classList.add('active');
// Load data for the tab
loadTabData(tabName);
});
});
// ==================== LOAD DASHBOARD DATA ====================
async function loadDashboardStats() {
try {
const response = await fetch('http://localhost:5000/api/admin/dashboard', {
headers: {
'Authorization': `Bearer ${DevHubAPI.getToken()}`
}
});
const data = await response.json();
// Update stats
document.getElementById('totalUsers').textContent = data.totalUsers;
document.getElementById('totalProjects').textContent = data.totalProjects;
document.getElementById('totalRevenue').textContent = `$${data.totalRevenue}`;
document.getElementById('activeProjects').textContent = data.activeProjects;
document.getElementById('totalDevelopers').textContent = data.totalDevelopers;
document.getElementById('totalClients').textContent = data.totalClients;
document.getElementById('totalReviews').textContent = data.totalReviews;
document.getElementById('avgRating').textContent = `${data.avgRating}⭐`;
} catch (error) {
console.error('Error loading dashboard stats:', error);
alert('Failed to load dashboard statistics');
}
}
// ==================== LOAD USERS ====================
async function loadUsers(search = '') {
try {
const url = search
? `http://localhost:5000/api/admin/users?search=${encodeURIComponent(search)}`
: 'http://localhost:5000/api/admin/users';
const response = await fetch(url, {
headers: {
'Authorization': `Bearer ${DevHubAPI.getToken()}`
}
});
const data = await response.json();
const tbody = document.getElementById('usersTable');
tbody.innerHTML = '';
if (data.users.length === 0) {
tbody.innerHTML = '<tr><td colspan="6" style="text-align: center;">No users found</td></tr>';
return;
}
data.users.forEach(user => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${user.id}</td>
<td>${user.full_name}</td>
<td>${user.email}</td>
<td><span class="badge ${user.user_type.toLowerCase()}">${user.user_type}</span></td>
<td>${DevHubAPI.formatDate(user.created_at)}</td>
<td>
<button class="action-btn suspend" onclick="suspendUser(${user.id}, ${user.suspended || false})">
${user.suspended ? 'Activate' : 'Suspend'}
</button>
<button class="action-btn" onclick="deleteUser(${user.id})">Delete</button>
</td>
`;
tbody.appendChild(row);
});
} catch (error) {
console.error('Error loading users:', error);
alert('Failed to load users');
}
}
// ==================== USER ACTIONS ====================
async function deleteUser(userId) {
if (!confirm('Are you sure you want to delete this user? This action cannot be undone.')) {
return;
}
try {
const response = await fetch(`http://localhost:5000/api/admin/users/${userId}`, {
method: 'DELETE',
headers: {
'Authorization': `Bearer ${DevHubAPI.getToken()}`
}
});
if (response.ok) {
alert('User deleted successfully');
loadUsers();
loadDashboardStats();
} else {
alert('Failed to delete user');
}
} catch (error) {
console.error('Error deleting user:', error);
alert('Error deleting user');
}
}
async function suspendUser(userId, currentStatus) {
const action = currentStatus ? 'activate' : 'suspend';
if (!confirm(`Are you sure you want to ${action} this user?`)) {
return;
}
try {
const response = await fetch(`http://localhost:5000/api/admin/users/${userId}/suspend`, {
method: 'PATCH',
headers: {
'Authorization': `Bearer ${DevHubAPI.getToken()}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({ suspended: !currentStatus })
});
if (response.ok) {
alert(`User ${action}d successfully`);
loadUsers();
} else {
alert(`Failed to ${action} user`);
}
} catch (error) {
console.error(`Error ${action}ing user:`, error);
alert(`Error ${action}ing user`);
}
}
function searchUsers() {
const search = document.getElementById('userSearch').value;
loadUsers(search);
}
// ==================== LOAD PROJECTS ====================
async function loadProjects(search = '') {
try {
const url = search
? `http://localhost:5000/api/admin/projects?search=${encodeURIComponent(search)}`
: 'http://localhost:5000/api/admin/projects';
const response = await fetch(url, {
headers: {
'Authorization': `Bearer ${DevHubAPI.getToken()}`
}
});
const data = await response.json();
const tbody = document.getElementById('projectsTable');
tbody.innerHTML = '';
if (data.projects.length === 0) {
tbody.innerHTML = '<tr><td colspan="7" style="text-align: center;">No projects found</td></tr>';
return;
}
data.projects.forEach(project => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${project.id}</td>
<td>${project.title}</td>
<td>${project.client_name || 'N/A'}</td>
<td>${project.developer_name || 'Unassigned'}</td>
<td>$${project.budget || 0}</td>
<td><span class="badge ${project.status}">${project.status}</span></td>
<td>
<button class="action-btn view" onclick="viewProject(${project.id})">View</button>
<button class="action-btn" onclick="deleteProject(${project.id})">Delete</button>
</td>
`;
tbody.appendChild(row);
});
} catch (error) {
console.error('Error loading projects:', error);
alert('Failed to load projects');
}
}
// ==================== PROJECT ACTIONS ====================
async function deleteProject(projectId) {
if (!confirm('Are you sure you want to delete this project?')) {
return;
}
try {
const response = await fetch(`http://localhost:5000/api/admin/projects/${projectId}`, {
method: 'DELETE',
headers: {
'Authorization': `Bearer ${DevHubAPI.getToken()}`
}
});
if (response.ok) {
alert('Project deleted successfully');
loadProjects();
loadDashboardStats();
} else {
alert('Failed to delete project');
}
} catch (error) {
console.error('Error deleting project:', error);
alert('Error deleting project');
}
}
function viewProject(projectId) {
alert(`View project details (ID: ${projectId})\nThis will open a detail modal in production.`);
}
function searchProjects() {
const search = document.getElementById('projectSearch').value;
loadProjects(search);
}
// ==================== LOAD REVIEWS ====================
async function loadReviews() {
try {
const response = await fetch('http://localhost:5000/api/admin/reviews', {
headers: {
'Authorization': `Bearer ${DevHubAPI.getToken()}`
}
});
const data = await response.json();
const tbody = document.getElementById('reviewsTable');
tbody.innerHTML = '';
if (data.reviews.length === 0) {
tbody.innerHTML = '<tr><td colspan="7" style="text-align: center;">No reviews found</td></tr>';
return;
}
data.reviews.forEach(review => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${review.id}</td>
<td>${review.developer_name}</td>
<td>${review.client_name}</td>
<td>${'⭐'.repeat(review.rating)}</td>
<td>${review.message.substring(0, 50)}${review.message.length > 50 ? '...' : ''}</td>
<td>${DevHubAPI.formatDate(review.created_at)}</td>
<td>
<button class="action-btn" onclick="deleteReview(${review.id})">Delete</button>
</td>
`;
tbody.appendChild(row);
});
} catch (error) {
console.error('Error loading reviews:', error);
alert('Failed to load reviews');
}
}
// ==================== REVIEW ACTIONS ====================
async function deleteReview(reviewId) {
if (!confirm('Are you sure you want to delete this review?')) {
return;
}
try {
const response = await fetch(`http://localhost:5000/api/admin/reviews/${reviewId}`, {
method: 'DELETE',
headers: {
'Authorization': `Bearer ${DevHubAPI.getToken()}`
}
});
if (response.ok) {
alert('Review deleted successfully');
loadReviews();
loadDashboardStats();
} else {
alert('Failed to delete review');
}
} catch (error) {
console.error('Error deleting review:', error);
alert('Error deleting review');
}
}
// ==================== LOAD ANALYTICS ====================
async function loadAnalytics() {
try {
const response = await fetch('http://localhost:5000/api/admin/analytics?period=30', {
headers: {
'Authorization': `Bearer ${DevHubAPI.getToken()}`
}
});
const data = await response.json();
// Load top developers
const topDevsTable = document.getElementById('topDevsTable');
topDevsTable.innerHTML = '';
if (data.topDevelopers.length === 0) {
topDevsTable.innerHTML = '<tr><td colspan="5" style="text-align: center;">No data</td></tr>';
} else {
data.topDevelopers.forEach(dev => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${dev.username}</td>
<td>${dev.full_name}</td>
<td>${parseFloat(dev.rating).toFixed(1)}⭐</td>
<td>${dev.total_reviews}</td>
<td>${dev.total_projects}</td>
`;
topDevsTable.appendChild(row);
});
}
// Load top clients
const topClientsTable = document.getElementById('topClientsTable');
topClientsTable.innerHTML = '';
if (data.topClients.length === 0) {
topClientsTable.innerHTML = '<tr><td colspan="4" style="text-align: center;">No data</td></tr>';
} else {
data.topClients.forEach(client => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${client.full_name}</td>
<td>${client.email}</td>
<td>${client.total_projects || 0}</td>
<td>$${parseFloat(client.total_spent || 0).toFixed(2)}</td>
`;
topClientsTable.appendChild(row);
});
}
} catch (error) {
console.error('Error loading analytics:', error);
alert('Failed to load analytics');
}
}
// ==================== SETTINGS ====================
async function loadSettings() {
try {
const response = await fetch('http://localhost:5000/api/admin/settings', {
headers: {
'Authorization': `Bearer ${DevHubAPI.getToken()}`
}
});
const data = await response.json();
document.getElementById('platformFee').value = data.platform_fee || '';
document.getElementById('minBudget').value = data.min_project_budget || '';
document.getElementById('maxBudget').value = data.max_project_budget || '';
} catch (error) {
console.error('Error loading settings:', error);
}
}
document.getElementById('settingsForm').addEventListener('submit', async (e) => {
e.preventDefault();
const settings = {
platformFee: document.getElementById('platformFee').value,
minProjectBudget: document.getElementById('minBudget').value,
maxProjectBudget: document.getElementById('maxBudget').value,
};
try {
const response = await fetch('http://localhost:5000/api/admin/settings', {
method: 'PUT',
headers: {
'Authorization': `Bearer ${DevHubAPI.getToken()}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(settings)
});
if (response.ok) {
alert('Settings updated successfully');
} else {
alert('Failed to update settings');
}
} catch (error) {
console.error('Error updating settings:', error);
alert('Error updating settings');
}
});
// ==================== TAB DATA LOADER ====================
function loadTabData(tabName) {
switch(tabName) {
case 'overview':
loadDashboardStats();
break;
case 'users':
loadUsers();
break;
case 'projects':
loadProjects();
break;
case 'reviews':
loadReviews();
break;
case 'analytics':
loadAnalytics();
break;
case 'settings':
loadSettings();
break;
}
}
// ==================== LOGOUT ====================
document.getElementById('logoutBtn').addEventListener('click', () => {
if (confirm('Are you sure you want to logout?')) {
DevHubAPI.Auth.logout();
}
});
// ==================== UPDATE ADMIN INFO ====================
function updateAdminInfo() {
document.getElementById('adminName').textContent = currentUser.fullName;
}
// ==================== INITIALIZE ====================
document.addEventListener('DOMContentLoaded', () => {
updateAdminInfo();
loadDashboardStats();
// Auto-refresh stats every 60 seconds
setInterval(loadDashboardStats, 60000);
});