Skip to content

Commit 36b3d97

Browse files
authored
Merge pull request #112 from pagers-org/carpe/home-popular-tags
feat(home): 홈 페이지의 인기 아티클 태그 목록 실제 데이터를 조회 후 연동하도록 합니다.
2 parents 49afe51 + 57f1057 commit 36b3d97

File tree

5 files changed

+58
-20
lines changed

5 files changed

+58
-20
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { isAxiosError } from 'axios';
2+
import { api } from '../apiInstances';
3+
import type { PopularArticleTagsResponse } from './PopularArticleTagService.types';
4+
5+
class PopularArticleTagService {
6+
static async fetchPopularArticleTags(): Promise<PopularArticleTagsResponse> {
7+
try {
8+
const response = await api.get('/tags');
9+
return response.data;
10+
} catch (error) {
11+
if (isAxiosError(error) && error.response) {
12+
throw error.response.data;
13+
}
14+
throw error;
15+
}
16+
}
17+
}
18+
19+
export default PopularArticleTagService;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export interface PopularArticleTagsResponse {
2+
tags: string[];
3+
}

apps/react-world/src/components/home/HomeFeedContents.tsx

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,28 @@ import HomeFeedTab from './HomeFeedTab';
66
import Pagination from './Pagination';
77
import useArticlePreviewQuery from '../../quries/useArticlePreviewQuery';
88
import { ARTICLE_PREVIEW_FETCH_LIMIT } from '../../apis/article/ArticlePreviewService';
9+
import usePopularArticleTagsQuery from '../../quries/usePopularArticleTagsQuery';
910

1011
const HomeFeedContents = () => {
1112
const [currentPageIndex, setCurrentPageIndex] = useState(0);
12-
const { data, isLoading } = useArticlePreviewQuery(currentPageIndex);
13-
13+
const { articlePreviews, isArticlePreviewsLoading } =
14+
useArticlePreviewQuery(currentPageIndex);
15+
const { popularArticleTags, isPopularArticleTagsLoading } =
16+
usePopularArticleTagsQuery();
1417
const handlePageChange = (newPageIndex: number) => {
1518
setCurrentPageIndex(newPageIndex);
1619
};
1720

18-
const totalPageCount = data?.articlesCount
19-
? Math.ceil(data.articlesCount / ARTICLE_PREVIEW_FETCH_LIMIT)
21+
const totalPageCount = articlePreviews?.articlesCount
22+
? Math.ceil(articlePreviews.articlesCount / ARTICLE_PREVIEW_FETCH_LIMIT)
2023
: 0;
2124

2225
return (
2326
<Container>
2427
<div className="row">
2528
<div className="col-md-9">
2629
<HomeFeedTab activeFeed="global_feed" />
27-
{isLoading ? (
30+
{isArticlePreviewsLoading ? (
2831
<span
2932
style={{
3033
display: 'inline-block',
@@ -35,7 +38,7 @@ const HomeFeedContents = () => {
3538
</span>
3639
) : (
3740
<>
38-
{data?.articles?.map(articlePreview => (
41+
{articlePreviews?.articles?.map(articlePreview => (
3942
<ArticlePreview
4043
key={articlePreview.slug}
4144
articlePreview={articlePreview}
@@ -49,19 +52,9 @@ const HomeFeedContents = () => {
4952
</>
5053
)}
5154
</div>
52-
53-
<PopularArticleTagList
54-
tags={[
55-
'programming',
56-
'javascript',
57-
'emberjs',
58-
'angularjs',
59-
'react',
60-
'mean',
61-
'node',
62-
'rails',
63-
]}
64-
/>
55+
{isPopularArticleTagsLoading ? null : (
56+
<PopularArticleTagList tags={popularArticleTags?.tags ?? []} />
57+
)}
6558
</div>
6659
</Container>
6760
);

apps/react-world/src/quries/useArticlePreviewQuery.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ export const ARTICLE_PREVIEW_CACHE_KEY = '@article/preview';
55

66
const useArticlePreviewQuery = (pageNumber: number) => {
77
ArticlePreviewService;
8-
return useQuery(
8+
const queryResult = useQuery(
99
[ARTICLE_PREVIEW_CACHE_KEY, pageNumber], // 페이지 번호를 조합해 QueryKey 지정
1010
() => ArticlePreviewService.fetchArticlePreviews(pageNumber),
1111
);
12+
13+
return {
14+
articlePreviews: queryResult.data,
15+
isArticlePreviewsLoading: queryResult.isLoading,
16+
};
1217
};
1318

1419
export default useArticlePreviewQuery;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { useQuery } from '@tanstack/react-query';
2+
import PopularArticleTagService from '../apis/article/PopularArticleTagService';
3+
4+
export const POPULAR_ARTICLE_TAG_CACHE_KEY = '@article/popular_tags';
5+
6+
const usePopularArticleTagsQuery = () => {
7+
PopularArticleTagService;
8+
const queryResult = useQuery([POPULAR_ARTICLE_TAG_CACHE_KEY], () =>
9+
PopularArticleTagService.fetchPopularArticleTags(),
10+
);
11+
12+
return {
13+
popularArticleTags: queryResult.data,
14+
isPopularArticleTagsLoading: queryResult.isLoading,
15+
};
16+
};
17+
18+
export default usePopularArticleTagsQuery;

0 commit comments

Comments
 (0)