Skip to content
This repository has been archived by the owner on Jun 12, 2022. It is now read-only.

support deleting board #73

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions frontend/cypress/integration/stubbed/board.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ context("Board Detail (Member)", () => {
cy.findAllByTestId("board-name-textarea").should("not.exist");
});
});
it("should not see delete board button", () => {
cy.findAllByTestId("delete-board").should("not.exist");
});
});

context("Board Detail (Owner)", () => {
Expand Down Expand Up @@ -232,4 +235,22 @@ context("Board Detail (Owner)", () => {
});
});
});
it("should delete board", () => {
const stub = cy.stub();
stub.onFirstCall().returns(true);
cy.on("window:confirm", stub);
cy.fixture("internals_board").then((board) => {
cy.route("DELETE", `/api/boards/${board.id}/`, "");

cy.findByText(board.name).should("be.visible");

cy.findByTestId(`board`).within(() => {
cy.findByTestId("delete-board").click();
});

cy.findAllByText(board.name).should("not.exist");
cy.findByText("All Boards").should("be.visible");
cy.findByText(/Create new board/i).should("be.visible");
});
});
});
32 changes: 30 additions & 2 deletions frontend/src/features/board/BoardBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { barHeight } from "const";
import { AvatarGroup } from "@material-ui/lab";
import { css } from "@emotion/core";
import { avatarStyles } from "styles";
import { useHistory } from "react-router-dom";
import BoardName from "components/BoardName";
import MemberInvite from "features/member/MemberInvite";
import MemberDetail from "features/member/MemberDetail";
Expand All @@ -17,10 +18,11 @@ import { Button } from "@material-ui/core";
import { PRIMARY } from "utils/colors";
import { addColumn } from "features/column/ColumnSlice";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faPlus, faPen } from "@fortawesome/free-solid-svg-icons";
import { faPlus, faPen, faTrash } from "@fortawesome/free-solid-svg-icons";
import { setDialogOpen } from "features/label/LabelSlice";
import LabelDialog from "features/label/LabelDialog";
import { useParams } from "react-router-dom";
import { deleteBoard } from "features/board/BoardSlice";
import {
selectAllMembers,
setMemberListOpen,
Expand Down Expand Up @@ -64,11 +66,12 @@ const buttonStyles = css`

const BoardBar = () => {
const dispatch = useDispatch();
const history = useHistory();
const members = useSelector(selectAllMembers);
const error = useSelector((state: RootState) => state.board.detailError);
const detail = useSelector((state: RootState) => state.board.detail);
const boardOwner = useSelector(currentBoardOwner);
const { id } = useParams();
const { id } = useParams<{ id: string }>();
const detailDataExists = detail?.id.toString() === id;

if (!detailDataExists || error || !detail) {
Expand All @@ -83,6 +86,17 @@ const BoardBar = () => {
dispatch(setDialogOpen(true));
};

const handleDelete = () => {
if (
window.confirm(
"Are you sure? Deleting the board will also delete related columns and tasks, and this cannot be undone."
)
) {
dispatch(deleteBoard(id));
history.push("/boards");
}
};

return (
<Container data-testid="board">
<Items>
Expand Down Expand Up @@ -149,6 +163,20 @@ const BoardBar = () => {
>
Add Column
</Button>
{boardOwner && (
<Button
size="small"
css={css`
${buttonStyles}
font-weight: 600;
`}
startIcon={<FontAwesomeIcon icon={faTrash} />}
data-testid="delete-board"
onClick={handleDelete}
>
Delete Board
</Button>
)}
</Right>
</Items>
<MemberDialog board={detail} />
Expand Down
17 changes: 17 additions & 0 deletions frontend/src/features/board/BoardSlice.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createSlice, PayloadAction, createAsyncThunk } from "@reduxjs/toolkit";
import { Board, IColumn, Id, ITask, Label, NanoBoard } from "types";
import api, { API_BOARDS } from "api";
import { createInfoToast } from "features/toast/ToastSlice";
import { RootState } from "store";
import { logout } from "features/auth/AuthSlice";

Expand Down Expand Up @@ -40,6 +41,15 @@ export const patchBoard = createAsyncThunk<
return response.data;
});

export const deleteBoard = createAsyncThunk<string, string>(
"board/deleteBoardStatus",
async (id, { dispatch }) => {
await api.delete(`${API_BOARDS}${id}/`);
dispatch(createInfoToast("Board deleted"));
return id;
}
);

interface ColumnsResponse extends IColumn {
tasks: ITask[];
}
Expand Down Expand Up @@ -146,6 +156,13 @@ export const slice = createSlice({
state.detailError = undefined;
state.detailLoading = false;
});
builder.addCase(deleteBoard.fulfilled, (state, action) => {
const indexOfDeletedBoard = state.all.findIndex(
(board) => board.id.toString() === action.payload
);
state.all.splice(indexOfDeletedBoard, 1);
state.detail = null;
});
builder.addCase(logout.fulfilled, (state) => {
state.all = [];
state.detail = null;
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/features/column/ColumnSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
createEntityAdapter,
createAsyncThunk,
} from "@reduxjs/toolkit";
import { fetchBoardById } from "features/board/BoardSlice";
import { deleteBoard, fetchBoardById } from "features/board/BoardSlice";
import { IColumn, Id } from "types";
import api, { API_SORT_COLUMNS, API_COLUMNS } from "api";
import { createErrorToast, createInfoToast } from "features/toast/ToastSlice";
Expand Down Expand Up @@ -75,6 +75,9 @@ export const slice = createSlice({
builder.addCase(deleteColumn.fulfilled, (state, action) => {
columnAdapter.removeOne(state, action.payload);
});
builder.addCase(deleteBoard.fulfilled, (state) => {
columnAdapter.removeAll(state);
});
},
});

Expand Down
4 changes: 4 additions & 0 deletions frontend/src/features/comment/CommentSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
createInfoToast,
createSuccessToast,
} from "features/toast/ToastSlice";
import { deleteBoard } from "features/board/BoardSlice";
import { RootState } from "store";
import { Id, NewTaskComment, Status, TaskComment } from "types";

Expand Down Expand Up @@ -93,6 +94,9 @@ export const slice = createSlice({
builder.addCase(deleteComment.fulfilled, (state, action) => {
commentAdapter.removeOne(state, action.payload as Id);
});
builder.addCase(deleteBoard.fulfilled, (state) => {
commentAdapter.removeAll(state);
});
},
});

Expand Down
5 changes: 4 additions & 1 deletion frontend/src/features/label/LabelSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
PayloadAction,
} from "@reduxjs/toolkit";
import { Label, Id } from "types";
import { fetchBoardById } from "features/board/BoardSlice";
import { deleteBoard, fetchBoardById } from "features/board/BoardSlice";
import { RootState } from "store";
import api, { API_LABELS } from "api";
import { createInfoToast, createErrorToast } from "features/toast/ToastSlice";
Expand Down Expand Up @@ -84,6 +84,9 @@ export const slice = createSlice({
builder.addCase(deleteLabel.fulfilled, (state, action) => {
labelAdapter.removeOne(state, action.payload);
});
builder.addCase(deleteBoard.fulfilled, (state) => {
labelAdapter.removeAll(state);
});
},
});

Expand Down
5 changes: 4 additions & 1 deletion frontend/src/features/member/MemberSlice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
createEntityAdapter,
} from "@reduxjs/toolkit";
import { BoardMember } from "types";
import { fetchBoardById } from "features/board/BoardSlice";
import { deleteBoard, fetchBoardById } from "features/board/BoardSlice";
import { RootState } from "store";

const memberAdapter = createEntityAdapter<BoardMember>({
Expand Down Expand Up @@ -38,6 +38,9 @@ export const slice = createSlice({
builder.addCase(fetchBoardById.fulfilled, (state, action) => {
memberAdapter.setAll(state, action.payload.members);
});
builder.addCase(deleteBoard.fulfilled, (state) => {
memberAdapter.removeAll(state);
});
},
});

Expand Down
6 changes: 5 additions & 1 deletion frontend/src/features/task/TaskSlice.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createSlice, PayloadAction, createAsyncThunk } from "@reduxjs/toolkit";
import { TasksByColumn, ITask, Id, NewTask, PriorityValue } from "types";
import { fetchBoardById } from "features/board/BoardSlice";
import { deleteBoard, fetchBoardById } from "features/board/BoardSlice";
import { AppDispatch, AppThunk, RootState } from "store";
import {
createErrorToast,
Expand Down Expand Up @@ -156,6 +156,10 @@ export const slice = createSlice({
);
}
});
builder.addCase(deleteBoard.fulfilled, (state) => {
state.byColumn = {};
state.byId = {};
});
},
});

Expand Down