-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #132 from prgrms-web-devcourse-final-project/38-fe…
…ature/notification-modal-data-binding [Feature] #38 알림창 데이터 바인딩
- Loading branch information
Showing
8 changed files
with
182 additions
and
133 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { AxiosError } from 'axios' | ||
import { APIResponse, CommonAPIResponse, ErrorResponse, PaginationResponse } from '~types/api' | ||
import { axiosInstance } from '~apis/axiosInstance' | ||
|
||
export type FetchNotificationListRequest = { | ||
page: number | ||
} | ||
|
||
export type FetchNotificationListResponse = PaginationResponse & { | ||
content: Array<Pick<CommonAPIResponse, 'notificationId' | 'type' | 'content' | 'isRead' | 'memberId' | 'createdAt'>> | ||
} | ||
|
||
export const fetchNotificationList = async ( | ||
req: FetchNotificationListRequest | ||
): Promise<APIResponse<FetchNotificationListResponse>> => { | ||
try { | ||
const { data } = await axiosInstance.get<APIResponse<FetchNotificationListResponse>>(`/notification/list`, { | ||
params: req, | ||
}) | ||
return data | ||
} catch (error) { | ||
if (error instanceof AxiosError) { | ||
const { response } = error as AxiosError<ErrorResponse> | ||
|
||
if (response) { | ||
const { code, message } = response.data | ||
switch (code) { | ||
case 400: | ||
throw new Error(message || '잘못된 요청입니다.') | ||
case 401: | ||
throw new Error(message || '인증에 실패했습니다.') | ||
case 500: | ||
throw new Error(message || '서버 오류가 발생했습니다.') | ||
default: | ||
throw new Error(message || '알 수 없는 오류가 발생했습니다.') | ||
} | ||
} | ||
// 요청 자체가 실패한 경우 | ||
throw new Error('네트워크 연결을 확인해주세요') | ||
} | ||
|
||
console.error('예상치 못한 에러:', error) | ||
throw new 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useSuspenseInfiniteQuery } from '@tanstack/react-query' | ||
import { FetchNotificationListResponse, fetchNotificationList } from '~apis/notification/fetchNotificationList' | ||
import { queryKey } from '~constants/queryKey' | ||
import { APIResponse } from '~types/api' | ||
|
||
export default function useInfiniteNotificationList() { | ||
return useSuspenseInfiniteQuery<APIResponse<FetchNotificationListResponse>>({ | ||
queryKey: queryKey.notification(), | ||
queryFn: async ({ pageParam = 0 }) => { | ||
if (pageParam !== 0) await new Promise(resolve => setTimeout(resolve, 500)) | ||
return await fetchNotificationList({ page: pageParam as number }) | ||
}, | ||
getNextPageParam: lastPage => { | ||
return lastPage.data.last ? undefined : lastPage.data.number + 1 | ||
}, | ||
initialPageParam: 0, | ||
}) | ||
} |
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,8 @@ | ||
import { styled } from 'styled-components' | ||
|
||
type InfiniteScrollTriggerProps = { | ||
$height?: number | ||
} | ||
export const InfiniteScrollTrigger = styled.div<InfiniteScrollTriggerProps>` | ||
height: ${({ $height = 20 }) => $height + 'px'}; | ||
` |
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 |
---|---|---|
@@ -1,134 +1,29 @@ | ||
import useInfiniteNotificationList from '~apis/notification/useInfiniteNotificationList' | ||
import Loader from '~components/Loader' | ||
import NotificationItem from '~components/NotificationItem' | ||
import useObserver from '~hooks/useObserver' | ||
import * as S from './styles' | ||
import { InfiniteScrollTrigger } from '~components/InfiniteScrollTrigger' | ||
|
||
const notificationList = [ | ||
{ | ||
content: '엄마 산책 시켜주세요!', | ||
date: new Date('2024-11-21 09:38:04'), | ||
}, | ||
{ | ||
content: '오늘 빼먹지 말고 산책!', | ||
date: new Date('2024-11-21 07:13:04'), | ||
}, | ||
{ | ||
content: '다 함께 즐거운 순간!', | ||
date: new Date('2024-11-21 01:32:04'), | ||
}, | ||
{ | ||
content: '오늘 빼먹지 말고 산책!', | ||
date: new Date('2024-11-21 11:51:04'), | ||
}, | ||
{ | ||
content: '다 함께 즐거운 순간!', | ||
date: new Date('2024-11-21 06:26:04'), | ||
}, | ||
{ | ||
content: '오늘 빼먹지 말고 산책!', | ||
date: new Date('2024-11-21 18:00:04'), | ||
}, | ||
{ | ||
content: '오늘 빼먹지 말고 산책!', | ||
date: new Date('2024-11-21 04:24:04'), | ||
}, | ||
{ | ||
content: '오늘의 미션을 완료했어요!', | ||
date: new Date('2024-11-21 16:10:04'), | ||
}, | ||
{ | ||
content: '내일 날씨가 좋으면 산책해요!', | ||
date: new Date('2024-11-21 13:44:04'), | ||
}, | ||
{ | ||
content: '엄마 산책 시켜주세요!', | ||
date: new Date('2024-11-21 18:35:04'), | ||
}, | ||
{ | ||
content: '다 함께 즐거운 순간!', | ||
date: new Date('2024-11-21 20:32:04'), | ||
}, | ||
{ | ||
content: '내일 날씨가 좋으면 산책해요!', | ||
date: new Date('2024-11-21 20:29:04'), | ||
}, | ||
{ | ||
content: '이번 주 미션을 완료할 거예요!', | ||
date: new Date('2024-11-21 16:24:04'), | ||
}, | ||
{ | ||
content: '오늘 빼먹지 말고 산책!', | ||
date: new Date('2024-11-21 04:00:04'), | ||
}, | ||
{ | ||
content: '다 함께 즐거운 순간!', | ||
date: new Date('2024-11-21 20:15:04'), | ||
}, | ||
{ | ||
content: '오늘 기분이 어때요?', | ||
date: new Date('2024-11-21 13:07:04'), | ||
}, | ||
{ | ||
content: '엄마 나와 함께 놀아요!', | ||
date: new Date('2024-11-21 12:04:04'), | ||
}, | ||
{ | ||
content: '산책이 너무 좋아요!', | ||
date: new Date('2024-11-21 07:04:04'), | ||
}, | ||
{ | ||
content: '엄마 나와 함께 놀아요!', | ||
date: new Date('2024-11-21 07:22:04'), | ||
}, | ||
{ | ||
content: '오늘 기분이 어때요?', | ||
date: new Date('2024-11-21 09:32:04'), | ||
}, | ||
{ | ||
content: '오늘 빼먹지 말고 산책!', | ||
date: new Date('2024-11-21 14:29:04'), | ||
}, | ||
{ | ||
content: '엄마 나와 함께 놀아요!', | ||
date: new Date('2024-11-21 11:32:04'), | ||
}, | ||
{ | ||
content: '밥은 언제 먹어?', | ||
date: new Date('2024-11-21 23:44:04'), | ||
}, | ||
{ | ||
content: '내일 날씨가 좋으면 산책해요!', | ||
date: new Date('2024-11-21 01:33:04'), | ||
}, | ||
{ | ||
content: '다 함께 즐거운 순간!', | ||
date: new Date('2024-11-21 19:34:04'), | ||
}, | ||
{ | ||
content: '엄마 나와 함께 놀아요!', | ||
date: new Date('2024-11-21 13:40:04'), | ||
}, | ||
{ | ||
content: '이번 주 미션을 완료할 거예요!', | ||
date: new Date('2024-11-21 14:05:04'), | ||
}, | ||
{ | ||
content: '밥은 언제 먹어?', | ||
date: new Date('2024-11-21 01:02:04'), | ||
}, | ||
{ | ||
content: '이번 주 미션을 완료할 거예요!', | ||
date: new Date('2024-11-21 04:02:04'), | ||
}, | ||
{ | ||
content: '엄마 산책 시켜주세요!', | ||
date: new Date('2024-11-21 22:11:04'), | ||
}, | ||
] | ||
export default function NotificationList() { | ||
const { observerRef } = useObserver<HTMLDivElement>({ | ||
callback: () => hasNextPage && !isFetchingNextPage && fetchNextPage(), | ||
}) | ||
|
||
const { data, fetchNextPage, hasNextPage, isFetchingNextPage } = useInfiniteNotificationList() | ||
|
||
return ( | ||
<S.NotificationList> | ||
{notificationList.map((notification, index) => ( | ||
<NotificationItem content={notification.content} date={notification.date} key={index} /> | ||
))} | ||
{data?.pages.map(page => | ||
page.data?.content.map(notification => ( | ||
<NotificationItem | ||
key={notification.notificationId} | ||
content={notification.content} | ||
date={new Date(notification.createdAt)} | ||
/> | ||
)) | ||
)} | ||
<InfiniteScrollTrigger ref={observerRef}>{isFetchingNextPage && <Loader />}</InfiniteScrollTrigger> | ||
</S.NotificationList> | ||
) | ||
} |
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,32 @@ | ||
import { useRef, useCallback, useEffect } from 'react' | ||
|
||
type UseObserverProps = { | ||
callback: () => void | ||
} | ||
|
||
export default function useObserver<T extends HTMLElement>({ callback }: UseObserverProps) { | ||
const observerRef = useRef<T>(null) | ||
const handleObserver = useCallback( | ||
(entries: IntersectionObserverEntry[]) => { | ||
const [target] = entries | ||
if (target.isIntersecting) { | ||
callback() | ||
} | ||
}, | ||
[callback] | ||
) | ||
|
||
useEffect(() => { | ||
const element = observerRef.current | ||
const option = { threshold: 0.5 } | ||
|
||
const observer = new IntersectionObserver(handleObserver, option) | ||
if (element) observer.observe(element) | ||
|
||
return () => { | ||
if (element) observer.unobserve(element) | ||
} | ||
}, [handleObserver]) | ||
|
||
return { observerRef } | ||
} |
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