Skip to content

Commit

Permalink
🚀 joining chat callback issue solved
Browse files Browse the repository at this point in the history
  • Loading branch information
sinanptm committed Oct 6, 2024
1 parent df9aeab commit b71e1d7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions client/app/(patient)/chats/@chatList/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const Page = () => {
joinChatRoom(chatId);
}


const handleAddDoctorChat = (doctorId: string) => {
setNewChatModalOpen(false);
createChat(doctorId);
Expand Down
23 changes: 16 additions & 7 deletions client/lib/hooks/useChats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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..." });
});
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit b71e1d7

Please sign in to comment.