11import { useEffect , useState } from 'react' ;
22import { useNavigate } from 'react-router' ;
33
4- import { getTimeLines } from '@/apis/notification' ;
4+ import { getTimeLines , patchReadNotification , patchReadNotificationAll } from '@/apis/notification' ;
55import PageTitle from '@/components/PageTitle' ;
6+ import { useServerSentEvents } from '@/hooks/useServerSentEvents' ;
67
78import NotificationItem from './components/NotificationItem' ;
89import WarningModal from './components/WarningModal' ;
910
10- const DUMMY_NOTI : Noti [ ] = [
11- {
12- timelineId : 1 ,
13- alarmType : 'LETTER' ,
14- content : 1 ,
15- message : '12E31님이 편지를 보냈습니다.' ,
16- read : false ,
17- } ,
18- {
19- timelineId : 2 ,
20- alarmType : 'REPORT' ,
21- content : '욕설 확인되어 경고 조치함.' ,
22- message : '따숨님, 욕설로 인해 경고를 받으셨어요.' ,
23- read : false ,
24- } ,
25- {
26- timelineId : 3 ,
27- alarmType : 'LETTER' ,
28- content : 1 ,
29- message : '12E31님이 편지를 보냈습니다.' ,
30- read : false ,
31- } ,
32- {
33- timelineId : 4 ,
34- alarmType : 'LETTER' ,
35- content : 1 ,
36- message : '12E31님이 편지를 보냈습니다.' ,
37- read : true ,
38- } ,
39- {
40- timelineId : 5 ,
41- alarmType : 'LETTER' ,
42- content : 1 ,
43- message : '12E31님이 편지를 보냈습니다.' ,
44- read : false ,
45- } ,
46- {
47- timelineId : 6 ,
48- alarmType : 'POSTED' ,
49- content : 1 ,
50- message : '12E31님과의 대화가 게시판에 공유되었어요.' ,
51- read : false ,
52- } ,
53- {
54- timelineId : 7 ,
55- alarmType : 'SHARE' ,
56- content : 1 ,
57- message : '12E31님과의 게시글에 대한 공유요청을 보냈어요.' ,
58- read : false ,
59- } ,
60- ] ;
61-
6211const NotificationsPage = ( ) => {
12+ useServerSentEvents ( ) ;
13+
6314 const navigate = useNavigate ( ) ;
6415
65- const [ noti , getNoti ] = useState < Noti [ ] > ( [ ] ) ;
16+ const [ noti , setNoti ] = useState < Noti [ ] > ( [ ] ) ;
6617
6718 const [ isOpenWarningModal , setIsOpenWarningModal ] = useState ( false ) ;
6819
@@ -84,8 +35,30 @@ const NotificationsPage = () => {
8435 }
8536 } ;
8637
38+ const handleGetTimeLines = async ( ) => {
39+ const res = await getTimeLines ( ) ;
40+ if ( res ?. status === 200 ) {
41+ console . log ( res ) ;
42+ setNoti ( res . data . data . content ) ;
43+ }
44+ } ;
45+
46+ const handlePatchReadNotification = async ( timelineId : number ) => {
47+ const res = await patchReadNotification ( timelineId ) ;
48+ if ( res ?. status !== 200 ) {
49+ console . log ( '읽음처리 에러 발생' ) ;
50+ }
51+ } ;
52+
53+ const handlePatchReadNotificationAll = async ( ) => {
54+ const res = await patchReadNotificationAll ( ) ;
55+ if ( res ?. status !== 200 ) {
56+ console . log ( '모두 읽음처리 에러 발생' ) ;
57+ }
58+ } ;
59+
8760 useEffect ( ( ) => {
88- getTimeLines ( getNoti ) ;
61+ handleGetTimeLines ( ) ;
8962 } , [ ] ) ;
9063
9164 return (
@@ -97,17 +70,26 @@ const NotificationsPage = () => {
9770 />
9871 < main className = "flex grow flex-col items-center px-5 pt-20 pb-9" >
9972 < PageTitle className = "mb-10" > 알림</ PageTitle >
100- < button type = "button" className = "body-sb text-gray-60 place-self-end" >
73+ < button
74+ type = "button"
75+ className = "body-sb text-gray-60 place-self-end"
76+ onClick = { ( ) => {
77+ handlePatchReadNotificationAll ( ) ;
78+ } }
79+ >
10180 모두 읽음
10281 </ button >
10382 < ul className = "mt-2 flex h-full w-full flex-col gap-2 pb-10" >
104- { DUMMY_NOTI . map ( ( notification ) => (
83+ { noti . map ( ( notification ) => (
10584 < li key = { notification . timelineId } >
10685 < NotificationItem
10786 type = { notification . alarmType }
108- message = { notification . message }
87+ title = { notification . title }
10988 read = { notification . read }
110- onClick = { ( ) => handleClickItem ( notification . alarmType , notification . content ) }
89+ onClick = { ( ) => {
90+ handleClickItem ( notification . alarmType , notification . content ) ;
91+ handlePatchReadNotification ( notification . timelineId ) ;
92+ } }
11193 />
11294 </ li >
11395 ) ) }
0 commit comments