Skip to content

Commit 78b7504

Browse files
chat page fixed
1 parent faf8660 commit 78b7504

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

assets/js/account.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ document.addEventListener('DOMContentLoaded', function () {
133133
const userBox = document.createElement('div');
134134
userBox.id = `chat-user-${action}`;
135135
userBox.onclick = function () {
136-
window.location = `/chat.html?action=${action}`;
136+
window.location = `/chat.html?chat=${action}`;
137137
}
138138

139139
const userInfo = `

assets/js/chat.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,17 @@ firebase.auth().onAuthStateChanged(async (user) => {
3737
});
3838

3939
function loadSidebarFromUserHistory(history) {
40-
for (const [uid, name] of Object.entries(history)) {
41-
firebase.firestore().collection("Users").doc(uid).get().then(doc => {
42-
const data = doc.data();
43-
const img = data.profileIMG || "/assets/img/default_user.jpeg";
44-
add_to_sidebar(name, img, uid, data.email || "");
40+
history.forEach(userId => {
41+
firebase.firestore().collection("Users").doc(userId).get().then(doc => {
42+
if (doc.exists) {
43+
const data = doc.data();
44+
const name = data.name || "Unknown";
45+
const profilePic = data.profileIMG || "/assets/img/default_user.jpeg";
46+
const email = data.email || "";
47+
add_to_sidebar(name, profilePic, userId, email);
48+
}
4549
});
46-
}
50+
});
4751
}
4852

4953
function add_to_sidebar(name, profilePic, userId, email) {
@@ -87,6 +91,25 @@ function updateSidebarList(filterText) {
8791
});
8892
sidebarList.appendChild(li);
8993
});
94+
95+
// Firebase email-based lookup for dynamic search
96+
if (sidebarList.children.length === 0 && filterText.includes("@")) {
97+
firebase.firestore().collection("Users")
98+
.where("email", "==", filterText)
99+
.get()
100+
.then(snapshot => {
101+
if (!snapshot.empty) {
102+
const doc = snapshot.docs[0];
103+
const data = doc.data();
104+
const userId = doc.id;
105+
const name = data.name || "Unknown";
106+
const profilePic = data.profileIMG || "/assets/img/default_user.jpeg";
107+
const email = data.email || "";
108+
add_to_sidebar(name, profilePic, userId, email);
109+
updateSidebarList(filterText); // re-run to show only matching user
110+
}
111+
});
112+
}
90113
}
91114

92115
searchInput.addEventListener("input", (e) => {
@@ -130,6 +153,9 @@ sendButton.addEventListener("click", () => {
130153
messageText: msg,
131154
timestamp: firebase.firestore.FieldValue.serverTimestamp()
132155
});
156+
firebase.firestore().collection("Users").doc(CURRENT_USER_ID).update({
157+
userHistory: firebase.firestore.FieldValue.arrayUnion(activeUserId)
158+
});
133159

134160
chatInput.value = "";
135161
});

0 commit comments

Comments
 (0)