-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathscript.js
More file actions
207 lines (189 loc) · 6.87 KB
/
script.js
File metadata and controls
207 lines (189 loc) · 6.87 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
// scripts.js
document.addEventListener('DOMContentLoaded', () => {
const registerForm = document.getElementById('register-form');
const loginForm = document.getElementById('login-form');
const logoutForm = document.getElementById('logout-form');
registerForm.addEventListener('submit', async (e) => {
e.preventDefault();
const formData = new FormData(registerForm);
const username = formData.get('username');
const password = formData.get('password');
const email = formData.get('email');
const full_name = formData.get('full_name');
try {
const response = await fetch('/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ username, password, email, full_name })
});
if (response.ok) {
alert('Registration successful');
} else {
alert('Registration failed');
}
} catch (error) {
console.error('Error:', error);
}
});
loginForm.addEventListener('submit', async (e) => {
e.preventDefault();
const formData = new FormData(loginForm);
const username = formData.get('username');
const password = formData.get('password');
try {
const response = await fetch('/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ username, password })
});
if (response.ok) {
alert('Login successful');
} else {
alert('Invalid username or password');
}
} catch (error) {
console.error('Error:', error);
}
});
logoutForm.addEventListener('submit', async (e) => {
e.preventDefault();
try {
const response = await fetch('/logout', {
method: 'POST'
});
if (response.ok) {
alert('Logout successful');
} else {
alert('Logout failed');
}
} catch (error) {
console.error('Error:', error);
}
});
// Check if the current page is the course content page
if (window.location.pathname === '/course-content') {
// Call the fetchCourseContent function
fetchCourseContent();
}
// Check if the current page is the course content page
if (window.location.pathname === '/leader-board') {
// Fetch course content from server
fetchLeaderboardData();
}
// Check if the current page is the course content page
if (window.location.pathname === '/dashboard') {
//fetch Logged in user's full name
fetchFullName();
}
});
function fetchCourseContent() {
// Get course ID from URL parameter (assuming course ID is passed in the URL)
const urlParams = new URLSearchParams(window.location.search);
const courseId = urlParams.get('id');
// Make AJAX request to fetch course content from server
fetch(`/course/${courseId}`)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
// Display course content on the page
displayCourseContent(data);
})
.catch(error => {
console.error('Error fetching course content:', error);
});
}
function displayCourseContent(courseContent) {
// Get the course name element
const courseNameElement = document.getElementById('course-name');
// Set the course name
courseNameElement.textContent = courseContent.name;
// Get the course content element
const courseContentElement = document.getElementById('course-content');
// Clear previous content
courseContentElement.innerHTML = '';
// Loop through the modules and display them
courseContent.modules.forEach(module => {
const moduleSection = document.createElement('section');
moduleSection.innerHTML = `
<h2>${module.title}</h2>
<p>${module.description}</p>
<!-- Add more elements as needed (e.g., videos, quizzes) -->
`;
courseContentElement.appendChild(moduleSection);
});
}
function fetchLeaderboardData() {
// Make AJAX request to fetch leaderboard data from server
fetch('/leaderboard')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
// Display leaderboard data on the page
displayLeaderboardData(data);
})
.catch(error => {
console.error('Error fetching leaderboard data:', error);
});
}
function displayLeaderboardData(leaderboardData) {
// Get the leaderboard element
const leaderboardElement = document.getElementById('leaderboard');
// Clear previous content
leaderboardElement.innerHTML = '';
// Create a table to display leaderboard data
const table = document.createElement('table');
table.innerHTML = `
<tr>
<th>Rank</th>
<th>Name</th>
<th>Score</th>
</tr>
`;
// Loop through the leaderboard data and add rows to the table
leaderboardData.forEach((entry, index) => {
const row = document.createElement('tr');
row.innerHTML = `
<td>${index + 1}</td>
<td>${entry.name}</td>
<td>${entry.score}</td>
`;
table.appendChild(row);
});
// Append the table to the leaderboard element
leaderboardElement.appendChild(table);
}
function fetchFullName() {
// Make AJAX request to fetch the user's full name from the server
fetch('/get-fullname')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
// Display the user's full name on the dashboard
displayFullName(data.fullName);
})
.catch(error => {
console.error('Error fetching user full name:', error);
});
}
function displayFullName(fullName) {
// Get the element where the full name will be displayed
const fullNameElement = document.getElementById('user-fullname');
// Set the inner HTML of the element to the user's full name
fullNameElement.textContent = fullName;
}