diff --git a/src/apis/getRecentComments.ts b/src/apis/getRecentComments.ts new file mode 100644 index 00000000..c563173b --- /dev/null +++ b/src/apis/getRecentComments.ts @@ -0,0 +1,13 @@ +import type { GetRecentCommentsResponseType } from '@/schema/recentcomment'; +import httpClient from './index'; + +const getRecentComments = async (): Promise => { + const response = await httpClient.get('/comments', { + params: { + limit: 3, + }, + }); + return response.data; +}; + +export default getRecentComments; diff --git a/src/components/Card/CommentCard.tsx b/src/components/Card/CommentCard.tsx index 3975f828..9c08f6d0 100644 --- a/src/components/Card/CommentCard.tsx +++ b/src/components/Card/CommentCard.tsx @@ -3,7 +3,9 @@ import Image from 'next/image'; import { CommentCardProps } from '@/types/CommentCardTypes'; import { sizeStyles, textSizeStyles, gapStyles, paddingStyles, contentWidthStyles } from '@/styles/CommentCardStyles'; -function CommentCard({ status }: CommentCardProps) { +function CommentCard({ writer, content, createdAt, status }: CommentCardProps) { + const formattedDate = createdAt.toLocaleString(); + return (
- 프로필 이미지{' '} + 프로필 이미지
-
지킬과 하이드
-
1시간 전
+
{writer.nickname}
+
{formattedDate}
{status === 'edit' && (
@@ -30,7 +32,7 @@ function CommentCard({ status }: CommentCardProps) {
- 오늘 하루 우울했었는데 덕분에 많은 힘 얻고 갑니다. 연금술사 책 다시 사서 오랜만에 읽어봐야겠어요! + {content}
diff --git a/src/components/main/RecentComment.tsx b/src/components/main/RecentComment.tsx new file mode 100644 index 00000000..85d18022 --- /dev/null +++ b/src/components/main/RecentComment.tsx @@ -0,0 +1,19 @@ +import React from 'react'; +import useGetRecentComments from '@/hooks/useGetRecentComments'; +import CommentCard from '@/components/Card/CommentCard'; + +function RecentComments() { + const { data, error, isLoading } = useGetRecentComments(); + + if (isLoading) return

로딩 중...

; + if (error) return

{error.message}

; + + return ( +
+

최신 댓글

+ {data?.list.map((comment) => )} +
+ ); +} + +export default RecentComments; diff --git a/src/hooks/useGetRecentComments.ts b/src/hooks/useGetRecentComments.ts new file mode 100644 index 00000000..45362e38 --- /dev/null +++ b/src/hooks/useGetRecentComments.ts @@ -0,0 +1,11 @@ +import { useQuery } from '@tanstack/react-query'; +import getRecentComments from '@/apis/getRecentComments'; +import { GetRecentCommentsResponseType } from '@/schema/recentcomment'; + +const useGetRecentComments = () => + useQuery({ + queryKey: ['recentComments', 3], + queryFn: getRecentComments, + }); + +export default useGetRecentComments; diff --git a/src/schema/recentcomment.ts b/src/schema/recentcomment.ts new file mode 100644 index 00000000..524c0473 --- /dev/null +++ b/src/schema/recentcomment.ts @@ -0,0 +1,27 @@ +import * as z from 'zod'; + +const WriterSchema = z.object({ + image: z.string().url(), + nickname: z.string(), + id: z.number(), +}); + +const CommentSchema = z.object({ + epigramId: z.number(), + writer: WriterSchema, + updatedAt: z.coerce.date(), + createdAt: z.coerce.date(), + isPrivate: z.boolean(), + content: z.string(), + id: z.number(), +}); + +export const GetRecentCommentsResponseSchema = z.object({ + totalCount: z.number(), + nextCursor: z.number(), + list: z.array(CommentSchema), +}); + +export type WriterType = z.infer; +export type CommentType = z.infer; +export type GetRecentCommentsResponseType = z.infer; diff --git a/src/types/CommentCardTypes.ts b/src/types/CommentCardTypes.ts index abb0a28b..8d0f1437 100644 --- a/src/types/CommentCardTypes.ts +++ b/src/types/CommentCardTypes.ts @@ -1,3 +1,8 @@ +import { WriterType } from '@/schema/recentcomment'; + export interface CommentCardProps { - status: 'edit' | 'complete'; + writer: WriterType; + content: string; + createdAt: Date; + status?: 'view' | 'edit'; }