-
Notifications
You must be signed in to change notification settings - Fork 4
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
카테고리별 게시글 조회 기능을 위해 getPostList 함수 수정 이후 usePostList 훅 추가 기능 구현 #113
Changes from 2 commits
ad44f0c
b6282b4
dccc6c2
d3b71b2
af0e438
ad86c28
e8a4b52
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,10 +4,11 @@ import type { PostSorting, PostStatus } from '@components/post/PostListPage/type | |
|
||
import { getFetch } from '@utils/fetch'; | ||
|
||
interface GetPostList { | ||
interface PostListByOption { | ||
postStatus: PostStatus; | ||
postSorting: PostSorting; | ||
pageNumber: number; | ||
categoryId?: number; | ||
} | ||
|
||
const REQUEST_STATUS_OPTION: Record<PostStatus, string> = { | ||
|
@@ -21,13 +22,27 @@ const REQUEST_SORTING_OPTION: Record<PostSorting, string> = { | |
popular: 'HOT', | ||
}; | ||
|
||
export const getPostList = async ({ postStatus, postSorting, pageNumber }: GetPostList) => { | ||
const getPostListUrl = ({ categoryId, postStatus, postSorting, pageNumber }: PostListByOption) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 개인적인 취향이지만 가지고 오는 것보다는 만드는 것에 가깝다고 생각이 들어서 const makePostListUrl = ({ categoryId, postStatus, postSorting, pageNumber }: PostListByOption) => { 어떤지 궁금합니다! 그냥 선호여서 넘어가도 좋아요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 수아의 제안대로 get보다는 make가 더 어울리는 것 같아 수정해보았습니다 💯💯💯 export const makePostListUrl = ({
categoryId,
postStatus,
postSorting,
pageNumber,
}: PostListByOption) => {
const requestedStatus = REQUEST_STATUS_OPTION[postStatus];
const requestedSorting = REQUEST_SORTING_OPTION[postSorting];
const OPTION_URL = `status=${requestedStatus}&sorting=${requestedSorting}&pages=${pageNumber}`;
if (categoryId) {
return `/posts?categoryId=${categoryId}&${OPTION_URL}`;
}
return `/posts?${OPTION_URL}`;
}; |
||
const requestedStatus = REQUEST_STATUS_OPTION[postStatus]; | ||
const requestedSorting = REQUEST_SORTING_OPTION[postSorting]; | ||
|
||
const postList = await getFetch<PostInfo[]>( | ||
`/posts?status=${requestedStatus}&sorting=${requestedSorting}&pages=${pageNumber}` | ||
); | ||
if (categoryId) { | ||
return `/posts?categoryId=${categoryId}&status=${requestedStatus}&sorting=${requestedSorting}&pages=${pageNumber}`; | ||
} | ||
|
||
return `/posts?status=${requestedStatus}&sorting=${requestedSorting}&pages=${pageNumber}`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 위에 if문 return값과 중복되는 url 코드는 분리해도 되겠네요! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 아래와 같이 수정해보았습니다~ export const getPostListUrl = ({
categoryId,
postStatus,
postSorting,
pageNumber,
}: PostListByOption) => {
const requestedStatus = REQUEST_STATUS_OPTION[postStatus];
const requestedSorting = REQUEST_SORTING_OPTION[postSorting];
const OPTION_URL = `status=${requestedStatus}&sorting=${requestedSorting}&pages=${pageNumber}`;
if (categoryId) {
return `/posts?categoryId=${categoryId}&${OPTION_URL}`;
}
return `/posts?${OPTION_URL}`;
}; |
||
}; | ||
|
||
export const getPostList = async ({ | ||
postStatus, | ||
postSorting, | ||
pageNumber, | ||
categoryId, | ||
}: PostListByOption) => { | ||
const postListUrl = getPostListUrl({ pageNumber, postSorting, postStatus, categoryId }); | ||
|
||
const postList = await getFetch<PostInfo[]>(postListUrl); | ||
|
||
return { | ||
pageNumber, | ||
postList, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,8 @@ import Skeleton from '@components/common/Skeleton'; | |
import { SORTING_OPTION, STATUS_OPTION } from '@components/post/PostListPage/constants'; | ||
import type { PostSorting, PostStatus } from '@components/post/PostListPage/types'; | ||
|
||
import { CATEGORY_ID } from '@constants/post'; | ||
|
||
import * as S from './style'; | ||
|
||
export default function PostList() { | ||
|
@@ -23,9 +25,14 @@ export default function PostList() { | |
const { selectedOption: selectedSortingOption, handleOptionChange: handleSortingOptionChange } = | ||
useSelect<PostSorting>('latest'); | ||
|
||
const URL = new URLSearchParams(window.location.search); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. react-router 가 제공하는 useSearchParams 로 categoryId값을 가져오지 않는 이유가 있으신가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 기능이 있는지 몰랐어요 알려주셔서 감사합니다 제로 🔥🔥🔥 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이해가 부족해 여쭤보려고 남깁니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 단순히 /?categoryId=1와 같이 api를 따라서 생각했는데요. 사용자들에게 보여줄때는 /posts/category/1 이런식으로도 보여줄 수 있어서 내일 같이 의논해보면 좋겠어요 👍👍👍 |
||
|
||
const categoryId = URL.get(CATEGORY_ID); | ||
|
||
const { data, fetchNextPage, hasNextPage, isFetchingNextPage } = usePostList({ | ||
postSorting: selectedSortingOption, | ||
postStatus: selectedStatusOption, | ||
categoryId: Number(categoryId), | ||
}); | ||
|
||
useEffect(() => { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export const POST = { | ||
NOT_VOTE: 0, | ||
}; | ||
|
||
export const CATEGORY_ID = 'categoryId'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 해당 부분을 상수화한 이유가 궁금합니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네 수아의 말처럼 api를 기반으로 url을 유추해서 로직을 구성했습니다. 단순히 제가 오타를 낼까봐 만든 상수였기에 사용되는 PostList 컴포넌트 폴더의 constants 파일로 옮기겠습니다 😵😵😵 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { example } from './example/get'; | ||
|
||
import { mockVoteResult } from './getVoteDetail'; | ||
import { mockPost } from './post'; | ||
import { mockVote } from './vote'; | ||
import { mockPostList } from './wus/post'; | ||
|
||
export const handlers = [...example, ...mockPost, ...mockVoteResult, ...mockVote]; | ||
export const handlers = [...example, ...mockPost, ...mockVoteResult, ...mockVote, ...mockPostList]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍👍👍