Skip to content

Commit

Permalink
Socket.io enabled to real-time data exchange
Browse files Browse the repository at this point in the history
  • Loading branch information
Md-Rubel-Ahmed-Rana committed Jul 6, 2024
1 parent c9184ad commit 61b8c3f
Show file tree
Hide file tree
Showing 17 changed files with 171 additions and 121 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ The Team Management App is a web-based application designed to facilitate team c
## Live Links

- [Frontend](https://team-manager-eight.vercel.app)
- [Backend](https://team-management-app-server-with-redis.onrender.com)
- [Backend](https://api-team-manager.onrender.com)

###### Login to website

Expand Down
90 changes: 87 additions & 3 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"react-icons": "^4.10.1",
"react-redux": "^9.1.0",
"react-select": "^5.8.0",
"socket.io-client": "^4.7.5",
"sweetalert2": "^11.7.18",
"swiper": "^11.0.5",
"tailwind-scrollbar": "^3.0.5"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const PendingInvitation = () => {
memberId: user?.id,
});
if (result?.data?.success) {
// socket.emit("notification", result?.data?.data);
socket.emit("notification", result?.data?.data);
Swal.fire({
position: "center",
icon: "success",
Expand All @@ -45,7 +45,7 @@ const PendingInvitation = () => {
});
if (result?.data?.success) {
// send notification viw socket
// socket.emit("notification", result?.data?.data);
socket.emit("notification", result?.data?.data);

Swal.fire({
position: "center",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ const ProjectLeaveRequest = () => {
);
const requests = requestsData?.data;

console.log("project", requests);

return (
<div>
{requests?.length > 0 &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const AddMemberToProject = ({ isOpen, setIsOpen, projectId, team }: Props) => {
const result: any = await addNewMember(memberData);

if (result?.data?.success) {
// socket.emit("notification", result?.data?.data);
socket.emit("notification", result?.data?.data);
Swal.fire({
position: "center",
icon: "success",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ const Projects = () => {
user?.id
);

console.log("Project for task", project);

const handleLeaveRequest = async () => {
Swal.fire({
title: "So sad",
Expand Down Expand Up @@ -69,9 +67,9 @@ const Projects = () => {
}, [query?.id]);

// connect to socket team room
// useEffect(() => {
// socket.emit("task-room", project?.id);
// }, [socket, project?.id]);
useEffect(() => {
socket.emit("task-room", project?.id);
}, [socket, project?.id]);

return (
<div className="flex h-screen">
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/components/pages/tasks/CreateTaskModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ const CreateTaskModal = ({ isOpen, setIsOpen, project, status }: any) => {
data.project = project.id;
const result: any = await createTask(data);
if (result?.data?.success) {
// window.location.reload();
// socket.emit("task", result?.data?.data?.data);
// socket.emit("notification", result?.data?.data?.notification);
window.location.reload();
socket.emit("task", result?.data?.data?.data);
socket.emit("notification", result?.data?.data?.notification);
Swal.fire({
position: "center",
icon: "success",
Expand Down
26 changes: 10 additions & 16 deletions frontend/src/components/pages/tasks/ParentTask.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,7 @@ const ParentTask = ({ project }: Props) => {
// Fetch tasks only if project id is available
if (project?.id) {
fetch(
`https://api-team-manager.onrender.com/task/by-project/${project.id}`,
{
headers: {
authorization: token || "",
},
}
`https://api-team-manager.onrender.com/task/by-project/${project.id}`
)
.then((res) => res.json())
.then((data: any) => {
Expand Down Expand Up @@ -163,16 +158,15 @@ const ParentTask = ({ project }: Props) => {
};

// Listen for new tasks from socket
// useEffect(() => {
// socket.on("task", (newTask: any) => {
// console.log("New task data", newTask);
// addNewTask(newTask);
// });

// return () => {
// socket.off("task");
// };
// }, [socket]);
useEffect(() => {
socket.on("task", (newTask: any) => {
addNewTask(newTask);
});

return () => {
socket.off("task");
};
}, [socket]);

return (
<DragDropContext onDragEnd={onDragEnd}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const AddMemberModal = ({ isOpen, setIsOpen, team }: any) => {
if (result?.data?.success) {
closeModal();
// send invitation notification
// socket.emit("notification", result?.data?.data);
socket.emit("notification", result?.data?.data);
Swal.fire({
position: "center",
icon: "success",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ const MessageForm = ({ teamId, type }: Props) => {
};

const emitData: IMessage = { ...message, poster };
// socket.emit("message", emitData);
socket.emit("message", emitData);
setRealTimeMessages((prev: IMessage[]) => [...prev, emitData]);

setImagePreview([]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const ShowMessages = ({ messages }: Props) => {
const [editedMessage, setEditedMessage] = useState<string | undefined>("");
const [imageModalOpen, setImageModalOpen] = useState<boolean>(false);
const [selectedImage, setSelectedImage] = useState<string>("");
const [onlineUsers, setOnlineUsers] = useState({});
const [isEditMessage, setIsEditMessage] = useState<{
id: string | undefined;
status: boolean;
Expand Down Expand Up @@ -67,30 +66,30 @@ const ShowMessages = ({ messages }: Props) => {
}
};

// useEffect(() => {
// const handleMessage = (data: IMessage) => {
// setRealTimeMessages((prev: IMessage[]) => [...prev, data]);
// };
useEffect(() => {
const handleMessage = (data: IMessage) => {
setRealTimeMessages((prev: IMessage[]) => [...prev, data]);
};

// socket.on("message", handleMessage);
socket?.on("message", handleMessage);

// return () => {
// socket.off("message", handleMessage);
// };
// }, [setRealTimeMessages, socket]);
return () => {
socket?.off("message", handleMessage);
};
}, [setRealTimeMessages, socket]);

// keep updated message in state
// useEffect(() => {
// setRealTimeMessages(messages);
// }, [messages, setRealTimeMessages]);
useEffect(() => {
setRealTimeMessages(messages);
}, [messages, setRealTimeMessages]);

// keep user in the bottom of the message
// useEffect(() => {
// if (messagesContainerRef.current) {
// messagesContainerRef.current.scrollTop =
// messagesContainerRef.current.scrollHeight;
// }
// }, [realTimeMessages, socket]);
useEffect(() => {
if (messagesContainerRef.current) {
messagesContainerRef.current.scrollTop =
messagesContainerRef.current.scrollHeight;
}
}, [realTimeMessages, socket]);

return (
<div
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ const TeamDetailsPage = () => {
};

// connect to socket team room
// useEffect(() => {
// socket.emit("join-room", team?.id);
// }, [socket, team?.id]);
useEffect(() => {
socket?.emit("join-room", team?.id);
}, [socket, team?.id]);

useEffect(() => {
setActiveNav(router?.query?.collaborate);
Expand Down
20 changes: 9 additions & 11 deletions frontend/src/components/shared/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const Navbar = () => {
const { theme, setTheme } = useTheme();
const { data }: any = useLoggedInUserQuery({});
const user: IUser = data?.data;
console.log("User from navbar", user);
const [isOpen, setIsOpen] = useState(false);
const [toggle, setToggle] = useState(false);
const { data: notifiedData } = useGetNotificationQuery(user?.id);
Expand All @@ -32,16 +31,15 @@ const Navbar = () => {
window.location.replace("/");
};

// useEffect(() => {
// const handleNotification = (data: INotification) => {
// console.log("New notification", data);
// setUnreadNotifications((prev: INotification[]) => [...prev, data]);
// };
// socket.on("notification", handleNotification);
// return () => {
// socket.off("notification", handleNotification);
// };
// }, [socket]);
useEffect(() => {
const handleNotification = (data: INotification) => {
setUnreadNotifications((prev: INotification[]) => [...prev, data]);
};
socket?.on("notification", handleNotification);
return () => {
socket?.off("notification", handleNotification);
};
}, [socket]);

useEffect(() => {
const unread = notifications.filter((notified) => !notified.read);
Expand Down
Loading

0 comments on commit 61b8c3f

Please sign in to comment.