Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Gab committed Feb 13, 2024
2 parents 316fc0c + ba6b113 commit 2d769c8
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
2 changes: 1 addition & 1 deletion backend/src/routes/roomRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
15 changes: 15 additions & 0 deletions backend/src/routes/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);

Expand Down
29 changes: 22 additions & 7 deletions frontend/src/components/Chat/ChatFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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");
Expand Down Expand Up @@ -250,4 +265,4 @@ function ChatFooter({ roomId }: { roomId: string }) {
);
}

export default ChatFooter;
export default ChatFooter;

0 comments on commit 2d769c8

Please sign in to comment.