-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
339 lines (294 loc) · 13.7 KB
/
index.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Doubts & Replies</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">
<link rel="stylesheet" href="styless.css">
<style>
</style>
</head>
<body>
<div class="notification-wrapper">
<button id="notificationBell" class="notification-bell" onclick="toggleNotifications()">
<i class="fas fa-bell"></i>
<span class="notification-dot"></span> <!-- Red dot for new notifications -->
</button>
<div id="notificationsDropdown" class="notifications-dropdown"></div>
</div>
<div class="container">
<div class="header">
<div class="user-info" id="user-info"></div>
</div>
<!-- Form to submit a doubt -->
<h3>Submit a Doubt</h3>
<textarea id="doubtText" placeholder="Type your doubt here..."></textarea>
<input type="text" id="imageUrl" placeholder="Enter image URL (optional)">
<img id="imagePreview" style="display:none;" />
<button id="submitDoubtBtn">Submit Doubt</button>
<!-- List of doubts -->
<div id="doubtList"></div>
</div>
<!-- Popup for Signup/Login -->
<div class="overlay" id="overlay" style="display:none;"></div>
<div class="popup" id="popup">
<h3>Please Sign Up or Log In</h3>
<input type="text" id="signupName" placeholder="Enter your name" required>
<input type="email" id="signupEmail" placeholder="Enter your email" required>
<input type="password" id="signupPassword" placeholder="Enter your password" required>
<button id="signupBtn">Sign Up</button>
<h4>or</h4>
<input type="email" id="loginEmail" placeholder="Enter your email to login" required>
<input type="password" id="loginPassword" placeholder="Enter your password to login" required>
<button id="loginBtn">Login</button>
<button id="closePopupBtn">Close</button>
</div>
<!-- Firebase SDK -->
<script src="https://www.gstatic.com/firebasejs/8.10.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.10.0/firebase-database.js"></script>
<script src="https://www.gstatic.com/firebasejs/8.10.0/firebase-auth.js"></script>
<script>
// Firebase configuration
const firebaseConfig = {
apiKey: "AIzaSyAaCo6eyq1XLorr0HIx2VMEu-8JC5Ycbfs",
authDomain: "doubts-solve.firebaseapp.com",
databaseURL: "https://doubts-solve-default-rtdb.asia-southeast1.firebasedatabase.app",
projectId: "doubts-solve",
storageBucket: "doubts-solve.firebasestorage.app",
messagingSenderId: "568989272846",
appId: "1:568989272846:web:178ed0843c01eacd86655f",
measurementId: "G-JWT5TMXED0"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
const database = firebase.database();
const auth = firebase.auth();
// DOM elements
const submitDoubtBtn = document.getElementById('submitDoubtBtn');
const doubtText = document.getElementById('doubtText');
const imageUrl = document.getElementById('imageUrl');
const imagePreview = document.getElementById('imagePreview');
const doubtList = document.getElementById('doubtList');
const popup = document.getElementById('popup');
const overlay = document.getElementById('overlay');
const userInfo = document.getElementById('user-info');
// Signup/Login form elements
const signupName = document.getElementById('signupName');
const signupEmail = document.getElementById('signupEmail');
const signupPassword = document.getElementById('signupPassword');
const loginEmail = document.getElementById('loginEmail');
const loginPassword = document.getElementById('loginPassword');
// Display popup for signup/login
function showPopup() {
overlay.style.display = 'block';
popup.style.display = 'block';
}
// Hide popup
function hidePopup() {
overlay.style.display = 'none';
popup.style.display = 'none';
}
// Preview image on URL input
imageUrl.addEventListener('input', () => {
const url = imageUrl.value.trim();
if (url) {
imagePreview.src = url;
imagePreview.style.display = 'block';
} else {
imagePreview.style.display = 'none';
}
});
// Submit Doubt function
submitDoubtBtn.addEventListener('click', () => {
const doubtContent = doubtText.value.trim();
const imgUrl = imageUrl.value.trim();
if (auth.currentUser) {
const userName = auth.currentUser.displayName || "User"; // Get logged-in user's name
const timestamp = new Date().toISOString();
saveDoubt(doubtContent, userName, timestamp, imgUrl);
} else {
showPopup(); // Prompt user to sign up or log in
}
});
// Save doubt to Firebase Realtime Database
function saveDoubt(content, userName, timestamp, imageUrl) {
const user = auth.currentUser;
// Check if the user's email is '[email protected]' and add the verified icon
const userNameWithTick = user.email === '[email protected]'
? `${userName} <i class="fas fa-check-circle" style="color: blue;"></i>` // Blue checkmark for verified user
: userName;
// Include the user's email in the doubt data
const doubtData = {
content,
userName: userNameWithTick, // Use the modified user name with tick
userEmail: user.email, // Save the user's email along with the doubt
timestamp,
imageUrl,
votes: 0
};
const newDoubtKey = database.ref('doubts').push().key;
database.ref('doubts/' + newDoubtKey).set(doubtData).then(() => {
loadDoubts();
doubtText.value = '';
imageUrl.value = '';
imagePreview.style.display = 'none';
});
}
// Load doubts immediately, regardless of whether the user is logged in
loadDoubts();
// Check if the user is logged in and update the user info accordingly
auth.onAuthStateChanged(user => {
const userInfo = document.getElementById('user-info');
if (user) {
// If the user is logged in, display their name with a checkmark if the email matches
const userNameWithTick = user.email === '[email protected]'
? `${user.displayName || "User"} <i class="fas fa-check-circle" style="color: blue;"></i>`
: user.displayName || "User";
userInfo.innerHTML = `Welcome, ${userNameWithTick}`; // Display the user's name
} else {
userInfo.textContent = "Not logged in";
}
});
// Function to load doubts from Firebase
function loadDoubts() {
database.ref('doubts')
.orderByChild('votes') // Sort by votes (descending order)
.once('value', (snapshot) => {
doubtList.innerHTML = ''; // Clear existing doubts
let doubtsArray = [];
snapshot.forEach(childSnapshot => {
const doubt = childSnapshot.val();
doubtsArray.push({
key: childSnapshot.key,
...doubt
});
});
// Reverse the array to show latest posts first
doubtsArray.reverse();
// Sort doubts by votes in descending order
doubtsArray.sort((a, b) => b.votes - a.votes);
doubtsArray.forEach((doubt, index) => {
const doubtElement = document.createElement('div');
doubtElement.classList.add('post');
let trendingText = index < 2 ? '<span class="trending-text">Trending..</span>' : '';
// Check if the logged-in user is the one who posted the doubt
const showEditButton = auth.currentUser && auth.currentUser.email === doubt.userEmail;
doubtElement.innerHTML = `
${trendingText}
<strong>${doubt.userName}</strong> <em>${new Date(doubt.timestamp).toLocaleString()}</em>
<p>${doubt.content}</p>
${doubt.imageUrl ? `<img src="${doubt.imageUrl}" alt="Doubt Image">` : ''}
${showEditButton ? `<button class="edit-btn" onclick="editPost('${doubt.key}')">Edit</button>` : ''}
<button class="vote-btn" onclick="votePost('${doubt.key}', 'up')"><i class="fas fa-thumbs-up"></i></button>
<button class="vote-btn" onclick="votePost('${doubt.key}', 'down')"><i class="fas fa-thumbs-down"></i></button>
<span class="vote-count">${doubt.votes}</span>
<div class="replies">
<h4>Replies:</h4>
<div id="replies-${doubt.key}"></div>
<textarea id="replyText-${doubt.key}" placeholder="Type your answer..."></textarea>
<button onclick="submitReply('${doubt.key}')">Submit Answer</button>
</div>
`;
doubtList.appendChild(doubtElement);
loadReplies(doubt.key);
});
});
}
let lastLoadedIndex = 0; // Keeps track of the last loaded reply index
// Load replies for a specific doubt
function loadReplies(doubtId) {
database.ref('replies/' + doubtId).once('value', (snapshot) => {
const repliesContainer = document.getElementById(`replies-${doubtId}`);
repliesContainer.innerHTML = ''; // Clear previous content
let replies = [];
snapshot.forEach(childSnapshot => {
const reply = childSnapshot.val();
replies.push({
id: childSnapshot.key,
userName: reply.userName,
timestamp: reply.timestamp,
content: reply.content,
votes: reply.votes,
userEmail: reply.userEmail // Store the email to compare with the logged-in user's email
});
});
// Sort replies by timestamp in descending order (latest first)
replies.sort((a, b) => b.timestamp - a.timestamp);
// Reverse the order of replies to display the latest at the top
replies.reverse();
// Show only the first 2 replies initially
const initialReplies = replies.slice(0, 2);
initialReplies.forEach(reply => {
const mostVotedClass = reply.votes >= 3 ? 'most-voted' : '';
const replyElement = document.createElement('div');
replyElement.classList.add('reply');
replyElement.innerHTML = `
<strong>${reply.userName}</strong> <em>${new Date(reply.timestamp).toLocaleString()}</em>
<p>${reply.content}</p>
${
auth.currentUser && auth.currentUser.email === reply.userEmail
? `<button class="edit-btn" onclick="editReply('${doubtId}', '${reply.id}')">Edit</button>`
: '' // Only show the edit button if the logged-in user is the one who posted the reply
}
<button class="vote-btn" onclick="voteReply('${doubtId}', '${reply.id}', 'up')"><i class="fas fa-thumbs-up"></i></button>
<button class="vote-btn" onclick="voteReply('${doubtId}', '${reply.id}', 'down')"><i class="fas fa-thumbs-down"></i></button>
<span class="vote-count">${reply.votes}</span>
`;
if (mostVotedClass) {
replyElement.classList.add(mostVotedClass);
}
repliesContainer.appendChild(replyElement);
});
// Handle 'View More' button if there are more replies
if (replies.length > 2) {
const viewMoreBtn = document.createElement('button');
viewMoreBtn.classList.add('view-more-btn');
viewMoreBtn.innerText = 'View More';
viewMoreBtn.onclick = () => loadMoreReplies(doubtId, replies, repliesContainer, viewMoreBtn);
repliesContainer.appendChild(viewMoreBtn);
}
});
}
// Initialize user session
auth.onAuthStateChanged(user => {
const userInfo = document.getElementById('user-info');
if (user) {
// If the user is logged in, display their name with a checkmark if the email matches
const userNameWithTick = user.email === '[email protected]'
? `${user.displayName || "User"} <i class="fas fa-check-circle" style="color: blue;"></i>`
: user.displayName || "User";
userInfo.innerHTML = `Welcome, ${userNameWithTick}`; // Display the user's name
} else {
userInfo.textContent = "Not logged in";
}
loadDoubts(); // Load doubts whether the user is logged in or not
});
// Handle sign up
signupBtn.addEventListener('click', () => {
const name = signupName.value.trim();
const email = signupEmail.value.trim();
const password = signupPassword.value.trim();
if (name && email && password) {
auth.createUserWithEmailAndPassword(email, password).then(cred => {
cred.user.updateProfile({ displayName: name });
hidePopup(); // Close popup after sign up
}).catch(error => alert(error.message));
}
});
// Handle login
loginBtn.addEventListener('click', () => {
const email = loginEmail.value.trim();
const password = loginPassword.value.trim();
if (email && password) {
auth.signInWithEmailAndPassword(email, password).then(() => {
hidePopup(); // Close popup after login
}).catch(error => alert(error.message));
}
});
// Close popup
closePopupBtn.addEventListener('click', hidePopup);
</script>
<script src="scriptss.js"></script>
</body>
</html>