-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoad.html
291 lines (255 loc) · 10.7 KB
/
Load.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
<!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>
/* Add your CSS styles here */
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>Doubts and Replies</h1>
<div class="user-info" id="user-info">
<i class="fas fa-bell" id="notificationIcon" style="position:relative; font-size: 24px; cursor: pointer;"></i>
<span id="notificationBadge" style="position: absolute; top: -5px; right: -5px; background-color: red; color: white; border-radius: 50%; padding: 5px; font-size: 12px; display: none;"></span>
</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');
const notificationIcon = document.getElementById('notificationIcon');
const notificationBadge = document.getElementById('notificationBadge');
// 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');
// 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';
}
});
// Signup
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));
}
});
// 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));
}
});
// 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';
}
// Submit Doubt
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
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;
const doubtData = {
content,
userName: userNameWithTick,
userEmail: user.email,
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();
// Handle replies
function submitReply(doubtId) {
const replyText = document.getElementById(`replyText-${doubtId}`).value.trim();
if (replyText) {
const userName = auth.currentUser.displayName || "User";
const timestamp = new Date().toISOString();
const replyData = {
userName,
content: replyText,
timestamp,
votes: 0
};
const replyKey = database.ref('replies/' + doubtId).push().key;
database.ref('replies/' + doubtId + '/' + replyKey).set(replyData).then(() => {
// Check if the doubt belongs to the current user (notify if it's their post)
database.ref('doubts/' + doubtId).once('value', (snapshot) => {
const doubt = snapshot.val();
if (doubt.userEmail !== auth.currentUser.email) {
createNotification(doubt.userEmail, `Your doubt has a new reply from ${userName}`);
}
});
loadReplies(doubtId);
loadDoubts();
});
}
}
// Create a notification
function createNotification(userEmail, message) {
const timestamp = new Date().toISOString();
const notificationData = {
userEmail,
message,
timestamp,
read: false
};
// Save notification to Firebase
database.ref('notifications').push().set(notificationData).then(() => {
updateNotificationIcon();
});
}
// Update notification icon with unread count
function updateNotificationIcon() {
const userEmail = auth.currentUser.email;
database.ref('notifications')
.orderByChild('userEmail')
.equalTo(userEmail)
.once('value', (snapshot) => {
let unreadCount = 0;
snapshot.forEach(childSnapshot => {
const notification = childSnapshot.val();
if (!notification.read) {
unreadCount++;
}
});
if (unreadCount > 0) {
notificationBadge.style.display = 'inline';
notificationBadge.textContent = unreadCount;
} else {
notificationBadge.style.display = 'none';
}
});
}
// Mark notifications as read when clicked
notificationIcon.addEventListener('click', () => {
const userEmail = auth.currentUser.email;
const notificationsRef = database.ref('notifications');
notificationsRef
.orderByChild('userEmail')
.equalTo(userEmail)
.once('value', (snapshot) => {
snapshot.forEach(childSnapshot => {
const notificationKey = childSnapshot.key;
notificationsRef.child(notificationKey).update({ read: true });
});
updateNotificationIcon();
});
});
// Initialize user session
auth.onAuthStateChanged(user => {
if (user) {
// Update the notification icon on login
updateNotificationIcon();
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}`;
} else {
userInfo.textContent = "Not logged in";
}
loadDoubts();
});
</script>
<script src="scriptss.js"></script>
</body>
</html>