-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi-client.js
More file actions
640 lines (558 loc) · 22.9 KB
/
api-client.js
File metadata and controls
640 lines (558 loc) · 22.9 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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
// Advanced API Client for Wayforge Backend Integration
class WayforgeAPI {
constructor() {
this.baseURL = window.location.hostname === 'localhost'
? 'http://localhost:3000/api'
: '/api';
this.cache = new Map();
this.requestQueue = [];
this.isOnline = navigator.onLine;
this.initializeEventListeners();
}
initializeEventListeners() {
// Network status monitoring
window.addEventListener('online', () => {
this.isOnline = true;
this.processQueue();
});
window.addEventListener('offline', () => {
this.isOnline = false;
});
}
async request(endpoint, options = {}) {
const config = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
...options.headers
},
...options
};
// Add request to queue if offline
if (!this.isOnline) {
return this.queueRequest(endpoint, config);
}
try {
const response = await fetch(`${this.baseURL}${endpoint}`, config);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const data = await response.json();
// Cache successful GET requests
if (config.method === 'GET') {
this.cache.set(endpoint, { data, timestamp: Date.now() });
}
return data;
} catch (error) {
console.error(`API Request failed: ${endpoint}`, error);
// Return cached data if available
if (config.method === 'GET' && this.cache.has(endpoint)) {
const cached = this.cache.get(endpoint);
// Return cached data if less than 5 minutes old
if (Date.now() - cached.timestamp < 300000) {
return cached.data;
}
}
throw error;
}
}
queueRequest(endpoint, config) {
return new Promise((resolve, reject) => {
this.requestQueue.push({ endpoint, config, resolve, reject });
});
}
async processQueue() {
while (this.requestQueue.length > 0) {
const { endpoint, config, resolve, reject } = this.requestQueue.shift();
try {
const result = await this.request(endpoint, config);
resolve(result);
} catch (error) {
reject(error);
}
}
}
// Assessment API
async saveAssessment(assessmentData) {
return this.request('/assessment/save', {
method: 'POST',
body: JSON.stringify(assessmentData)
});
}
// Pathway Generation API
async generatePathway(userProfile, assessmentResults, preferences = {}) {
const requestData = {
userProfile,
assessmentResults,
preferences
};
return this.request('/generate-pathway', {
method: 'POST',
body: JSON.stringify(requestData)
});
}
// Get user pathways
async getUserPathways(userId) {
return this.request(`/pathways/${userId}`);
}
// Recommendations API
async getCourseRecommendations(userProfile, assessmentResults) {
return this.request('/recommendations/courses', {
method: 'POST',
body: JSON.stringify({ userProfile, assessmentResults })
});
}
// Opportunities API
async getOpportunities() {
return this.request('/opportunities');
}
// Micro-credentials API
async getMicroCredentials() {
return this.request('/micro-credentials');
}
// Health check
async healthCheck() {
return this.request('/health');
}
}
// Enhanced Questionnaire Manager with Backend Integration
class EnhancedQuestionnaireManager extends QuestionnaireManager {
constructor() {
super();
this.api = new WayforgeAPI();
this.userProfile = this.loadUserProfile();
this.isGeneratingPathway = false;
}
loadUserProfile() {
const saved = localStorage.getItem('wayforge_user_profile');
return saved ? JSON.parse(saved) : {
id: `user_${Date.now()}`,
name: '',
email: '',
careerGoals: '',
timeCommitment: 10,
budget: '',
learningStyle: 'visual',
experience: 'intermediate',
interests: []
};
}
saveUserProfile() {
localStorage.setItem('wayforge_user_profile', JSON.stringify(this.userProfile));
}
async generateLearningPaths() {
if (this.isGeneratingPathway) return;
try {
this.isGeneratingPathway = true;
this.showPathwayGenerationUI();
// Collect user preferences
const preferences = this.collectUserPreferences();
// Update user profile
this.updateUserProfile();
// Save assessment results
await this.api.saveAssessment({
userId: this.userProfile.id,
results: this.formData,
timestamp: new Date()
});
// Generate personalized pathway
const response = await this.api.generatePathway(
this.userProfile,
this.formData,
preferences
);
if (response.success) {
this.displayGeneratedPathway(response.pathway);
this.saveGeneratedPathway(response.pathway);
this.showSuccessMessage("Your personalized learning pathway is ready!");
} else {
throw new Error(response.error || 'Failed to generate pathway');
}
} catch (error) {
console.error('Error generating pathway:', error);
this.showErrorMessage('Failed to generate pathway. Please try again.');
} finally {
this.isGeneratingPathway = false;
}
}
collectUserPreferences() {
return {
learningStyle: this.userProfile.learningStyle,
timeCommitment: this.userProfile.timeCommitment,
budget: this.userProfile.budget,
careerGoals: this.userProfile.careerGoals,
preferredFormat: this.getPreferredFormat(),
difficulty: this.getPreferredDifficulty(),
timeline: this.getPreferredTimeline()
};
}
updateUserProfile() {
// Update profile based on questionnaire responses
if (this.formData.careerGoals) {
this.userProfile.careerGoals = this.formData.careerGoals;
}
if (this.formData.timeCommitment) {
this.userProfile.timeCommitment = parseInt(this.formData.timeCommitment);
}
if (this.formData.learningStyle) {
this.userProfile.learningStyle = this.formData.learningStyle;
}
this.saveUserProfile();
}
showPathwayGenerationUI() {
// Create and show pathway generation modal
const modal = document.createElement('div');
modal.className = 'pathway-generation-modal';
modal.innerHTML = `
<div class="modal-content">
<div class="generation-header">
<h2>🤖 AI is crafting your perfect learning pathway...</h2>
<p>Analyzing your profile and creating a personalized journey</p>
</div>
<div class="generation-progress">
<div class="progress-steps">
<div class="step active">
<div class="step-icon">🧠</div>
<span>Analyzing Skills</span>
</div>
<div class="step">
<div class="step-icon">🎯</div>
<span>Matching Goals</span>
</div>
<div class="step">
<div class="step-icon">📚</div>
<span>Selecting Resources</span>
</div>
<div class="step">
<div class="step-icon">✨</div>
<span>Personalizing</span>
</div>
</div>
<div class="progress-bar">
<div class="progress-fill" style="width: 0%"></div>
</div>
</div>
<div class="generation-insights">
<div class="insight-item">
<span class="insight-label">Skill Match Score:</span>
<span class="insight-value">98%</span>
</div>
<div class="insight-item">
<span class="insight-label">Career Alignment:</span>
<span class="insight-value">Perfect</span>
</div>
<div class="insight-item">
<span class="insight-label">Learning Style:</span>
<span class="insight-value">Visual + Hands-on</span>
</div>
</div>
</div>
`;
document.body.appendChild(modal);
// Animate progress
this.animatePathwayGeneration(modal);
}
animatePathwayGeneration(modal) {
const steps = modal.querySelectorAll('.step');
const progressFill = modal.querySelector('.progress-fill');
let currentStep = 0;
const interval = setInterval(() => {
if (currentStep > 0) {
steps[currentStep - 1].classList.remove('active');
steps[currentStep - 1].classList.add('completed');
}
if (currentStep < steps.length) {
steps[currentStep].classList.add('active');
progressFill.style.width = `${((currentStep + 1) / steps.length) * 100}%`;
currentStep++;
} else {
clearInterval(interval);
}
}, 800);
}
displayGeneratedPathway(pathway) {
// Remove generation modal
const modal = document.querySelector('.pathway-generation-modal');
if (modal) modal.remove();
// Create pathway display
const pathwayDisplay = document.createElement('div');
pathwayDisplay.className = 'pathway-display-modal';
pathwayDisplay.innerHTML = `
<div class="modal-content">
<div class="pathway-header">
<div class="pathway-score">
<div class="score-circle">
<span class="score-number">${pathway.personalizedScore}</span>
<span class="score-label">Match Score</span>
</div>
</div>
<div class="pathway-info">
<h1>${pathway.title}</h1>
<p class="pathway-description">${pathway.description}</p>
<div class="pathway-meta">
<span class="duration">📅 ${pathway.duration}</span>
<span class="difficulty">⚡ ${pathway.difficulty}</span>
<span class="phases">📚 ${pathway.phases.length} Phases</span>
</div>
</div>
</div>
<div class="pathway-phases">
<h3>Your Learning Journey</h3>
<div class="phases-timeline">
${pathway.phases.map((phase, index) => `
<div class="phase-card">
<div class="phase-number">${index + 1}</div>
<div class="phase-content">
<h4>${phase.title}</h4>
<p>${phase.description}</p>
<div class="phase-stats">
<span>📖 ${phase.modules.length} Modules</span>
<span>⏱️ ${phase.duration}</span>
<span>🎯 ${phase.projects.length} Projects</span>
</div>
</div>
</div>
`).join('')}
</div>
</div>
<div class="pathway-recommendations">
<h3>Recommended for You</h3>
<div class="recommendations-grid">
${pathway.recommendations.courses.slice(0, 3).map(course => `
<div class="recommendation-card">
<div class="rec-score">${course.matchScore}%</div>
<h4>${course.title}</h4>
<p class="rec-provider">${course.provider}</p>
<div class="rec-meta">
<span>⭐ ${course.rating}</span>
<span>⏱️ ${course.duration}</span>
<span>💰 ${course.price}</span>
</div>
<div class="rec-reasons">
${course.reasons.slice(0, 2).map(reason => `
<span class="reason-tag">✓ ${reason}</span>
`).join('')}
</div>
</div>
`).join('')}
</div>
</div>
<div class="pathway-actions">
<button class="btn btn-primary pathway-start-btn">
🚀 Start Your Journey
</button>
<button class="btn btn-outline pathway-customize-btn">
⚙️ Customize Pathway
</button>
<button class="btn btn-outline pathway-save-btn">
💾 Save for Later
</button>
</div>
<button class="close-pathway-btn">
<i class="fas fa-times"></i>
</button>
</div>
`;
document.body.appendChild(pathwayDisplay);
// Add event listeners
this.bindPathwayEvents(pathwayDisplay, pathway);
// Animate entrance
setTimeout(() => pathwayDisplay.classList.add('show'), 100);
}
bindPathwayEvents(modal, pathway) {
const startBtn = modal.querySelector('.pathway-start-btn');
const customizeBtn = modal.querySelector('.pathway-customize-btn');
const saveBtn = modal.querySelector('.pathway-save-btn');
const closeBtn = modal.querySelector('.close-pathway-btn');
startBtn.addEventListener('click', () => {
this.startLearningJourney(pathway);
});
customizeBtn.addEventListener('click', () => {
this.customizePathway(pathway);
});
saveBtn.addEventListener('click', () => {
this.savePathwayForLater(pathway);
});
closeBtn.addEventListener('click', () => {
modal.remove();
});
}
startLearningJourney(pathway) {
// Navigate to learning paths page with the generated pathway
localStorage.setItem('active_pathway', JSON.stringify(pathway));
window.location.href = 'learning-paths.html';
}
saveGeneratedPathway(pathway) {
const pathways = JSON.parse(localStorage.getItem('generated_pathways') || '[]');
pathways.unshift(pathway);
localStorage.setItem('generated_pathways', JSON.stringify(pathways.slice(0, 10))); // Keep last 10
}
showSuccessMessage(message) {
const notification = document.createElement('div');
notification.className = 'success-notification';
notification.innerHTML = `
<div class="notification-content">
<i class="fas fa-check-circle"></i>
<span>${message}</span>
</div>
`;
document.body.appendChild(notification);
setTimeout(() => notification.classList.add('show'), 100);
setTimeout(() => {
notification.classList.remove('show');
setTimeout(() => notification.remove(), 300);
}, 4000);
}
showErrorMessage(message) {
const notification = document.createElement('div');
notification.className = 'error-notification';
notification.innerHTML = `
<div class="notification-content">
<i class="fas fa-exclamation-circle"></i>
<span>${message}</span>
</div>
`;
document.body.appendChild(notification);
setTimeout(() => notification.classList.add('show'), 100);
setTimeout(() => {
notification.classList.remove('show');
setTimeout(() => notification.remove(), 300);
}, 4000);
}
// Helper methods
getPreferredFormat() {
// Analyze user responses to determine preferred learning format
return 'mixed'; // video, text, interactive, mixed
}
getPreferredDifficulty() {
// Determine preferred difficulty based on assessment
const avgScore = Object.values(this.formData).flat().reduce((a, b) => a + b, 0) / Object.values(this.formData).flat().length;
if (avgScore < 2.5) return 'beginner';
if (avgScore < 3.5) return 'intermediate';
return 'advanced';
}
getPreferredTimeline() {
return this.userProfile.timeCommitment > 15 ? 'intensive' : 'flexible';
}
}
// Initialize API client globally
window.wayforgeAPI = new WayforgeAPI();
// Enhanced Dashboard Manager with Real Data
class EnhancedDashboardManager extends DashboardManager {
constructor() {
super();
this.api = new WayforgeAPI();
this.realTimeData = {
opportunities: [],
microCredentials: [],
recommendations: []
};
}
async init() {
await this.loadRealTimeData();
super.init();
this.updateWithRealData();
}
async loadRealTimeData() {
try {
// Load real opportunities and credentials
const [opportunities, microCredentials] = await Promise.all([
this.api.getOpportunities(),
this.api.getMicroCredentials()
]);
this.realTimeData.opportunities = opportunities.opportunities || [];
this.realTimeData.microCredentials = microCredentials.microCredentials || [];
} catch (error) {
console.error('Error loading real-time data:', error);
}
}
updateWithRealData() {
this.updateOpportunities();
this.updateMicroCredentials();
}
updateOpportunities() {
const container = document.querySelector('.opportunities-section');
if (!container || this.realTimeData.opportunities.length === 0) return;
const opportunitiesHTML = this.realTimeData.opportunities.slice(0, 6).map(opp => `
<div class="opportunity-card" data-type="${opp.type.toLowerCase()}">
<div class="opp-header">
<div class="opp-type">${opp.type}</div>
<div class="opp-date">${new Date(opp.date).toLocaleDateString()}</div>
</div>
<h3>${opp.title}</h3>
<p>${opp.description}</p>
<div class="opp-meta">
<span class="level">📊 ${opp.level}</span>
<span class="duration">⏱️ ${opp.duration}</span>
<span class="price">💰 ${opp.price}</span>
</div>
<div class="opp-skills">
${opp.skills.slice(0, 3).map(skill => `<span class="skill-tag">${skill}</span>`).join('')}
</div>
<div class="opp-actions">
<button class="btn btn-primary btn-sm">Apply Now</button>
<button class="btn btn-outline btn-sm">Learn More</button>
</div>
</div>
`).join('');
container.innerHTML = `
<h2>🎯 Live Opportunities</h2>
<div class="opportunities-grid">
${opportunitiesHTML}
</div>
`;
}
updateMicroCredentials() {
const container = document.querySelector('.micro-credentials-section');
if (!container || this.realTimeData.microCredentials.length === 0) return;
const credentialsHTML = this.realTimeData.microCredentials.map(cred => `
<div class="credential-card">
<div class="cred-header">
<div class="cred-provider">${cred.provider}</div>
<div class="cred-rating">⭐ ${cred.rating}</div>
</div>
<h3>${cred.title}</h3>
<div class="cred-meta">
<span>⏱️ ${cred.duration}</span>
<span>📈 ${cred.level}</span>
<span>💰 ${cred.price}</span>
</div>
<div class="cred-verification">
<i class="fas fa-certificate"></i>
<span>${cred.verification}</span>
</div>
<div class="cred-acceptance">
<span>Accepted by:</span>
<div class="company-logos">
${cred.acceptedBy.slice(0, 4).map(company => `
<span class="company-tag">${company}</span>
`).join('')}
</div>
</div>
<button class="btn btn-primary btn-sm">Enroll Now</button>
</div>
`).join('');
container.innerHTML = `
<h2>🏆 Micro-Credentials</h2>
<div class="credentials-grid">
${credentialsHTML}
</div>
`;
}
}
// Initialize enhanced managers when DOM is ready
document.addEventListener('DOMContentLoaded', function() {
// Check if we're on the questionnaire page
if (window.location.pathname.includes('questionnaire.html')) {
window.questionnaireManager = new EnhancedQuestionnaireManager();
}
// Check if we're on the dashboard page
if (window.location.pathname.includes('dashboard.html')) {
window.dashboardManager = new EnhancedDashboardManager();
window.dashboardManager.init();
}
});
// Export for use in other files
if (typeof module !== 'undefined' && module.exports) {
module.exports = { WayforgeAPI, EnhancedQuestionnaireManager, EnhancedDashboardManager };
}