-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient-dashboard.js
More file actions
304 lines (248 loc) · 10.3 KB
/
Copy pathclient-dashboard.js
File metadata and controls
304 lines (248 loc) · 10.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
// client-dashboard.js - Client Dashboard Logic
// Check authentication
DevHubAPI.checkAuth();
const currentUser = DevHubAPI.getCurrentUser();
// Verify user is a client
if (currentUser.userType !== 'Client') {
alert('Access denied. Client account required.');
window.location.href = 'index.html';
}
// ==================== LOAD DASHBOARD DATA ====================
async function loadClientDashboard() {
try {
// Load stats
await loadClientStats();
// Load projects
await loadClientProjects();
// Load messages
await loadRecentMessages();
// Load recommended developers
await loadRecommendedDevelopers();
} catch (error) {
console.error('Error loading dashboard:', error);
showError('Failed to load dashboard data');
}
}
// ==================== LOAD CLIENT STATS ====================
async function loadClientStats() {
try {
const stats = await DevHubAPI.Stats.getClientStats(currentUser.id);
// Update stats cards
document.querySelector('.stats .card:nth-child(1) h3').textContent = stats.activeProjects;
document.querySelector('.stats .card:nth-child(2) h3').textContent = `$${stats.totalSpent}`;
document.querySelector('.stats .card:nth-child(3) h3').textContent = stats.developersHired;
document.querySelector('.stats .card:nth-child(4) h3').textContent = `${stats.avgRating}⭐`;
} catch (error) {
console.error('Error loading stats:', error);
}
}
// ==================== LOAD CLIENT PROJECTS ====================
async function loadClientProjects() {
try {
const response = await DevHubAPI.Project.getClientProjects(currentUser.id);
const projects = response.projects || response;
const projectList = document.querySelector('.project-list');
projectList.innerHTML = '';
if (projects.length === 0) {
projectList.innerHTML = '<p style="padding: 20px; text-align: center;">No projects yet</p>';
return;
}
// Show only recent 5 projects
projects.slice(0, 5).forEach(project => {
const statusClass = project.status.replace('-', '');
const statusText = project.status.charAt(0).toUpperCase() + project.status.slice(1).replace('-', ' ');
const projectItem = document.createElement('div');
projectItem.className = 'project-item';
projectItem.innerHTML = `
<span>${project.title}</span>
<span class="project-status ${statusClass}">${statusText}</span>
`;
projectList.appendChild(projectItem);
});
} catch (error) {
console.error('Error loading projects:', error);
}
}
// ==================== LOAD RECENT MESSAGES ====================
async function loadRecentMessages() {
try {
const response = await DevHubAPI.Message.getConversations();
const conversations = response.conversations || response;
const messageList = document.querySelector('.message-list');
messageList.innerHTML = '';
if (conversations.length === 0) {
messageList.innerHTML = '<p style="padding: 20px; text-align: center;">No messages yet</p>';
return;
}
// Show only recent 3 conversations
conversations.slice(0, 3).forEach(conv => {
const messageDiv = document.createElement('div');
messageDiv.className = 'message';
messageDiv.innerHTML = `
<span><strong>${conv.other_user_name}:</strong> ${conv.last_message.substring(0, 50)}${conv.last_message.length > 50 ? '...' : ''}</span>
<small>${DevHubAPI.timeAgo(conv.last_message_time)}</small>
`;
messageList.appendChild(messageDiv);
});
} catch (error) {
console.error('Error loading messages:', error);
}
}
// ==================== LOAD RECOMMENDED DEVELOPERS ====================
async function loadRecommendedDevelopers() {
try {
const response = await DevHubAPI.Developer.getAll({ rating: 4, limit: 3 });
const developers = response.developers || response;
const devList = document.querySelector('.dev-list');
if (!devList) return;
devList.innerHTML = '';
if (developers.length === 0) {
devList.innerHTML = '<p style="text-align: center;">No developers available</p>';
return;
}
developers.forEach(dev => {
const devCard = document.createElement('div');
devCard.className = 'dev-card';
devCard.innerHTML = `
<div class="dev-info">
<img src="https://ui-avatars.com/api/?name=${encodeURIComponent(dev.full_name)}&background=007bff&color=fff" alt="${dev.full_name}" />
<div class="dev-details">
<h4>${dev.full_name}</h4>
<p>${dev.skills || 'Full Stack Developer'}</p>
<p>⭐ ${parseFloat(dev.rating).toFixed(1)} (${dev.total_reviews} reviews)</p>
</div>
</div>
<div class="dev-buttons">
<a href="developer-profile.html?id=${dev.id}" class="btn-outline">View Profile</a>
<a href="javascript:void(0)" onclick="messageDevelope(${dev.user_id})" class="btn-blue">Message</a>
</div>
`;
devList.appendChild(devCard);
});
} catch (error) {
console.error('Error loading developers:', error);
}
}
// ==================== MESSAGE DEVELOPER ====================
function messageDeveloper(userId) {
// Store developer ID and redirect to messages page
localStorage.setItem('message_to_user', userId);
window.location.href = 'messages.html';
}
// ==================== UPDATE USER INFO ====================
function updateUserInfo() {
const userName = document.querySelector('.dashboard-header h2');
if (userName) {
userName.textContent = `Welcome back, ${currentUser.fullName.split(' ')[0]} 👋`;
}
const userEmail = document.querySelector('.user-info span');
if (userEmail) {
userEmail.textContent = `@${currentUser.email.split('@')[0]}`;
}
const userAvatar = document.querySelector('.user-info img');
if (userAvatar) {
userAvatar.src = `https://ui-avatars.com/api/?name=${encodeURIComponent(currentUser.fullName)}&background=007bff&color=fff`;
}
}
// ==================== INITIALIZE DASHBOARD ====================
document.addEventListener('DOMContentLoaded', () => {
updateUserInfo();
loadClientDashboard();
// Make navigation links interactive
setupNavigation();
// Logout button
document.querySelector('.logout button')?.addEventListener('click', () => {
if (confirm('Are you sure you want to logout?')) {
DevHubAPI.Auth.logout();
}
});
// Refresh data every 30 seconds
setInterval(() => {
loadClientStats();
loadRecentMessages();
}, 30000);
});
// ==================== SETUP NAVIGATION ====================
function setupNavigation() {
const navLinks = document.querySelectorAll('.nav-links a');
navLinks.forEach((link) => {
link.addEventListener('click', (e) => {
e.preventDefault();
const text = link.textContent.trim();
if (text.includes('Dashboard')) {
// Already on dashboard
window.location.reload();
} else if (text.includes('My Projects')) {
showProjectsModal();
} else if (text.includes('Messages')) {
alert('Messages page coming soon! Recent messages are shown in the dashboard.');
} else if (text.includes('Developers')) {
window.location.href = 'find-developers.html';
} else if (text.includes('Settings')) {
alert('Settings page coming soon!');
}
});
});
}
// ==================== SHOW PROJECTS MODAL ====================
async function showProjectsModal() {
try {
const response = await DevHubAPI.Project.getClientProjects(currentUser.id);
const projects = response.projects || response;
const modal = document.createElement('div');
modal.style.cssText = `
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.7);
display: flex;
justify-content: center;
align-items: center;
z-index: 9999;
overflow-y: auto;
`;
let projectsHTML = '';
if (projects.length === 0) {
projectsHTML = '<p style="text-align: center; color: #666;">No projects yet</p>';
} else {
projectsHTML = projects.map(p => `
<div style="border-bottom: 1px solid #eee; padding: 15px 0;">
<div style="display: flex; justify-content: space-between; align-items: center;">
<div>
<strong style="font-size: 1.1rem;">${p.title}</strong>
<p style="color: #666; margin: 5px 0;">${p.description || 'No description'}</p>
<small style="color: #999;">Budget: ${p.budget || 'N/A'}</small>
</div>
<span style="padding: 4px 12px; border-radius: 20px; font-size: 0.85rem; ${
p.status === 'completed' ? 'background: #d4edda; color: #155724;' :
p.status === 'in-progress' ? 'background: #fff3cd; color: #856404;' :
'background: #f8d7da; color: #721c24;'
}">${p.status}</span>
</div>
</div>
`).join('');
}
modal.innerHTML = `
<div style="background: white; padding: 30px; border-radius: 12px; max-width: 700px; width: 90%; max-height: 80vh; overflow-y: auto;">
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
<h2 style="margin: 0;">📁 All My Projects</h2>
<button onclick="this.closest('div').parentElement.parentElement.remove()" style="background: none; border: none; font-size: 24px; cursor: pointer; color: #666;">×</button>
</div>
${projectsHTML}
<button onclick="this.closest('div').parentElement.remove()" style="background: #007bff; color: white; border: none; padding: 10px 20px; border-radius: 6px; cursor: pointer; width: 100%; margin-top: 20px;">
Close
</button>
</div>
`;
document.body.appendChild(modal);
modal.addEventListener('click', (e) => {
if (e.target === modal) {
modal.remove();
}
});
} catch (error) {
alert('Failed to load projects');
}
}