-
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.
* ✨ feature: useNotifcationEvent 훅 생성 * 🥅 chore: enabled 옵션 수정 * ✨ feature: 알림페이지 SSE 적용 * ✨ feature: 알림 수신시 토스트 나오기 적용 * ✨ feature: 알림 아이템에 애니메이션 적용
- Loading branch information
Showing
7 changed files
with
201 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
19 changes: 19 additions & 0 deletions
19
frontend/src/components/common/NotificationToast/index.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,19 @@ | ||
import { useRouter } from 'next/router'; | ||
|
||
import useNotificationEvent from '@hooks/useNotificationEvent'; | ||
import { showToast } from '@utils/toast'; | ||
|
||
const NotificationToast = () => { | ||
const { isReady, pathname } = useRouter(); | ||
|
||
useNotificationEvent({ | ||
onNotification: (e) => { | ||
showToast({ title: '알림 도착!', message: '알림 페이지에서 내용을 확인해보세요.' }); | ||
}, | ||
enabled: isReady && pathname !== '/notification', | ||
}); | ||
|
||
return <></>; | ||
}; | ||
|
||
export default NotificationToast; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useEffect } from 'react'; | ||
|
||
import useFetchMyInfo from '@hooks/queries/useFetchMyInfo'; | ||
|
||
interface Props { | ||
onNotification: (e: MessageEvent) => void; | ||
enabled?: boolean; | ||
} | ||
|
||
const useNotificationEvent = ({ onNotification, enabled = true }: Props) => { | ||
const { data: myData } = useFetchMyInfo(); | ||
useEffect(() => { | ||
if (!myData) return; | ||
let sse: EventSource | null = null; | ||
try { | ||
sse = new EventSource(`${process.env.NEXT_PUBLIC_API_URL}/v1/sse`, { | ||
withCredentials: true, | ||
}); | ||
|
||
sse.addEventListener('NOTIFICATION', (e) => { | ||
if (enabled) onNotification(e); | ||
}); | ||
|
||
sse.onerror = (event) => { | ||
sse.close(); | ||
}; | ||
} catch (err) { | ||
throw Error('Server Sent Event Error'); | ||
} | ||
return () => { | ||
if (sse) sse.close(); | ||
}; | ||
}, [myData, onNotification, enabled]); | ||
}; | ||
|
||
export default useNotificationEvent; |
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