Skip to content

Commit

Permalink
feat: 알림 수신 시 가입, 참여여부 쿼리 무효화 기능 (#489)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlwl98 authored Dec 4, 2023
1 parent a7a0109 commit 9785c86
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/ConnectSSE.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,31 @@ import { GameNotificationItem } from '@pages/NotificationPage/components/GameNot

import { useEventSource } from '@hooks/useEventSource';

import { useLoginInfoStore } from '@stores/loginInfo.store';

import { Alarm, CrewAlarm, GameAlarm } from '@type/models';

export const ConnectSSE = () => {
const queryClient = useQueryClient();
const loginInfo = useLoginInfoStore((state) => state.loginInfo);

useEventSource({
subscribeUrl: `${import.meta.env.VITE_BASE_URL}/alarms/subscribe`,
eventListenerParameters: [
[
'AlarmEvent',
(e) => {
if (!loginInfo?.id) {
return;
}
invalidateAlarmsQueries(queryClient);
if ('data' in e) {
const newAlarm: Alarm = JSON.parse(e.data as string);
if ('crewId' in newAlarm) {
invalidateCrewQueries(queryClient, newAlarm);
invalidateCrewQueries(loginInfo?.id, queryClient, newAlarm);
toastCrewAlarm(newAlarm);
} else {
invalidateGameQueries(queryClient, newAlarm);
invalidateGameQueries(loginInfo?.id, queryClient, newAlarm);
toastGameAlarm(newAlarm);
}
}
Expand All @@ -44,6 +50,7 @@ const invalidateAlarmsQueries = (queryClient: QueryClient) => {
};

const invalidateCrewQueries = (
loginId: number,
queryClient: QueryClient,
newCrewAlarm: CrewAlarm
) => {
Expand All @@ -53,9 +60,13 @@ const invalidateCrewQueries = (
queryClient.invalidateQueries({
queryKey: ['crew-detail', newCrewAlarm.crewId],
});
queryClient.invalidateQueries({
queryKey: ['crew-registration', loginId, newCrewAlarm.crewId],
});
};

const invalidateGameQueries = (
loginId: number,
queryClient: QueryClient,
newGameAlarm: GameAlarm
) => {
Expand All @@ -65,6 +76,9 @@ const invalidateGameQueries = (
queryClient.invalidateQueries({
queryKey: ['game-detail', newGameAlarm.gameId],
});
queryClient.invalidateQueries({
queryKey: ['game-registration', loginId, newGameAlarm.gameId],
});
};

const toastCrewAlarm = (newCrewAlarm: CrewAlarm) => {
Expand Down

0 comments on commit 9785c86

Please sign in to comment.