Skip to content

Commit 0dd90d8

Browse files
Feat/Notification 1차 작업 #38
1 parent 7112cc0 commit 0dd90d8

File tree

6 files changed

+101
-1
lines changed

6 files changed

+101
-1
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"@testing-library/react": "^13.0.0",
99
"@testing-library/user-event": "^13.5.0",
1010
"axios": "^1.3.2",
11+
"event-source-polyfill": "^1.0.31",
1112
"html-webpack-plugin": "^5.5.0",
1213
"json-server": "^0.17.1",
1314
"picker": "^0.1.4",

src/components/notification/NotificationList.jsx

+2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import React from "react";
2+
import NotificationsTest from "./NotificationsTest";
23

34
const NotificationList = () => {
45
return (
56
<>
67
{/* 확인안한 것은 볼드처리, 확인한 것은 라이트 */}
78
<div className="pt-[50px]">NotificationList</div>
9+
<NotificationsTest />
810
</>
911
);
1012
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import React, { useEffect, useState } from "react";
2+
import { EventSourcePolyfill } from "event-source-polyfill";
3+
import {
4+
addNotification,
5+
clearNotifications,
6+
} from "../../redux/modules/notificationSlice";
7+
import { useDispatch, useSelector } from "react-redux";
8+
9+
const NotificationsTest = () => {
10+
const dispatch = useDispatch();
11+
//notifications 를 가져와 notifications 변수에 할당
12+
const notifications = useSelector(
13+
(state) => state.notification.notifications
14+
);
15+
16+
useEffect(() => {
17+
const accessToken = localStorage.getItem("accessToken");
18+
// SSE 연결을 위해 EventSource 객체를 생성
19+
const eventSource = new EventSourcePolyfill(
20+
`http://shinjeong.shop/pushs?token=${accessToken}`
21+
);
22+
// const eventSource = new EventSourcePolyfill(`/pushs`);
23+
24+
// SSE 연결 성공 시 호출되는 이벤트 핸들러
25+
eventSource.onopen = () => {
26+
console.log("SSE 연결완료");
27+
};
28+
// SSE 메시지 수신 시 호출되는 이벤트 핸들러 함수를 등록하여 SSE 메시지가 도착했을 때의 동작을 정의
29+
eventSource.onmessage = (event) => {
30+
const data = JSON.parse(event.data);
31+
console.log("알림이 도착했습니다", data);
32+
alert("알림이 도착했습니다", data);
33+
//addNotification액션 dispatch
34+
dispatch(addNotification(data));
35+
};
36+
37+
// SSE 연결 오류 발생 시 호출되는 이벤트 핸들러
38+
eventSource.onerror = (error) => {
39+
console.error("SSE 에러발생!", error);
40+
};
41+
42+
return () => {
43+
eventSource.close();
44+
dispatch(clearNotifications());
45+
};
46+
}, [dispatch]);
47+
return (
48+
<div>
49+
<h2> Notifications List </h2>
50+
{notifications.map((notification) => (
51+
<div key={notification.id}>
52+
<span>{notification.message}</span>
53+
</div>
54+
))}
55+
</div>
56+
);
57+
};
58+
59+
export default NotificationsTest;

src/redux/config/ConfigStore.jsx

+8-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,16 @@ import LoginSlice from "../modules/LoginSlice";
33
import ScheduleSlice from "../modules/ScheduleSlice";
44
import SingupSlice from "../modules/SingupSlice";
55
import InvitationSlice from "../modules/InvitationSlice";
6+
import NotificationSlice from "../modules/notificationSlice";
67

78
const store = configureStore({
8-
reducer: { LoginSlice, SingupSlice, ScheduleSlice, InvitationSlice },
9+
reducer: {
10+
LoginSlice,
11+
SingupSlice,
12+
ScheduleSlice,
13+
InvitationSlice,
14+
NotificationSlice,
15+
},
916
});
1017

1118
export default store;
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { createSlice } from "@reduxjs/toolkit";
2+
3+
const initialState = {
4+
notifications: [],
5+
};
6+
7+
// 리듀서
8+
const NotificationSlice = createSlice({
9+
name: "notification",
10+
initialState,
11+
reducers: {
12+
//알림 리스트에 새로운 알림을 추가
13+
addNotification: (state, action) => {
14+
state.notifications.push(action.payload);
15+
},
16+
//알림 리스트 비우기
17+
clearNotifications: (state) => {
18+
state.notifications = [];
19+
},
20+
},
21+
});
22+
23+
export const { addNotification, clearNotifications } =
24+
NotificationSlice.actions;
25+
26+
export default NotificationSlice.reducer;

yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -4395,6 +4395,11 @@ etag@~1.8.1:
43954395
resolved "https://registry.yarnpkg.com/etag/-/etag-1.8.1.tgz#41ae2eeb65efa62268aebfea83ac7d79299b0887"
43964396
integrity sha512-aIL5Fx7mawVa300al2BnEE4iNvo1qETxLrPI/o05L7z6go7fCw1J6EQmbK4FmJ2AS7kgVF/KEZWufBfdClMcPg==
43974397

4398+
event-source-polyfill@^1.0.31:
4399+
version "1.0.31"
4400+
resolved "https://registry.yarnpkg.com/event-source-polyfill/-/event-source-polyfill-1.0.31.tgz#45fb0a6fc1375b2ba597361ba4287ffec5bf2e0c"
4401+
integrity sha512-4IJSItgS/41IxN5UVAVuAyczwZF7ZIEsM1XAoUzIHA6A+xzusEZUutdXz2Nr+MQPLxfTiCvqE79/C8HT8fKFvA==
4402+
43984403
eventemitter3@^4.0.0:
43994404
version "4.0.7"
44004405
resolved "https://registry.yarnpkg.com/eventemitter3/-/eventemitter3-4.0.7.tgz#2de9b68f6528d5644ef5c59526a1b4a07306169f"

0 commit comments

Comments
 (0)