Skip to content

Commit

Permalink
feat: (#275) 게스트용 게시글 api 제작 및 연결
Browse files Browse the repository at this point in the history
- 상세페이지에서 로그인 정보가 false라면 게스트용 게시글 상세정보 api로 fetch
  • Loading branch information
chsua committed Aug 9, 2023
1 parent d96d620 commit c3393a8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
6 changes: 6 additions & 0 deletions frontend/src/api/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ export const getPost = async (postId: number): Promise<PostInfo> => {
return transformPostResponse(post);
};

export const getPostForGuest = async (postId: number): Promise<PostInfo> => {
const post = await getFetch<PostInfoResponse>(`${BASE_URL}/posts/${postId}/guest`);

return transformPostResponse(post);
};

export const createPost = async (newPost: FormData) => {
return await multiPostFetch(`${BASE_URL}/posts`, newPost);
};
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/comment/CommentList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import * as S from './style';

interface CommentListProps {
commentList: Comment[];
memberId: number;
memberId?: number;
isGuest: boolean;
postWriterName: string;
}
Expand Down
8 changes: 5 additions & 3 deletions frontend/src/hooks/query/post/usePostDetail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import { useQuery } from '@tanstack/react-query';

import { PostInfo } from '@type/post';

import { getPost } from '@api/post';
import { getPost, getPostForGuest } from '@api/post';

import { QUERY_KEY } from '@constants/queryKey';

export const usePostDetail = (postId: number) => {
export const usePostDetail = (isGuest: boolean, postId: number) => {
const fetchApi = isGuest ? getPostForGuest : getPost;

const { data, isError, isLoading } = useQuery<PostInfo>(
[QUERY_KEY.POST_DETAIL, postId],
() => getPost(postId),
() => fetchApi(postId),
{
onSuccess: data => {
return data;
Expand Down
14 changes: 6 additions & 8 deletions frontend/src/pages/post/PostDetail/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useContext } from 'react';
import { useNavigate, useParams } from 'react-router-dom';

import { AuthContext } from '@hooks/context/auth';
import { useCommentList } from '@hooks/query/comment/useCommentList';
import { usePostDetail } from '@hooks/query/post/usePostDetail';

Expand All @@ -10,7 +12,6 @@ import Layout from '@components/common/Layout';
import NarrowTemplateHeader from '@components/common/NarrowTemplateHeader';
import Post from '@components/common/Post';

import { getCookieToken, getMemberId } from '@utils/cookie';
import { checkClosedPost } from '@utils/time';

import BottomButtonPart from './BottomButtonPart';
Expand All @@ -23,14 +24,11 @@ export default function PostDetailPage() {
const params = useParams() as { postId: string };
const postId = Number(params.postId);

const token = getCookieToken().accessToken;
const decodedPayload = getMemberId(token);
const memberId = decodedPayload.memberId;
const { loggedInfo } = useContext(AuthContext);
const memberId = loggedInfo.id;

const { data: postData, isError, isLoading } = usePostDetail(postId);
// const { data: postData, errorMessage, isLoading, refetch } = useFetch(() => getPost(postId));
const { data: postData, isError, isLoading } = usePostDetail(!loggedInfo.isLoggedIn, postId);
const { data: commentData, isLoading: isCommentLoading } = useCommentList(postId);
// const { data: userInfo, isLoading: isUserInfoLoading, error } = useUserInfo(isLoggedIn);

if (!postData) {
return (
Expand Down Expand Up @@ -112,7 +110,7 @@ export default function PostDetailPage() {
<CommentList
commentList={commentData}
memberId={memberId}
isGuest={false}
isGuest={!!memberId}
postWriterName={'익명의손님1'}
/>
)}
Expand Down

0 comments on commit c3393a8

Please sign in to comment.