Skip to content

Commit

Permalink
Feat: 게시물 단일 페이지 만듦 #65
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyeeeah committed Oct 22, 2023
1 parent 1238355 commit 415ea34
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 19 deletions.
13 changes: 13 additions & 0 deletions app/(main)/board/[boardId]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Board from '@/components/pages/board';

export default async function BoardPage({
params: { boardId },
}: {
params: { boardId: number };
}) {
return (
<main className="flex flex-1 w-screen">
<Board boardId={boardId} />
</main>
);
}
15 changes: 15 additions & 0 deletions components/pages/board/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use client';

import BoardIdCard from '@/components/ui/BoardCard/BoardIdCard';
// import useGetBoard from '@/hooks/queries/useGetBoard';

export default function Board({ boardId }: { boardId: number }) {
// const { data } = useGetBoard(boardId);
// if (data) {
return (
<div className="w-full">
<BoardIdCard boardId={boardId} />
</div>
);
// }
}
2 changes: 1 addition & 1 deletion components/pages/main/Feed/BoardsList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default function BoardsList({
)}
{hasNextPage ? null : <div>🐾 더이상 게시물이 없어요 🐾</div>}
<Observer>
<div>로딩스피너...</div>
<div>로딩중...🐾</div>
</Observer>
</FlexBox>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import FlexBox from '@/components/ui/FlexBox';
import Header from '../Header';
import Content from '../Content';
import { BoardCardCommentWrapper } from '../CommentWrapper';
import BoardCardModalComments from '../BoardCardModalPackage/ModalComments';

