diff --git a/client/app/(patient)/chats/@chatList/page.tsx b/client/app/(patient)/chats/@chatList/page.tsx index 8643170c..890670c1 100644 --- a/client/app/(patient)/chats/@chatList/page.tsx +++ b/client/app/(patient)/chats/@chatList/page.tsx @@ -27,6 +27,7 @@ const Page = () => { joinChatRoom(chatId); } + const handleAddDoctorChat = (doctorId: string) => { setNewChatModalOpen(false); createChat(doctorId); diff --git a/client/lib/hooks/useChats.ts b/client/lib/hooks/useChats.ts index 246b32bb..5c0c8f75 100644 --- a/client/lib/hooks/useChats.ts +++ b/client/lib/hooks/useChats.ts @@ -32,6 +32,10 @@ const useChats = ({ role, messagePath }: Props) => { setChats(chats); }); + socket.on("connect", () => { + socket.emit("getChats"); + }); + socket.on("connect_error", () => { setError({ message: "Connection failed. Reconnecting..." }); }); @@ -70,23 +74,28 @@ const useChats = ({ role, messagePath }: Props) => { }, [role, messagePath, setCredentials, router]); - const joinChatRoom = useCallback((chatId: string) => { - if (socketRef.current) { + const joinChatRoom = (chatId: string) => { + if (socketRef.current && socketRef.current.connected) { socketRef.current.emit("joinRoom", chatId.toString()); + } else { + connectSocket(); + socketRef.current?.once("connect", () => { + socketRef.current?.emit("joinRoom", chatId.toString()); + }); } - }, []); + } - const createChat = useCallback((receiverId: string) => { + const createChat = (receiverId: string) => { if (socketRef.current) { socketRef.current.emit("createChat", receiverId); } - }, []); + } - const getPatients = useCallback(() => { + const getPatients = () => { if (socketRef.current) { socketRef.current.emit("getPatients"); } - }, []); + } useEffect(() => { connectSocket();