Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(home): 홈 페이지의 인기 아티클 태그 목록 실제 데이터를 조회 후 연동하도록 합니다. #112

Merged
merged 4 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 PR 부터 두 개의 useQuery 를 사용하게 되어서, 네이밍을 좀 더 구체화하는게 가독성을 높힐 수 있다고 생각했습니다.

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;