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/#147: 관리자는 공지사항 게시판 위치 변경할 수 있도록 추가 #148

Merged
merged 4 commits into from
Sep 25, 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
10 changes: 10 additions & 0 deletions __mocks__/handlers/boardHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ const boardsInfo: BoardsInfo = {
boardTitle: '공지사항',
boardIndex: 0,
},
{
boardId: 'bbb',
boardTitle: '게임 룰',
boardIndex: 1,
},
{
boardId: 'ccc',
boardTitle: '커뮤니티',
boardIndex: 2,
},
],
'234': [
{
Expand Down
97 changes: 76 additions & 21 deletions src/components/Sidebar/BoardBar/BoardBody.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DragDropContext, Droppable, Draggable, DropResult } from '@hello-pangea/dnd';
import { MouseEventHandler, useEffect, useState } from 'react';
import { useQuery } from '@tanstack/react-query';
import { useRouter } from 'next/router';
Expand Down Expand Up @@ -44,6 +45,7 @@ const postData = async (channelLink: string) => {

const BoardBody = ({ channelLink }: Props) => {
const [selected, setSelected] = useState<string>('');
const [boards, setBoards] = useState<Channels[]>();
const router = useRouter();

const { data, isSuccess } = useQuery(['getBoardLists', channelLink], () =>
Expand Down Expand Up @@ -71,24 +73,51 @@ const BoardBody = ({ channelLink }: Props) => {
};

const onClickNewBoard: MouseEventHandler<HTMLElement> = async () => {
if (boards === undefined) return;
const res = await postData(channelLink);
const newBoard: Channels = {
boardId: res.boardId.toString(),
boardTitle: res.boardTitle,
boardIndex: res.boardIndex,
};
data?.push(newBoard);
setBoards([...boards, newBoard]);
selectBoardId(newBoard.boardId);
handleBoard(channelLink, newBoard.boardId, res.boardTitle);
};

const postCustomBoard = async (customedBoards: Channels[]) => {
const res = await authAPI({
method: 'post',
url: `/api/channel/${channelLink}/order`,
data: {
channelBoardLoadDtoList: boards,
},
});

if (res.status === 200) setBoards(customedBoards);
};

const selectBoardId = (boardId: string) => {
router.push(`/contents/${channelLink}/${boardId}`);
setSelected(boardId);
};

const dragEnd = ({ source, destination }: DropResult) => {
if (!destination || !boards) return;
if (source.index === destination.index) return;

const newBoards = [...boards];
const [removed] = newBoards.splice(source.index, 1);
newBoards.splice(destination.index, 0, removed);
for (let i = 0; i < newBoards.length; i++) {
newBoards[i].boardIndex = i;
}
postCustomBoard(newBoards);
};

useEffect(() => {
const lastVisitBoardId = lastVisitedBoardIdLists[channelLink]?.boardId;
if (isSuccess) setBoards(data);

if (lastVisitBoardId) {
selectBoardId(lastVisitBoardId);
Expand All @@ -103,26 +132,51 @@ const BoardBody = ({ channelLink }: Props) => {

return (
<Container>
{isSuccess &&
data.map((board) => (
<Wrapper
key={board.boardId}
data-id={board.boardId}
data-board-title={board.boardTitle}
onClick={onClickBoard}
isSelected={board.boardId === selected}
>
{board.boardTitle}
<Icon kind='lock' color='#637083' size='1.5rem' />
</Wrapper>
))}
{channelPermission === 0 && (
<Wrapper isSelected={false} onClick={onClickNewBoard}>
공지 추가하기
<Icon kind='plus' color='#637083' size='1.6rem' />
</Wrapper>
)}

<DragDropContext onDragEnd={dragEnd}>
<Droppable droppableId='boards'>
{(provided) => (
<div ref={provided.innerRef} {...provided.droppableProps}>
{boards &&
boards.map((board, index) =>
channelPermission === 0 ? (
<Draggable key={board.boardId} draggableId={board.boardId} index={index}>
{(provided) => (
<Wrapper
ref={provided.innerRef}
{...provided.draggableProps}
{...provided.dragHandleProps}
data-id={board.boardId}
data-board-title={board.boardTitle}
onClick={onClickBoard}
isSelected={board.boardId === selected}
>
{board.boardTitle}
<Icon kind='lock' color='#637083' size='1.5rem' />
</Wrapper>
)}
</Draggable>
) : (
<Wrapper
key={board.boardId}
data-id={board.boardId}
data-board-title={board.boardTitle}
onClick={onClickBoard}
isSelected={selected === board.boardId.toString()}
>
{board.boardTitle}
</Wrapper>
),
)}
{channelPermission === 0 && (
<Wrapper isSelected={false} onClick={onClickNewBoard}>
공지 추가하기
<Icon kind='plus' color='#637083' size='1.6rem' />
</Wrapper>
)}
</div>
)}
</Droppable>
</DragDropContext>
<Boarder></Boarder>
</Container>
);
Expand All @@ -144,6 +198,7 @@ const Wrapper = styled.li<{ isSelected: boolean }>`
&:hover {
background-color: #39587e;
}
color: white;

${({ isSelected }) => isSelected && `background-color: #39587E`};
`;
Expand Down