This repository has been archived by the owner on Mar 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
46bb3d3
commit 3cfff00
Showing
13 changed files
with
201 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { io } from 'socket.io-client'; | ||
|
||
export const socket = io('ws://localhost:3001'); |
44 changes: 44 additions & 0 deletions
44
client/src/widgets/sidebar/lib/hooks/useListenToNotifications.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { useEffect } from 'react'; | ||
import { useQueryClient } from '@tanstack/react-query'; | ||
import { socket } from '@/shared/api/socket'; | ||
import { IUserProtectedResponse } from '@teameights/types'; | ||
|
||
export const useSocketConnection = (user?: IUserProtectedResponse) => { | ||
const queryClient = useQueryClient(); | ||
|
||
useEffect(() => { | ||
if (!user) { | ||
return; | ||
} | ||
const handleConnect = () => { | ||
console.log('connected'); | ||
}; | ||
|
||
const handleDisconnect = () => { | ||
console.log('disconnected'); | ||
}; | ||
|
||
const handleNotification = () => { | ||
queryClient | ||
.invalidateQueries({ queryKey: ['useGetNotifications'] }) | ||
.then(() => console.log('notification added')); | ||
}; | ||
|
||
const setupSocketListeners = () => { | ||
socket.on('connect', handleConnect); | ||
socket.on('disconnect', handleDisconnect); | ||
socket.on(`notification-${user.id}`, handleNotification); | ||
}; | ||
|
||
const cleanupSocketListeners = () => { | ||
socket.off('connect', handleConnect); | ||
socket.off('disconnect', handleDisconnect); | ||
socket.off(`notification-${user.id}`, handleNotification); | ||
}; | ||
|
||
setupSocketListeners(); | ||
|
||
// Cleanup socket listeners when the component unmounts | ||
return cleanupSocketListeners; | ||
}, [user, queryClient]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.