Attempt three? at making the message history pop back up for a room.

This commit is contained in:
Charles Descamps 2025-03-28 18:51:47 -07:00
parent 2c02fde8c5
commit 9e8158a5c0

View file

@ -227,12 +227,19 @@
function changeChatRoom() { function changeChatRoom() {
const newChatRoom = prompt("Enter the new chat room:"); const newChatRoom = prompt("Enter the new chat room:");
if (newChatRoom && newChatRoom.trim() !== "") { if (newChatRoom && newChatRoom.trim() !== "") {
// Leave the current chat room
socket.emit('leave', { username: username, chat_room: chatRoom }); socket.emit('leave', { username: username, chat_room: chatRoom });
// Update the chat room
chatRoom = newChatRoom.trim(); chatRoom = newChatRoom.trim();
currentChatRoomElement.textContent = chatRoom; currentChatRoomElement.textContent = chatRoom;
console.log(`Switched to chat room: ${chatRoom}`); console.log(`Switched to chat room: ${chatRoom}`);
// Join the new chat room
socket.emit('join', { username: username, chat_room: chatRoom }); socket.emit('join', { username: username, chat_room: chatRoom });
chatDiv.innerHTML = ""; // Clear the chat history
// Clear the chat history in the UI
chatDiv.innerHTML = "";
// Fetch message history for the new chat room // Fetch message history for the new chat room
fetch(`/get_history?chat_room=${encodeURIComponent(chatRoom)}`) fetch(`/get_history?chat_room=${encodeURIComponent(chatRoom)}`)
@ -244,6 +251,7 @@
}) })
.then(data => { .then(data => {
if (data.messages && data.messages.length > 0) { if (data.messages && data.messages.length > 0) {
// Display each message in the chat history
data.messages.forEach(message => { data.messages.forEach(message => {
const messageElement = document.createElement('div'); const messageElement = document.createElement('div');
messageElement.textContent = message; messageElement.textContent = message;