Skip to content

Commit

Permalink
Fix Communication service from user typing change
Browse files Browse the repository at this point in the history
  • Loading branch information
Josephaedan committed Nov 13, 2023
1 parent 3eebc06 commit f1cc010
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const ChatInput = ({ socket }: ChatInputProps) => {
e.preventDefault();
if (inputMessage.trim()) {
socket.emit(COMMUNICATION_SOCKET_API.CHAT_MESSAGE, {
userId: user?.id,
userId: user?.id.toString(),
message: inputMessage,
});
setInputMessage("");
Expand Down
15 changes: 12 additions & 3 deletions frontend/app/src/features/room/components/chat/Chatbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { WEBSOCKET_PATH } from "@/constants/api";
import { COMMUNICATION_SOCKET_API } from "@/constants/socket";
import { useAuth } from "@/hooks";
import { env } from "@/lib/env";
import { Box, Spinner } from "@chakra-ui/react";
import { Box, Spinner, useToast } from "@chakra-ui/react";
import { useEffect, useState } from "react";
import { Socket, io } from "socket.io-client";
import { User } from "@/types/user";
Expand All @@ -25,14 +25,15 @@ export const ChatBox = ({ roomId }: ChatBoxProps): JSX.Element => {
setRoomId: state.setRoomId,
})),
);
const toast = useToast();

useEffect(() => {
if (!user?.id) return;

const newSocket = io(`${env.VITE_BACKEND_URL}`, {
path: WEBSOCKET_PATH.COMMUNICATION,
withCredentials: true,
query: { ...user },
query: { ...user, roomId },
});

newSocket.on(
Expand All @@ -42,6 +43,14 @@ export const ChatBox = ({ roomId }: ChatBoxProps): JSX.Element => {

newSocket.on(COMMUNICATION_SOCKET_API.CHAT_MESSAGE_RESPONSE, addMessage);

newSocket.on(COMMUNICATION_SOCKET_API.ERROR, (error: string) => {
toast({
status: "error",
title: error,
isClosable: true,
});
});

newSocket.emit(COMMUNICATION_SOCKET_API.GET_MESSAGES);

setSocket(newSocket);
Expand All @@ -52,7 +61,7 @@ export const ChatBox = ({ roomId }: ChatBoxProps): JSX.Element => {
newSocket.disconnect();
}
};
}, [addMessage, populateMessages, setRoomId, roomId, user, user?.id]);
}, [addMessage, populateMessages, setRoomId, roomId, user, user?.id, toast]);

if (!socket) {
return <Spinner />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const Messages = (): JSX.Element => {
<UserMessage
message={message}
sender={sender}
isUser={sender.id === user.id}
isUser={sender.id.toString() === user.id.toString()}
key={index}
/>
</>
Expand Down

0 comments on commit f1cc010

Please sign in to comment.