Skip to content

Commit

Permalink
Feat: 북마크하기 기능 #65
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyeeeah committed Oct 19, 2023
1 parent d26d7da commit b08e24e
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,7 @@ export default function BookmarkedBoardsList() {
}}
className="w-full"
>
<MyBoardCard
userName={board.writer}
content={board.content}
imgs={board.fileNames}
setShowModal={setShowModal}
commentsCount={board.replyCount}
likedCount={board.likedCount}
createdDate={board.createdDate}
/>
<MyBoardCard board={board} setShowModal={setShowModal} />
</div>
)),
)}
Expand Down
10 changes: 1 addition & 9 deletions components/pages/mypage/MyPageMain/MyBoardsList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,7 @@ export default function MyBoardsList() {
}}
className="w-full"
>
<MyBoardCard
userName={board.writer}
content={board.content}
imgs={board.fileNames}
setShowModal={setShowModal}
commentsCount={board.replyCount}
likedCount={board.likedCount}
createdDate={board.createdDate}
/>
<MyBoardCard board={board} setShowModal={setShowModal} />
</div>
)),
)}
Expand Down
21 changes: 19 additions & 2 deletions components/ui/BoardCard/BoardCardPackage/BoardCardDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
import DotsIcon from 'public/DotsIcon.svg';
import copyToClipBoard from '@/utils/copyToClipBoard';
import useAddBookmark from '@/hooks/mutations/useAddBookmark';
import useDeleteBookmark from '@/hooks/mutations/useDeleteBookmark';
import { Dropdown } from '../../ui';

