-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.js
More file actions
178 lines (145 loc) · 5.8 KB
/
Copy pathchat.js
File metadata and controls
178 lines (145 loc) · 5.8 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
// Import required Firebase modules
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.16.0/firebase-app.js";
import { getAuth, onAuthStateChanged } from "https://www.gstatic.com/firebasejs/9.16.0/firebase-auth.js";
import { getFirestore, doc, getDoc, setDoc, updateDoc, collection, getDocs, query, orderBy, addDoc, arrayUnion } from "https://www.gstatic.com/firebasejs/9.16.0/firebase-firestore.js";
import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.16.0/firebase-analytics.js";
// Your Firebase configuration and initialization
const firebaseConfig = {
apiKey: "AIzaSyCM2bVe7bRvhlBducH9kxw1CRmrsK_kYEA",
authDomain: "balancebuddy-5427b.firebaseapp.com",
databaseURL: "https://balancebuddy-5427b-default-rtdb.asia-southeast1.firebasedatabase.app",
projectId: "balancebuddy-5427b",
storageBucket: "balancebuddy-5427b.firebasestorage.app",
messagingSenderId: "263147724503",
appId: "1:263147724503:web:6ee0f6171e89a4d4575a92",
measurementId: "G-5SJQ1VLRHH"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const db = getFirestore(app);
onAuthStateChanged(auth, (user) => {
if (!user) {
console.log("User not logged in, redirecting to login page");
window.location.href = "login.html"; // Check if the path is correct
} else {
console.log("User is logged in:", user.email);
}
});
// Function to create a new room
async function createRoom() {
const user = auth.currentUser;
if (!user) {
alert('You must be logged in to create a room');
return;
}
const roomId = Math.random().toString(36).substring(2,10); // Function to generate a unique room ID
// Add room to Firestore
const roomRef = doc(db, "rooms", roomId);
try{
await setDoc(roomRef, {
roomId: roomId,
createdBy: user.uid,
members: [user.uid],
createdAt: new Date().toISOString(),
});
alert(`Room created successfully! Room ID: ${roomId}`);
document.getElementById('roomIdInput').value = roomId;
toggleRoomOptions(false);
} catch(error) {
console.error("Error creating room: ", error);
alert("Error creating room. Please try again.");
}
}
//Event listener for creating a room
document.getElementById('createRoomBtn').addEventListener('click', createRoom);
// Function to join the room
async function joinRoom() {
const roomId = document.getElementById('roomIdInput').value;
const user = auth.currentUser;
if (!user) {
alert('You must be logged in to join a room');
return;
}
if (!roomId) {
alert('Please enter a valid room ID');
return;
}
const roomRef = doc(db, 'rooms', roomId);
const roomDoc = await getDoc(roomRef);
if (!roomDoc.exists()) {
alert('Room does not exist');
return;
}
const roomData = roomDoc.data();
const members = roomData.members || [];
if (members.includes(user.uid)) {
alert('You are already in this room');
return;
}
// Add the user to the room's member list
await updateDoc(roomRef, {
members: arrayUnion(user.uid)
});
alert('Joined the room successfully!');
document.getElementById('chatContainer').style.display = 'block'; // Show the chat UI
toggleRoomOptions(false)
listenForMessages(roomId); // Start listening for messages
}
function toggleRoomOptions(show) {
const roomOptions = document.getElementById('roomOptions');
const chatWindow = documents.getElementById('chatWindow');
if(show) {
roomOptions.style.display = 'block';
chatWindow.style.display = 'none';
} else {
roomOptions.style.display = 'none';
chatWindow.style.display = 'block';
}
}
// Function to send a message
async function sendMessage() {
const roomId = document.getElementById('roomIdInput').value;
const messageText = document.getElementById('messageInput').value.trim();
const user = auth.currentUser;
if (!user) {
alert('You must be logged in to send a message');
return;
}
if (!messageText) {
alert('Please enter a message');
return;
}
const message = {
text: messageText,
senderId: user.uid,
timestamp: new Date().toISOString(),
};
const roomRef = doc(db, 'rooms', roomId);
const messagesRef = collection(roomRef, 'messages');
await addDoc(messagesRef, message);
document.getElementById('messageInput').value = ''; // Clear the input field
}
// Real-time message listener for the room
function listenForMessages(roomId) {
const roomRef = doc(db, 'rooms', roomId);
const messagesRef = collection(roomRef, 'messages');
const messagesQuery = query(messagesRef, orderBy('timestamp'));
// Listen for changes in the messages collection
onSnapshot(messagesQuery, (snapshot) => {
const chatWindow = document.getElementById('chatWindow');
snapshot.docChanges().forEach(change => {
if (change.type === 'added') {
const messageData = change.doc.data();
const messageDiv = document.createElement('div');
messageDiv.classList.add('message');
messageDiv.textContent = messageData.text;
chatWindow.appendChild(messageDiv);
chatWindow.scrollTop = chatWindow.scrollHeight; // Auto-scroll to the bottom
}
});
});
}
// Event listeners for buttons
document.getElementById('sendMessageBtn').addEventListener('click', sendMessage);
document.getElementById('joinRoomBtn').addEventListener('click', joinRoom);