-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: qfeed-148 채팅 api 로그인 토큰 연결, 알림 api 세팅 * feat: qfeed-148 알림 api 연동 * fix: qfeed-148 알림리스트 타입 별 구분, 팔로우 버튼
- Loading branch information
Showing
9 changed files
with
296 additions
and
123 deletions.
There are no files selected for viewing
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,81 @@ | ||
/** @jsxImportSource @emotion/react */ | ||
import React, { useState } from 'react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import ProfileImage from '@/components/ui/ProfileImageCon/ProfileImageCon'; | ||
import { NotificationItem } from '@/pages/Alarm/type/alarmType'; | ||
import { | ||
listCon, | ||
listConRead, | ||
notificationContentStyle, | ||
notificationMessageStyle, | ||
notificationTypeStyle, | ||
timeStyle, | ||
followButtonStyle, | ||
} from '@/pages/Alarm/styles'; | ||
import { markNotificationAsRead } from '@/pages/Alarm/api/fetchAlarm'; | ||
|
||
interface NotificationItemProps { | ||
notification: NotificationItem; // 알림 데이터 타입 | ||
isRead: boolean; // 읽음 여부 | ||
onRead: (id: number) => void; // 읽음 처리 콜백 함수 | ||
} | ||
|
||
const NotificationItemComponent: React.FC<NotificationItemProps> = ({ | ||
notification, | ||
isRead, | ||
onRead, | ||
}) => { | ||
const navigate = useNavigate(); | ||
const [isFollowing, setIsFollowing] = useState(false); // 팔로우 상태 관리 | ||
|
||
const handleFollow = () => { | ||
if (isFollowing) { | ||
console.log(`${notification.sender}를 팔로우 취소 요청`); | ||
// TODO: 팔로우 취소 API 호출 | ||
setIsFollowing(false); // 상태 변경 | ||
} else { | ||
console.log(`${notification.sender}를 맞팔로우 요청`); | ||
// TODO: 맞팔로우 API 호출 | ||
setIsFollowing(true); // 상태 변경 | ||
} | ||
}; | ||
|
||
const handleClick = async () => { | ||
if (!isRead) { | ||
await markNotificationAsRead(notification.notificationId); // 읽음 처리 | ||
onRead(notification.notificationId); // 부모 컴포넌트에 읽음 처리 콜백 전달 | ||
} | ||
|
||
// URL이 있는 경우 해당 페이지로 이동 | ||
if (notification.url) { | ||
navigate(notification.url); | ||
} | ||
}; | ||
|
||
return ( | ||
<div | ||
css={[listCon, isRead && listConRead]} // 읽음 여부에 따라 스타일 변경 | ||
onClick={handleClick} | ||
> | ||
<ProfileImage src="" size={40} /> | ||
<div css={notificationContentStyle}> | ||
<span css={notificationTypeStyle}>{notification.type}</span> | ||
<p css={notificationMessageStyle}>{notification.content}</p> | ||
{notification.type === 'FOLLOW' && ( | ||
<button | ||
css={followButtonStyle} | ||
onClick={(e) => { | ||
e.stopPropagation(); // 부모 클릭 이벤트 차단 | ||
handleFollow(); // 팔로우/취소 처리 | ||
}} | ||
> | ||
{isFollowing ? '팔로잉' : '맞팔로우'} | ||
</button> | ||
)} | ||
</div> | ||
<span css={timeStyle}>{notification.time}</span> | ||
</div> | ||
); | ||
}; | ||
|
||
export default NotificationItemComponent; |
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,54 @@ | ||
import { apiClient } from '@/api/fetch'; | ||
import { NotificationItem } from '@/pages/Alarm/type/alarmType'; | ||
|
||
export const notificationAPI = { | ||
// 알림 목록 가져오기 | ||
getNotifications: () => apiClient.get<NotificationItem[]>('/notifications'), | ||
|
||
// 알림 읽음 처리 | ||
markNotificationAsRead: (notificationId: number) => | ||
apiClient.put(`/notifications/${notificationId}/read`), | ||
|
||
// 모든 알림 읽음 처리 | ||
markAllNotificationsAsRead: () => apiClient.put('/notifications/read-all'), | ||
}; | ||
|
||
//알림 호출 | ||
export const fetchNotifications = async (): Promise<NotificationItem[]> => { | ||
try { | ||
const response = await notificationAPI.getNotifications(); | ||
console.log('알림 데이터: ', response.data); // 응답 데이터 확인 | ||
return response.data || []; | ||
} catch (error) { | ||
console.error('알림 조회 중 오류 발생: ', error); // 에러 로그 | ||
return []; | ||
} | ||
}; | ||
|
||
//알림 읽음 처리 | ||
export const markNotificationAsRead = async (notificationId: number): Promise<void> => { | ||
try { | ||
const response = await notificationAPI.markNotificationAsRead(notificationId); | ||
if (response.status === 200) { | ||
console.log(`알림 ${notificationId} 읽음 처리 완료`); | ||
} else { | ||
console.error(`알림 ${notificationId} 읽음 처리 실패`, response); | ||
} | ||
} catch (error) { | ||
console.error('알림 읽음 처리 중 오류 발생:', error); | ||
} | ||
}; | ||
|
||
//알림 모두읽음 처리 | ||
export const markAllNotificationsAsRead = async (): Promise<void> => { | ||
try { | ||
const response = await notificationAPI.markAllNotificationsAsRead(); | ||
if (response.status === 200) { | ||
console.log('모든 알림 읽음 처리 완료'); | ||
} else { | ||
console.error('모든 알림 읽음 처리 실패', response); | ||
} | ||
} catch (error) { | ||
console.error('모든 알림 읽음 처리 중 오류 발생:', error); | ||
} | ||
}; |
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
Oops, something went wrong.