Skip to content

Commit

Permalink
Merge pull request #112 from pagers-org/carpe/home-popular-tags
Browse files Browse the repository at this point in the history
feat(home): 홈 페이지의 인기 아티클 태그 목록 실제 데이터를 조회 후 연동하도록 합니다.
  • Loading branch information
innocarpe authored Sep 17, 2023
2 parents 49afe51 + 57f1057 commit 36b3d97
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 20 deletions.
19 changes: 19 additions & 0 deletions apps/react-world/src/apis/article/PopularArticleTagService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { isAxiosError } from 'axios';
import { api } from '../apiInstances';
import type { PopularArticleTagsResponse } from './PopularArticleTagService.types';

class PopularArticleTagService {
static async fetchPopularArticleTags(): Promise<PopularArticleTagsResponse> {
try {
const response = await api.get('/tags');
return response.data;
} catch (error) {
if (isAxiosError(error) && error.response) {
throw error.response.data;
}
throw error;
}
}
}

export default PopularArticleTagService;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface PopularArticleTagsResponse {
tags: string[];
}
31 changes: 12 additions & 19 deletions apps/react-world/src/components/home/HomeFeedContents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,28 @@ import HomeFeedTab from './HomeFeedTab';
import Pagination from './Pagination';
import useArticlePreviewQuery from '../../quries/useArticlePreviewQuery';
import { ARTICLE_PREVIEW_FETCH_LIMIT } from '../../apis/article/ArticlePreviewService';
import usePopularArticleTagsQuery from '../../quries/usePopularArticleTagsQuery';

const HomeFeedContents = () => {
const [currentPageIndex, setCurrentPageIndex] = useState(0);
const { data, isLoading } = useArticlePreviewQuery(currentPageIndex);

const { articlePreviews, isArticlePreviewsLoading } =
useArticlePreviewQuery(currentPageIndex);
const { popularArticleTags, isPopularArticleTagsLoading } =
usePopularArticleTagsQuery();
const handlePageChange = (newPageIndex: number) => {
setCurrentPageIndex(newPageIndex);
};

const totalPageCount = data?.articlesCount
? Math.ceil(data.articlesCount / ARTICLE_PREVIEW_FETCH_LIMIT)
const totalPageCount = articlePreviews?.articlesCount
? Math.ceil(articlePreviews.articlesCount / ARTICLE_PREVIEW_FETCH_LIMIT)
: 0;

return (
<Container>
<div className="row">
<div className="col-md-9">
<HomeFeedTab activeFeed="global_feed" />
{isLoading ? (
{isArticlePreviewsLoading ? (
<span
style={{
display: 'inline-block',
Expand All @@ -35,7 +38,7 @@ const HomeFeedContents = () => {
</span>
) : (
<>
{data?.articles?.map(articlePreview => (
{articlePreviews?.articles?.map(articlePreview => (
<ArticlePreview
key={articlePreview.slug}
articlePreview={articlePreview}
Expand All @@ -49,19 +52,9 @@ const HomeFeedContents = () => {
</>
)}
</div>

<PopularArticleTagList
tags={[
'programming',
'javascript',
'emberjs',
'angularjs',
'react',
'mean',
'node',
'rails',
]}
/>
{isPopularArticleTagsLoading ? null : (
<PopularArticleTagList tags={popularArticleTags?.tags ?? []} />
)}
</div>
</Container>
);
Expand Down
7 changes: 6 additions & 1 deletion apps/react-world/src/quries/useArticlePreviewQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ export const ARTICLE_PREVIEW_CACHE_KEY = '@article/preview';

const useArticlePreviewQuery = (pageNumber: number) => {
ArticlePreviewService;
return useQuery(
const queryResult = useQuery(
[ARTICLE_PREVIEW_CACHE_KEY, pageNumber], // 페이지 번호를 조합해 QueryKey 지정
() => ArticlePreviewService.fetchArticlePreviews(pageNumber),
);

return {
articlePreviews: queryResult.data,
isArticlePreviewsLoading: queryResult.isLoading,
};
};

export default useArticlePreviewQuery;
18 changes: 18 additions & 0 deletions apps/react-world/src/quries/usePopularArticleTagsQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { useQuery } from '@tanstack/react-query';
import PopularArticleTagService from '../apis/article/PopularArticleTagService';

export const POPULAR_ARTICLE_TAG_CACHE_KEY = '@article/popular_tags';

const usePopularArticleTagsQuery = () => {
PopularArticleTagService;
const queryResult = useQuery([POPULAR_ARTICLE_TAG_CACHE_KEY], () =>
PopularArticleTagService.fetchPopularArticleTags(),
);

return {
popularArticleTags: queryResult.data,
isPopularArticleTagsLoading: queryResult.isLoading,
};
};

export default usePopularArticleTagsQuery;

0 comments on commit 36b3d97

Please sign in to comment.