Skip to content

Commit bd7f818

Browse files
committed
chore(services): recomment 관련 로직 추가정의
1 parent a9fe7c2 commit bd7f818

File tree

3 files changed

+150
-48
lines changed

3 files changed

+150
-48
lines changed

src/page/forum-post/services/api.service.ts

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,18 @@ interface IGetPostCommentListProps {
66
pageParam: number;
77
}
88

9-
export const getPostCommentList = async ({
10-
id,
11-
pageParam: page,
12-
}: IGetPostCommentListProps) => {
13-
const { data } = await httpClient.comment.getById({ params: { id, page } });
14-
return data;
15-
};
16-
17-
interface ICreatePostCommentProps {
18-
id: string;
19-
detail: string;
9+
interface IGetRecommentListProps {
10+
id: number;
11+
pageParam: number;
2012
}
2113

22-
export const createPostComment = async ({
23-
id,
24-
detail,
25-
}: ICreatePostCommentProps) => {
26-
const { data } = await httpClient.comment.postById(
27-
{ detail },
28-
{ params: { id } },
29-
);
30-
return data;
31-
};
32-
33-
interface IUpdatePostCommentProps {
14+
interface IPostCommentProps {
3415
id: number;
3516
detail: string;
3617
}
3718

19+
// like
20+
3821
export const updatePostLike = async (id: string) => {
3922
const { data } = await httpClient.like.put({
4023
type: LIKE.POST,
@@ -59,7 +42,25 @@ export const updateRecommentLike = async (id: number) => {
5942
return data;
6043
};
6144

62-
export const updatePostComment = async (comment: IUpdatePostCommentProps) => {
45+
// comment
46+
47+
export const getPostCommentList = async ({
48+
id,
49+
pageParam: page,
50+
}: IGetPostCommentListProps) => {
51+
const { data } = await httpClient.comment.getById({ params: { id, page } });
52+
return data;
53+
};
54+
55+
export const createPostComment = async ({ id, detail }: IPostCommentProps) => {
56+
const { data } = await httpClient.comment.postById(
57+
{ detail },
58+
{ params: { id } },
59+
);
60+
return data;
61+
};
62+
63+
export const updatePostComment = async (comment: IPostCommentProps) => {
6364
const { data } = await httpClient.comment.put(comment);
6465
return data;
6566
};
@@ -70,3 +71,35 @@ export const deletePostComment = async (id: number) => {
7071
});
7172
return data;
7273
};
74+
75+
// recomment
76+
77+
export const getRecommentList = async ({
78+
id,
79+
pageParam: page,
80+
}: IGetRecommentListProps) => {
81+
const { data } = await httpClient.recomment.getById({ params: { id, page } });
82+
return data;
83+
};
84+
85+
export const createRecomment = async ({ id, detail }: IPostCommentProps) => {
86+
const { data } = await httpClient.recomment.postById(
87+
{ detail },
88+
{
89+
params: { id },
90+
},
91+
);
92+
return data;
93+
};
94+
95+
export const updateRecomment = async (comment: IPostCommentProps) => {
96+
const { data } = await httpClient.recomment.put(comment);
97+
return data;
98+
};
99+
100+
export const deleteRecomment = async (id: number) => {
101+
const { data } = await httpClient.recomment.deleteById({
102+
params: { id },
103+
});
104+
return data;
105+
};

src/page/forum-post/services/mutation.service.ts

Lines changed: 73 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,46 +9,63 @@ import { useRouter } from "next/navigation";
99
import { toast } from "react-toastify";
1010
import {
1111
createPostComment,
12+
createRecomment,
1213
deletePostComment,
14+
deleteRecomment,
1315
updateCommentLike,
1416
updatePostComment,
1517
updatePostLike,
18+
updateRecomment,
19+
updateRecommentLike,
1620
} from "./api.service";
1721

18-
export const useDeletePostMutation = () => {
22+
interface IUsePostCommentMutationProps {
23+
id: number;
24+
detail: string;
25+
}
26+
27+
// like
28+
29+
export const useUpdatePostLikeMutation = () => {
1930
const apolloClient = useApolloClient();
20-
const router = useRouter();
2131

22-
const mutations = useApolloMutation(DELETE_POST, {
23-
onCompleted: () => {
32+
return useMutation((id: string) => updatePostLike(id), {
33+
onSuccess: () => {
2434
apolloClient.cache.reset();
25-
toast.success("글이 삭제되었습니다!");
26-
router.push(ROUTER.POST.LIST);
2735
},
2836
});
29-
return mutations;
3037
};
3138

32-
interface IUseCreateCommentMutationProps {
33-
id: string;
34-
detail: string;
35-
}
39+
export const useUpdateCommentLikeMutation = () => {
40+
return useMutation((id: number) => updateCommentLike(id));
41+
};
3642

37-
export const useUpdatePostLikeMutation = () => {
43+
export const useUpdateRecommentLikeMutation = () => {
44+
return useMutation((id: number) => updateRecommentLike(id));
45+
};
46+
47+
// post
48+
export const useDeletePostMutation = () => {
3849
const apolloClient = useApolloClient();
50+
const router = useRouter();
3951

40-
return useMutation((id: string) => updatePostLike(id), {
41-
onSuccess: () => {
52+
const mutations = useApolloMutation(DELETE_POST, {
53+
onCompleted: () => {
4254
apolloClient.cache.reset();
55+
toast.success("글이 삭제되었습니다!");
56+
router.push(ROUTER.POST.LIST);
4357
},
4458
});
59+
return mutations;
4560
};
4661

62+
// comment
63+
4764
export const useCreatePostCommentMutation = () => {
4865
const queryClient = useQueryClient();
4966

5067
return useMutation(
51-
(props: IUseCreateCommentMutationProps) => createPostComment(props),
68+
(comment: IUsePostCommentMutationProps) => createPostComment(comment),
5269
{
5370
onSuccess: () => {
5471
toast.success("댓글을 작성했어요!");
@@ -58,16 +75,11 @@ export const useCreatePostCommentMutation = () => {
5875
);
5976
};
6077

61-
interface IUseUpdatePostCommentMutationProps {
62-
id: number;
63-
detail: string;
64-
}
65-
6678
export const useUpdatePostCommentMutation = () => {
6779
const queryClient = useQueryClient();
6880

6981
return useMutation(
70-
(comment: IUseUpdatePostCommentMutationProps) => updatePostComment(comment),
82+
(comment: IUsePostCommentMutationProps) => updatePostComment(comment),
7183
{
7284
onSuccess: () => {
7385
toast.success("댓글이 수정되었어요!");
@@ -88,6 +100,44 @@ export const useDeletePostCommentMutation = () => {
88100
});
89101
};
90102

91-
export const useUpdateCommentLikeMutation = () => {
92-
return useMutation((id: number) => updateCommentLike(id));
103+
// recomment
104+
105+
export const useCreateRecommentMutation = () => {
106+
const queryClient = useQueryClient();
107+
108+
return useMutation(
109+
(comment: IUsePostCommentMutationProps) => createRecomment(comment),
110+
{
111+
onSuccess: () => {
112+
toast.success("답글을 작성했어요!");
113+
queryClient.invalidateQueries([KEY.RECOMMENT]);
114+
queryClient.invalidateQueries([KEY.COMMENT]);
115+
},
116+
},
117+
);
118+
};
119+
120+
export const useUpdateRecommentMutation = () => {
121+
const queryClient = useQueryClient();
122+
123+
return useMutation(
124+
(comment: IUsePostCommentMutationProps) => updateRecomment(comment),
125+
{
126+
onSuccess: () => {
127+
toast.success("답글이 수정되었어요!");
128+
queryClient.invalidateQueries([KEY.RECOMMENT]);
129+
},
130+
},
131+
);
132+
};
133+
134+
export const useDeleteRecommentMutation = () => {
135+
const queryClient = useQueryClient();
136+
137+
return useMutation((id: number) => deleteRecomment(id), {
138+
onSuccess: () => {
139+
toast.success("답글이 삭제되었어요.");
140+
queryClient.invalidateQueries([KEY.RECOMMENT]);
141+
},
142+
});
93143
};

src/page/forum-post/services/query.service.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { GET_POST } from "@/gql/post/queries";
33
import { useQuery as useApolloQuery } from "@apollo/client";
44
import { useInfiniteQuery } from "@tanstack/react-query";
55
import { KEY } from "@/constants";
6-
import { getPostCommentList } from "./api.service";
6+
import { getPostCommentList, getRecommentList } from "./api.service";
77

88
export const usePostQuery = ({ id }: IPostQuery) => {
99
const { data, ...queryRest } = useApolloQuery(GET_POST({ id }), {
@@ -17,6 +17,10 @@ interface IUseCommentListQueryProps {
1717
postId: string;
1818
}
1919

20+
interface IUseRecommentQueryProps {
21+
commentId: number;
22+
}
23+
2024
export const useCommentListQuery = ({
2125
postId: id,
2226
}: IUseCommentListQueryProps) => {
@@ -31,3 +35,18 @@ export const useCommentListQuery = ({
3135
});
3236
return { data: data?.pages, ...queryRest };
3337
};
38+
39+
export const useRecommentListQuery = ({
40+
commentId: id,
41+
}: IUseRecommentQueryProps) => {
42+
const { data, ...queryRest } = useInfiniteQuery({
43+
queryKey: [KEY.RECOMMENT],
44+
queryFn: ({ pageParam = 0 }) => getRecommentList({ id, pageParam }),
45+
getNextPageParam: (lastPage) => {
46+
return lastPage.currentPage !== lastPage.totalPage - 1
47+
? lastPage.currentPage + 1
48+
: undefined;
49+
},
50+
});
51+
return { data: data?.pages, ...queryRest };
52+
};

0 commit comments

Comments
 (0)