export default function PostCardDropdown() {
export default function PostCardDropdown({
boardId, // TODO : 북마크 되어있는지 확인
// isBookmarked,
}: {
boardId: number;
// isBookmarked: boolean;
}) {
const { mutate: addBookmark } = useAddBookmark();
const { mutate: deleteBookmark } = useDeleteBookmark();
const handleBookmark = () => {
// if (isBookmarked) {
deleteBookmark(boardId);
// } else {
// addBookmark(boardId);
// }
};
return (
<Dropdown>
<Dropdown.Trigger>
<DotsIcon className="w-6 h-6 tablet:w-7 tablet:h-7" />
</Dropdown.Trigger>
<Dropdown.Menu>
<Dropdown.Item>북마크 추가</Dropdown.Item>
<Dropdown.Item event={handleBookmark}>북마크 추가</Dropdown.Item>
<Dropdown.Item event={() => copyToClipBoard(window.location.href)}>
공유하기
</Dropdown.Item>
Expand Down
4 changes: 3 additions & 1 deletion components/ui/BoardCard/BoardCardPackage/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ export default function BoardCardHeader({
userName,
userImage,
createdDate,
boardId,
}: {
userName: string;
userImage: string;
createdDate: string;
boardId: number;
}) {
return (
<FlexBox justify="between" className="w-full">
Expand All @@ -25,7 +27,7 @@ export default function BoardCardHeader({
</div>
</FlexBox>
</FlexBox>
<BoardCardDropdown />
<BoardCardDropdown boardId={boardId} />
</FlexBox>
);
}
1 change: 1 addition & 0 deletions components/ui/BoardCard/FeedBoardCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default function FeedBoardCard({
userName={board.writer}
createdDate={board.createdDate}
userImage={board.userImageUrl}
boardId={board.id}
/>
<BoardCard.Content
type="mainPC"
Expand Down
30 changes: 13 additions & 17 deletions components/ui/BoardCard/MyPageBoardCard.tsx
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
import { BoardCard } from '@/components/ui/BoardCard/BoardCardPackage';
import { Board } from '@/types/types';
import { Dispatch, SetStateAction } from 'react';

interface MyBoardCardProps {
userName: string;
content: string;
imgs: string[];
board: Board;
setShowModal: Dispatch<SetStateAction<boolean>>;
commentsCount: number;
likedCount: number;
createdDate: string;
}

export default function MyPageBoardCard({
userName,
content,
imgs,
board,
setShowModal,
commentsCount,
likedCount,
createdDate,
}: MyBoardCardProps) {
return (
<BoardCard>
<BoardCard.Header userName={userName} createdDate={createdDate} />
<BoardCard.Header
userName={board.writer}
createdDate={board.createdDate}
userImage={board.userImageUrl}
boardId={board.id}
/>
<BoardCard.Content
type="myPage"
content={content}
imgs={imgs}
content={board.content}
imgs={board.fileNames}
onClickModal={() => setShowModal(true)}
>
<BoardCard.MyPageBoardCardCommentWrapper
onClickModal={() => setShowModal(true)}
commentsCount={commentsCount}
likedCount={likedCount}
commentsCount={board.replyCount}
likedCount={board.likedCount}
/>
</BoardCard.Content>
</BoardCard>
Expand Down
16 changes: 16 additions & 0 deletions hooks/mutations/useAddBookmark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { addBookmarkBoard } from '@/service/board';
import Toast from '@/utils/notification';
import { useMutation } from '@tanstack/react-query';

export default function useAddBookmark() {
const { mutate, isLoading } = useMutation({
mutationFn: addBookmarkBoard,
onSuccess: () => {
Toast.success('북마크가 성공적으로 저장 되었습니다.');
},
onError: () => {
Toast.error('북마크를 저장하지 못했습니다. 잠시후 다시 시도해주세요.🥲');
},
});
return { mutate, isLoading };
}
16 changes: 16 additions & 0 deletions hooks/mutations/useDeleteBookmark.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { deleteBookmarkBoard } from '@/service/board';
import Toast from '@/utils/notification';
import { useMutation } from '@tanstack/react-query';

export default function useDeleteBookmark() {
const { mutate, isLoading } = useMutation({
mutationFn: deleteBookmarkBoard,
onSuccess: () => {
Toast.success('북마크가 성공적으로 삭제 되었습니다.');
},
onError: () => {
Toast.error('북마크를 삭제하지 못했습니다. 잠시후 다시 시도해주세요.🥲');
},
});
return { mutate, isLoading };
}
File renamed without changes.
2 changes: 1 addition & 1 deletion hooks/queries/useGetMyBoardList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getMyBoardList from '@/service/myPage';
import { getMyBoardList } from '@/service/myPage';
import { MyBoardList } from '@/types/types';
import useInfiniteScroll from '../common/useInfiniteScroll';

Expand Down
4 changes: 2 additions & 2 deletions hooks/queries/useGetMyBookmarkedBoardList.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import getMyBoardList from '@/service/myPage';
import { getBookmarkedBoardList } from '@/service/myPage';
import { BookmarkedBoardList } from '@/types/types';
import useInfiniteScroll from '../common/useInfiniteScroll';

export default function useGetBookmarkedBoardList() {
const { data, isLoading, Observer } = useInfiniteScroll<BookmarkedBoardList>({
queryKey: 'myBookmarkedBoards',
firstPageParam: 0,
queryFn: getMyBoardList,
queryFn: getBookmarkedBoardList,
getNextPageParamFn: (boardlist) =>
boardlist.last ? undefined : boardlist.number + 1,
});
Expand Down
52 changes: 52 additions & 0 deletions service/board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,58 @@ export async function getCommentList(
}
}

export async function addBookmarkBoard(boardId: number) {
const url = `endpoint/api/bookmark/add?boardId=${boardId}`;
try {
const response = await fetch(url, {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
});
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 deleteBookmarkBoard(boardId: number) {
const url = `endpoint/api/bookmark/delete?boardId=${boardId}`;
try {
const response = await fetch(url, {
method: 'DELETE',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
});
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 updateBoardLike(boardId: number) {
const url = `endpoint/api/boardLike/like?boardId=${boardId}`;
try {
Expand Down
25 changes: 24 additions & 1 deletion service/myPage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AuthError } from '@/lib/error';

export default async function getMyBoardList(pageNumber: number) {
export async function getMyBoardList(pageNumber: number) {
let url = `/endpoint/api/board/myPage?pageSize=4`;
if (pageNumber !== 0) {
url += `&pageNumber=${pageNumber}`;
Expand All @@ -22,3 +22,26 @@ export default async function getMyBoardList(pageNumber: number) {
throw error;
}
}

export async function getBookmarkedBoardList(pageNumber: number) {
let url = `/endpoint/api/bookmark/list?pageSize=4`;
if (pageNumber !== 0) {
url += `&pageNumber=${pageNumber}`;
}
try {
const response = await fetch(url, { credentials: 'include' });
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;
}
}

0 comments on commit b08e24e

Please sign in to comment.