Skip to content

Commit

Permalink
Merge pull request #54 from knowankit/feat/delete-all-column
Browse files Browse the repository at this point in the history
Delete all columns when its board is deleted
  • Loading branch information
knowankit authored Jun 1, 2021
2 parents 6e43a8c + 874b991 commit 43bf60c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
9 changes: 9 additions & 0 deletions pages/api/boards/[slug]/columns/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
return;
}

case 'DELETE': {
const { slug } = req.query;

await db.collection('columns').remove({ boardId: slug });
res.send({ message: 'All columns deleted' });

return;
}

default:
res.send({ message: 'DB error' });
break;
Expand Down
4 changes: 2 additions & 2 deletions src/components/board/columns/column.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Cards from '@/src/components/board/columns/cards';
import { useDrop } from 'react-dnd';
import { ItemTypes } from '@/util/items';
import { useDispatch } from 'react-redux';
import columns, { deleteColumn, fetchColumns, updateColumn } from '@/src/slices/columns';
import { deleteColumn, fetchColumns, updateColumn } from '@/src/slices/columns';
import debounce from 'lodash.debounce';

const Column = ({ showCardDetail, column, index, id }): JSX.Element => {
Expand Down Expand Up @@ -69,6 +69,7 @@ const Column = ({ showCardDetail, column, index, id }): JSX.Element => {
columnName: value,
columnId: column._id
};

await dispatch(updateColumn(data));
};

Expand All @@ -94,7 +95,6 @@ const Column = ({ showCardDetail, column, index, id }): JSX.Element => {
</Button>
</Box>
</Box>

<Cards showCardDetail={showCardDetail} cards={column.cards} columnIndex={index} />
<Button
size="xs"
Expand Down
1 change: 1 addition & 0 deletions src/components/board/columns/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const BoardColumns: FC = (): JSX.Element => {

const addColumn = async () => {
const columnId = shortId.generate();

await dispatch(addColumnToBoard(columnId));
await dispatch(fetchColumns());
};
Expand Down
10 changes: 3 additions & 7 deletions src/components/sub-navbar/board-settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,8 @@ import {
import Link from 'next/link';
import { useAppSelector } from '@/src/hooks';
import { useDispatch } from 'react-redux';
import {
updateBoardDetail,
saveBoard,
fetchBoard,
deleteBoard,
resetBoard
} from '@/src/slices/board';
import { updateBoardDetail, saveBoard, fetchBoard, deleteBoard } from '@/src/slices/board';
import { deleteAllColumn } from '@/src/slices/columns';
import { AiFillSetting } from 'react-icons/ai';
import { useRouter } from 'next/router';

Expand All @@ -49,6 +44,7 @@ const BoardSettings = (): JSX.Element => {

const handleDelete = async () => {
await dispatch(deleteBoard());
await dispatch(deleteAllColumn());

if (boardDetail.status === 'success') {
router.push('/boards');
Expand Down
34 changes: 34 additions & 0 deletions src/slices/columns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,31 @@ export const deleteColumn = createAsyncThunk(
}
);

export const deleteAllColumn = createAsyncThunk(
'column/deleteAllColumn',
async (_obj, { getState }) => {
const { board } = getState() as { board: BoardSlice };

const url = `${host}/api/boards/${board.board._id}/columns`;

const response = await fetch(url, {
method: 'DELETE',
mode: 'cors',
cache: 'no-cache',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/json'
},
redirect: 'follow',
referrerPolicy: 'no-referrer'
});

const inJSON = await response.json();

return inJSON;
}
);

export const addColumnToBoard = createAsyncThunk(
'column/add',
async (columnId: string, { getState }) => {
Expand Down Expand Up @@ -158,6 +183,15 @@ export const boardSlice = createSlice({
},
[updateColumn.rejected.toString()]: (state) => {
state.status = 'failed';
},
[deleteAllColumn.pending.toString()]: (state) => {
state.status = 'pending';
},
[deleteAllColumn.fulfilled.toString()]: (state, { payload }) => {
state.status = 'success';
},
[deleteAllColumn.rejected.toString()]: (state) => {
state.status = 'failed';
}
}
});
Expand Down

1 comment on commit 43bf60c

@vercel
Copy link

@vercel vercel bot commented on 43bf60c Jun 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.