Skip to content

Commit 98fb1bd

Browse files
committed
refactor: 프로필까지 같이 받아오기
1 parent 7c60232 commit 98fb1bd

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/features/post-load/api/post-load.api.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ export const postLoadApi = {
2222
}));
2323
return { posts: sortPosts(postsWithUsers, params?.sortBy, params?.order), total };
2424
},
25+
async searchWithAuthors(
26+
query: string,
27+
params?: Pick<PostsParams, "limit" | "skip" | "sortBy" | "order">
28+
): Promise<PostsResponse> {
29+
const [{ posts, total }, users] = await Promise.all([postApi.search(query, params), userApi.getProfile()]);
30+
const postsWithUsers: Post[] = posts.map((post) => ({
31+
...post,
32+
author: users.find((user) => user.id === post.userId),
33+
}));
34+
return { posts: sortPosts(postsWithUsers, params?.sortBy, params?.order), total };
35+
},
2536
} as const;
2637

2738
function sortPosts(posts: Post[], sortBy?: string, order: "asc" | "desc" = "asc"): Post[] {

src/features/post-load/model/post-load.query.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const postsByTagQuery = (tag: string, params?: Pick<PostsParams, "limit"
2323

2424
export const postsSearchQuery = (query: string, params?: Pick<PostsParams, "limit" | "skip" | "sortBy" | "order">) => ({
2525
queryKey: postsQueryKeys.search(query, params),
26-
queryFn: (): Promise<PostsResponse> => postApi.search(query, params),
26+
queryFn: (): Promise<PostsResponse> => postLoadApi.searchWithAuthors(query, params),
2727
});
2828

2929
export type BuildPostsQueryParams = {

0 commit comments

Comments
 (0)