|
1 | | -import { useState } from 'react'; |
| 1 | +import { useEffect, useState } from 'react'; |
| 2 | +import { useNavigate } from 'react-router'; |
2 | 3 |
|
| 4 | +import { getTimeLines, patchReadNotification, patchReadNotificationAll } from '@/apis/notification'; |
3 | 5 | import PageTitle from '@/components/PageTitle'; |
4 | 6 |
|
5 | 7 | import NotificationItem from './components/NotificationItem'; |
6 | 8 | import WarningModal from './components/WarningModal'; |
7 | 9 |
|
8 | | -const DUMMY_NOTI = [ |
9 | | - { id: 1, type: 'letter', message: '12E31님이 편지를 보냈습니다.', isRead: false }, |
10 | | - { id: 2, type: 'warning', message: '따숨님, 욕설로 인해 경고를 받으셨어요.', isRead: false }, |
11 | | - { id: 3, type: 'letter', message: '12E31님이 편지를 보냈습니다.', isRead: false }, |
12 | | - { id: 4, type: 'letter', message: '12E31님이 편지를 보냈습니다.', isRead: true }, |
13 | | - { id: 5, type: 'letter', message: '12E31님이 편지를 보냈습니다.', isRead: false }, |
14 | | - { id: 6, type: 'board', message: '12E31님과의 대화가 게시판에 공유되었어요.', isRead: false }, |
15 | | - { |
16 | | - id: 7, |
17 | | - type: 'board', |
18 | | - message: '12E31님과의 게시글에 대한 공유요청을 보냈어요.', |
19 | | - isRead: false, |
20 | | - }, |
21 | | -]; |
22 | | - |
23 | 10 | const NotificationsPage = () => { |
| 11 | + const navigate = useNavigate(); |
| 12 | + |
| 13 | + const [noti, setNoti] = useState<Noti[]>([]); |
| 14 | + |
24 | 15 | const [isOpenWarningModal, setIsOpenWarningModal] = useState(false); |
25 | 16 |
|
26 | | - const handleClickItem = (type: string) => { |
27 | | - if (type === 'warning') { |
| 17 | + const [adminText, setAdmintext] = useState<string>(''); |
| 18 | + |
| 19 | + // MEMO : 편지 데이터 전송중 데이터도 추가될건데 나중에 데이터 추가되면 코드 업데이트 하긔 |
| 20 | + const handleClickItem = (alarmType: string, content?: string | number) => { |
| 21 | + if (alarmType === 'LETTER') { |
| 22 | + navigate(`/letter/${content}`); |
| 23 | + } |
| 24 | + if (alarmType === 'REPORT') { |
28 | 25 | setIsOpenWarningModal(true); |
| 26 | + if (typeof content === 'string') setAdmintext(content); |
| 27 | + } |
| 28 | + if (alarmType === 'SHARE') { |
| 29 | + navigate(`/board/letter/${content}`, { state: { isShareLetterPreview: true } }); |
| 30 | + } |
| 31 | + if (alarmType === 'POSTED') { |
| 32 | + navigate(`/board/letter/${content}`); |
29 | 33 | } |
30 | 34 | }; |
31 | 35 |
|
| 36 | + const handleGetTimeLines = async () => { |
| 37 | + const res = await getTimeLines(); |
| 38 | + if (res?.status === 200) { |
| 39 | + console.log(res); |
| 40 | + setNoti(res.data.data.content); |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + const handlePatchReadNotification = async (timelineId: number) => { |
| 45 | + const res = await patchReadNotification(timelineId); |
| 46 | + if (res?.status !== 200) { |
| 47 | + console.log('읽음처리 에러 발생'); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + const handlePatchReadNotificationAll = async () => { |
| 52 | + const res = await patchReadNotificationAll(); |
| 53 | + if (res?.status !== 200) { |
| 54 | + console.log('모두 읽음처리 에러 발생'); |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + useEffect(() => { |
| 59 | + handleGetTimeLines(); |
| 60 | + }, []); |
| 61 | + |
32 | 62 | return ( |
33 | 63 | <> |
34 | | - <WarningModal isOpen={isOpenWarningModal} onClose={() => setIsOpenWarningModal(false)} /> |
| 64 | + <WarningModal |
| 65 | + isOpen={isOpenWarningModal} |
| 66 | + adminText={adminText} |
| 67 | + onClose={() => setIsOpenWarningModal(false)} |
| 68 | + /> |
35 | 69 | <main className="flex grow flex-col items-center px-5 pt-20 pb-9"> |
36 | 70 | <PageTitle className="mb-10">알림</PageTitle> |
37 | | - <button type="button" className="body-sb text-gray-60 place-self-end"> |
| 71 | + <button |
| 72 | + type="button" |
| 73 | + className="body-sb text-gray-60 place-self-end" |
| 74 | + onClick={() => { |
| 75 | + handlePatchReadNotificationAll(); |
| 76 | + }} |
| 77 | + > |
38 | 78 | 모두 읽음 |
39 | 79 | </button> |
40 | 80 | <ul className="mt-2 flex h-full w-full flex-col gap-2 pb-10"> |
41 | | - {DUMMY_NOTI.map((notification) => ( |
42 | | - <li key={notification.id}> |
| 81 | + {noti.map((notification) => ( |
| 82 | + <li key={notification.timelineId}> |
43 | 83 | <NotificationItem |
44 | | - type={notification.type} |
45 | | - message={notification.message} |
46 | | - isRead={notification.isRead} |
47 | | - onClick={() => handleClickItem(notification.type)} |
| 84 | + type={notification.alarmType} |
| 85 | + title={notification.title} |
| 86 | + read={notification.read} |
| 87 | + onClick={() => { |
| 88 | + handleClickItem(notification.alarmType, notification.content); |
| 89 | + handlePatchReadNotification(notification.timelineId); |
| 90 | + }} |
48 | 91 | /> |
49 | 92 | </li> |
50 | 93 | ))} |
|
0 commit comments