-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoubt.html
303 lines (258 loc) · 12.6 KB
/
doubt.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
<!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">
</head>
<body>
<div class="container">
<div class="header">
<h1>Doubts and Replies</h1>
<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 and save to Firebase (authentication check remains the same)
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
};
// Create a new unique key for the doubt and save it in the Firebase database
const newDoubtKey = database.ref('doubts').push().key;
database.ref('doubts/' + newDoubtKey).set(doubtData).then(() => {
loadDoubts(); // Reload doubts to display the new one
doubtText.value = ''; // Clear the doubt text input
imageUrl.value = ''; // Clear the image URL input
imagePreview.style.display = 'none'; // Hide the image preview
}).catch(error => {
alert('Error saving the doubt: ' + error.message); // Handle any errors while saving the doubt
});
}
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
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 // Assuming you store the email with the reply
});
});
// 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();
replies.forEach(reply => {
const replyElement = document.createElement('div');
replyElement.classList.add('reply');
const replyUserNameWithTick = reply.userName.includes('[email protected]')
? `${reply.userName} <i class="fas fa-check-circle" style="color: blue;"></i>`
: reply.userName;
// Check if the logged-in user is the one who posted the reply
const showEditButton = auth.currentUser && auth.currentUser.email === reply.userEmail;
replyElement.innerHTML = `
<strong>${replyUserNameWithTick}</strong> <em>${new Date(reply.timestamp).toLocaleString()}</em>
<p>${reply.content}</p>
${showEditButton ? `<button class="edit-btn" onclick="editReply('${doubtId}', '${reply.id}')">Edit</button>` : ''}
<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>
`;
repliesContainer.appendChild(replyElement);
});
});
}
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
document.getElementById('signupBtn').addEventListener('click', () => {
const email = signupEmail.value.trim();
const password = signupPassword.value.trim();
const name = signupName.value.trim();
auth.createUserWithEmailAndPassword(email, password).then(cred => {
return cred.user.updateProfile({ displayName: name });
}).then(() => {
hidePopup();
alert('Sign-up successful!');
}).catch(err => {
alert(err.message);
});
});
// Handle login
document.getElementById('loginBtn').addEventListener('click', () => {
const email = loginEmail.value.trim();
const password = loginPassword.value.trim();
auth.signInWithEmailAndPassword(email, password).then(() => {
hidePopup();
}).catch(err => {
alert(err.message);
});
});
</script>
<script src="scriptss.js"></script>
</body>
</html>