export default function BoardCardIdWrapper({
children,
}: {
children: React.ReactNode;
}) {
return (
<FlexBox direction="column" justify="between" className="w-full gap-4 p-9">
{children}
</FlexBox>
);
}
export const BoardCardId = Object.assign(BoardCardIdWrapper, {
Header,
Content,
BoardCardCommentWrapper,
BoardCardModalComments,
});
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function BoardCardCommentWrapper({
const postNewComment = () => {
commentMutate({
boardId,
parentId: 1,
parentId: boardId,
content: commentText,
});
setCommentText('');
Expand Down
14 changes: 7 additions & 7 deletions components/ui/BoardCard/BoardCardPackage/Content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function BoardCardContent({
onClickModal,
}: {
children: React.ReactNode;
type: 'mainPC' | 'modal' | 'myPage';
type: 'mainPC' | 'modal' | 'myPage' | 'id';
content: string;
imgs: string[];
onClickModal?: () => void;
Expand All @@ -44,9 +44,9 @@ export default function BoardCardContent({
<FlexBox
direction="column"
align="start"
className={`${(type === 'mainPC' || type === 'myPage') && 'w-full'} ${
type === 'modal' && 'w-[375px] h-full'
} gap-3`}
className={`${
(type === 'mainPC' || type === 'myPage' || type === 'id') && 'w-full'
} ${type === 'modal' && 'w-[375px] h-full'} gap-3`}
>
<div
className={`body3 text-grey-800 ${
Expand All @@ -73,9 +73,9 @@ export default function BoardCardContent({
</div>
)}

{type === 'modal' && (
<FlexBox className="gap-9">
<div className="relative w-[545px] h-[574px]">
{(type === 'modal' || type === 'id') && (
<FlexBox className="w-full gap-9">
<div className="relative w-full h-[574px]">
<Image
src={imgs[imgNum]}
alt="게시글 사진"
Expand Down
47 changes: 47 additions & 0 deletions components/ui/BoardCard/BoardIdCard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// import { Board } from '@/types/types';
import useGetBoard from '@/hooks/queries/useGetBoard';
import useGetCommentList from '@/hooks/queries/useGetCommentList';
import { BoardCardId } from './BoardCardPackage/BoardCardIdPackage';

// interface MyBoardCardProps {
// board: Board;
// }

export default function BoardIdCard({ boardId }: { boardId: number }) {
const { data: board } = useGetBoard(boardId);
const { data: commentList, Observer } = useGetCommentList(boardId);

if (board) {
return (
<BoardCardId>
<BoardCardId.Header board={board} />
<BoardCardId.Content
type="id"
content={board.content}
imgs={board.fileNames}
>
<BoardCardId.BoardCardCommentWrapper
isModal
boardId={board.id}
commentsCount={board.replyCount}
likedCount={board.likedCount}
isLiked={board.boardLiked}
>
{commentList?.pages.map((page) =>
page.content.map((comment) => (
<BoardCardId.BoardCardModalComments
id={comment.id}
userName={comment.nickname}
content={comment.content}
// TODO: 유저 프로필 사진 연결!
userImage="/Feed/desktop/tempProfilePic.svg"
/>
)),
)}
<Observer>로딩중...</Observer>
</BoardCardId.BoardCardCommentWrapper>
</BoardCardId.Content>
</BoardCardId>
);
}
}
2 changes: 2 additions & 0 deletions constant/query-keys.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const queryKeys = {
BOARD_LIST: 'boardList',
BOARD: 'board',
ENTERED_CHAT_LIST: 'enteredChatList',
RECOMMEND_CHAT_LIST: 'recommendChatlist',
TRENDING_CHAT_LIST: 'trendingChatList',
Expand Down
4 changes: 2 additions & 2 deletions hooks/common/useInfiniteScroll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useEffect } from 'react';
import { useInView } from 'react-intersection-observer';

interface InfiniteScrollProps<T> {
queryKey: string;
queryKey: string[];
firstPageParam: number;
queryFn: (pageNumber: number, ...params: number[]) => Promise<T>;
getNextPageParamFn: (page: T) => void;
Expand All @@ -21,7 +21,7 @@ export default function useInfiniteScroll<T>({
}: InfiniteScrollProps<T>) {
const { data, fetchNextPage, isLoading, hasNextPage, isFetchingNextPage } =
useInfiniteQuery({
queryKey: [queryKey],
queryKey,
queryFn: ({ pageParam = firstPageParam }): Promise<T> =>
queryFn(pageParam, ...params),
getNextPageParam: getNextPageParamFn,
Expand Down
11 changes: 11 additions & 0 deletions hooks/queries/useGetBoard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { queryKeys } from '@/constant/query-keys';
import { getBoard } from '@/service/board';
import { useQuery } from '@tanstack/react-query';

export default function useGetBoard(boardId: number) {
const { data, isLoading } = useQuery({
queryKey: [queryKeys.BOARD, boardId],
queryFn: () => getBoard(boardId),
});
return { data, isLoading };
}
3 changes: 2 additions & 1 deletion hooks/queries/useGetBoardList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import { getBoardList } from '@/service/board';
import { BoardList } from '@/types/types';
import { queryKeys } from '@/constant/query-keys';
import useInfiniteScroll from '../common/useInfiniteScroll';

export default function useGetBoardList() {
const { data, isLoading, Observer, hasNextPage } =
useInfiniteScroll<BoardList>({
queryKey: 'boardList',
queryKey: [queryKeys.BOARD_LIST],
firstPageParam: 0,
queryFn: getBoardList,
getNextPageParamFn: (boardList) =>
Expand Down
35 changes: 28 additions & 7 deletions service/board.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { AuthError } from '@/lib/error';
import {
Board,
CommentList,
PostBoardType,
PostCommentType,
PostImageType,
} from '@/types/types';

export async function postBoard(postBoardData: PostBoardType) {
const url = `endpoint/api/board/register`;
const url = `/endpoint/api/board/register`;

const response = await fetch(url, {
method: 'POST',
Expand All @@ -24,7 +25,7 @@ export async function postImageBoard(
boardId: number,
postImageData: PostImageType,
) {
const url = `endpoint/api/file/upload?boardId=${boardId}`;
const url = `/endpoint/api/file/upload?boardId=${boardId}`;
const response = await fetch(url, {
method: 'POST',
credentials: 'include',
Expand Down Expand Up @@ -56,6 +57,26 @@ export async function getBoardList(pageParam: number) {
}
}

export async function getBoard(boardId: number): Promise<Board> {
const url = `/endpoint/api/board/${boardId}`;
try {
const response = await fetch(url);
if (response.status === 401) {
throw new AuthError('로그인이 필요한 서비스입니다.');
}
if (!response.ok) {
throw new Error(`서버오류:${response.status}`);
}
return await response.json();
} catch (error) {
if (error instanceof AuthError) {
window.location.replace('/auth/login');
alert(error.message);
}
throw error;
}
}

export async function deleteBoard(boardId: number) {
const url = `/endpoint/api/board/remove/${boardId}`;
try {
Expand Down Expand Up @@ -83,7 +104,7 @@ export async function deleteBoard(boardId: number) {
}

export async function postComment(postCommentData: PostCommentType) {
const url = `endpoint/api/reply/register`;
const url = `/endpoint/api/reply/register`;
const response = await fetch(url, {
method: 'POST',
credentials: 'include',
Expand Down Expand Up @@ -148,7 +169,7 @@ export async function deleteComment(boardId: number, replyId: number) {
}

export async function addBookmarkBoard(boardId: number) {
const url = `endpoint/api/bookmark/add?boardId=${boardId}`;
const url = `/endpoint/api/bookmark/add?boardId=${boardId}`;
try {
const response = await fetch(url, {
method: 'POST',
Expand All @@ -174,7 +195,7 @@ export async function addBookmarkBoard(boardId: number) {
}

export async function deleteBookmarkBoard(boardId: number) {
const url = `endpoint/api/bookmark/delete?boardId=${boardId}`;
const url = `/endpoint/api/bookmark/delete?boardId=${boardId}`;
try {
const response = await fetch(url, {
method: 'DELETE',
Expand All @@ -200,7 +221,7 @@ export async function deleteBookmarkBoard(boardId: number) {
}

export async function updateBoardLike(boardId: number) {
const url = `endpoint/api/boardLike/like?boardId=${boardId}`;
const url = `/endpoint/api/boardLike/like?boardId=${boardId}`;
try {
const response = await fetch(url, {
method: 'POST',
Expand All @@ -226,7 +247,7 @@ export async function updateBoardLike(boardId: number) {
}

export async function deleteBoardLike(boardId: number) {
const url = `endpoint/api/boardLike/deleteLike?boardId=${boardId}`;
const url = `/endpoint/api/boardLike/deleteLike?boardId=${boardId}`;
try {
const response = await fetch(url, {
method: 'DELETE',
Expand Down

0 comments on commit 415ea34

Please sign in to comment.