diff --git a/backend/src/routes/roomRoutes.ts b/backend/src/routes/roomRoutes.ts index 3e22333..b853072 100644 --- a/backend/src/routes/roomRoutes.ts +++ b/backend/src/routes/roomRoutes.ts @@ -21,7 +21,7 @@ router.get("/", (req: Request, res: Response) => { router.get("/all", async (req: Request, res: Response) => { try { - const rooms = await Room.find(); // Use Mongoose to get all rooms from the database + const rooms = await Room.find(); res.json(rooms); } catch (err) { res.status(500).json({ message: err }); diff --git a/backend/src/routes/socket.ts b/backend/src/routes/socket.ts index c3a1c09..badbe3d 100644 --- a/backend/src/routes/socket.ts +++ b/backend/src/routes/socket.ts @@ -78,6 +78,21 @@ io.on("connection", (socket) => { } }); + socket.on("delete_room", async (roomId) => { + try { + // Delete the room from the database + await Room.findByIdAndDelete(roomId); + + // Notify all clients that the room has been deleted + io.emit("room_deleted", roomId); + + console.log(`Room ${roomId} deleted.`); + } catch (error) { + console.error(`Error deleting room ${roomId}: `, error); + socket.emit("error", "Failed to delete room."); + } + }); + io.emit("users_response", roomUsers); log(`User Connected: ${socket.id}`); diff --git a/frontend/src/components/Chat/ChatFooter.tsx b/frontend/src/components/Chat/ChatFooter.tsx index f8c77a2..4a18262 100644 --- a/frontend/src/components/Chat/ChatFooter.tsx +++ b/frontend/src/components/Chat/ChatFooter.tsx @@ -49,6 +49,20 @@ function ChatFooter({ roomId }: { roomId: string }) { } }, [socket]); + useEffect(() => { + if (socket) { + socket.on("room_deleted", (deletedRoomId) => { + if (deletedRoomId === roomId) { + toast.success("Room deleted successfully."); + setMyRooms(myRooms.filter((room) => room.id !== roomId)); + } + }); + return () => { + socket.off("room_deleted"); + }; + } + }, [socket, roomId]); + function handleCommand(commandString: string, socket: any) { const parts = commandString.substr(1).split(" "); const command = parts[0].toLowerCase(); @@ -72,28 +86,29 @@ function ChatFooter({ roomId }: { roomId: string }) { toast.error("Please specify a room name."); } else { const roomName = args.join(" "); - const newRoomId = uuidv4(); // Use a constant here + const newRoomId = uuidv4(); let newRoom = { title: roomName, id: newRoomId, // Use the constant }; - console.log("New room: ", newRoomId); // Log the constant + console.log("New room: ", newRoomId); setMyRooms([...myRooms, newRoom]); toast.info(`Creating room: ${roomName}`); - socket?.emit("join_room", newRoomId, roomName); // Use the constant + socket?.emit("join_room", newRoomId, roomName); } break; case "delete": - socket?.emit("delete", roomId); + socket?.emit("delete_room", roomId); + toast.info("Deleting room..."); break; case "join": const joinParam = args.join(" "); socket?.emit("join", joinParam); break; case "quit": - const quitParam = args.join(" "); - socket?.emit("quit", quitParam); + const username = localStorage.getItem("name"); + socket?.emit("leave_room", username, roomId); break; case "users": socket?.emit("users"); @@ -250,4 +265,4 @@ function ChatFooter({ roomId }: { roomId: string }) { ); } -export default ChatFooter; +export default ChatFooter; \ No newline at end of file