File size: 4,820 Bytes
f5071ca |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
import React, { useEffect, useState } from "react";
import Board from "../Components/Board/Board";
import "./Dashboard.css";
import CustomInput from "../Components/CustomInput/CustomInput";
import { ICard, IBoard } from "../Interfaces/Kanban";
import { fetchBoardList, updateLocalStorageBoards } from "../Helper/APILayers";
function Dashboard() {
const [boards, setBoards] = useState<IBoard[]>([]);
useEffect(() => {
fetchData();
}, []);
async function fetchData() {
const boards: IBoard[] = await fetchBoardList();
setBoards(boards);
}
const [targetCard, setTargetCard] = useState({
boardId: 0,
cardId: 0,
});
const addboardHandler = (name: string) => {
const tempBoardsList = [...boards];
tempBoardsList.push({
id: Date.now() + Math.random() * 2,
title: name,
cards: [],
});
setBoards(tempBoardsList);
};
const removeBoard = (boardId: number) => {
const boardIndex = boards.findIndex((item: IBoard) => item.id === boardId);
if (boardIndex < 0) return;
const tempBoardsList = [...boards];
tempBoardsList.splice(boardIndex, 1);
setBoards(tempBoardsList);
};
const addCardHandler = (boardId: number, title: string) => {
const boardIndex = boards.findIndex((item: IBoard) => item.id === boardId);
if (boardIndex < 0) return;
const tempBoardsList = [...boards];
tempBoardsList[boardIndex].cards.push({
id: Date.now() + Math.random() * 2,
title,
labels: [],
date: "",
tasks: [],
desc: "",
});
setBoards(tempBoardsList);
};
const removeCard = (boardId: number, cardId: number) => {
const boardIndex = boards.findIndex((item: IBoard) => item.id === boardId);
if (boardIndex < 0) return;
const tempBoardsList = [...boards];
const cards = tempBoardsList[boardIndex].cards;
const cardIndex = cards.findIndex((item) => item.id === cardId);
if (cardIndex < 0) return;
cards.splice(cardIndex, 1);
setBoards(tempBoardsList);
};
const updateCard = (boardId: number, cardId: number, card: ICard) => {
const boardIndex = boards.findIndex((item) => item.id === boardId);
if (boardIndex < 0) return;
const tempBoardsList = [...boards];
const cards = tempBoardsList[boardIndex].cards;
const cardIndex = cards.findIndex((item) => item.id === cardId);
if (cardIndex < 0) return;
tempBoardsList[boardIndex].cards[cardIndex] = card;
setBoards(tempBoardsList);
};
const onDragEnd = (boardId: number, cardId: number) => {
const sourceBoardIndex = boards.findIndex(
(item: IBoard) => item.id === boardId,
);
if (sourceBoardIndex < 0) return;
const sourceCardIndex = boards[sourceBoardIndex]?.cards?.findIndex(
(item) => item.id === cardId,
);
if (sourceCardIndex < 0) return;
const targetBoardIndex = boards.findIndex(
(item: IBoard) => item.id === targetCard.boardId,
);
if (targetBoardIndex < 0) return;
const targetCardIndex = boards[targetBoardIndex]?.cards?.findIndex(
(item) => item.id === targetCard.cardId,
);
if (targetCardIndex < 0) return;
const tempBoardsList = [...boards];
const sourceCard = tempBoardsList[sourceBoardIndex].cards[sourceCardIndex];
tempBoardsList[sourceBoardIndex].cards.splice(sourceCardIndex, 1);
tempBoardsList[targetBoardIndex].cards.splice(
targetCardIndex,
0,
sourceCard,
);
setBoards(tempBoardsList);
setTargetCard({
boardId: 0,
cardId: 0,
});
};
const onDragEnter = (boardId: number, cardId: number) => {
if (targetCard.cardId === cardId) return;
setTargetCard({
boardId: boardId,
cardId: cardId,
});
};
useEffect(() => {
updateLocalStorageBoards(boards);
}, [boards]);
return (
<div className="app">
<div className="app-nav">
<h1>Trello Board</h1>
</div>
<div className="app-boards-container">
<div className="app-boards">
{boards.map((item) => (
<Board
key={item.id}
board={item}
addCard={addCardHandler}
removeBoard={() => removeBoard(item.id)}
removeCard={removeCard}
onDragEnd={onDragEnd}
onDragEnter={onDragEnter}
updateCard={updateCard}
/>
))}
<div className="app-boards-last">
<CustomInput
displayClass="app-boards-add-board"
editClass="app-boards-add-board-edit"
placeholder="Enter Board Name"
text="Add Board"
buttonText="Add Board"
onSubmit={addboardHandler}
/>
</div>
</div>
</div>
</div>
);
}
export default Dashboard;
|