Skip to content

Commit

Permalink
add drag and drop
Browse files Browse the repository at this point in the history
  • Loading branch information
felipeog committed Dec 25, 2024
1 parent 1c5ec93 commit 6bd14fb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/BoardContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type TBoardContext = {
boards: string[];
addBoard: (board: string) => void;
removeBoard: (board: string) => void;
reorderBoard: (from: string, to: string) => void;
};

type TBoardContextProviderProps = {
Expand Down Expand Up @@ -44,6 +45,21 @@ export function BoardContextProvider(props: TBoardContextProviderProps) {
[]
);

const reorderBoard = useCallback(
(from: string, to: string) =>
setBoards((prevBoards) => {
const boards = [...prevBoards];
const fromIndex = boards.indexOf(from);
const toIndex = boards.indexOf(to);

boards.splice(fromIndex, 1);
boards.splice(toIndex, 0, from);

return boards;
}),
[]
);

useEffect(() => {
localStorage.setItem(LOCAL_STORAGE_KEY, JSON.stringify(boards));
}, [boards]);
Expand All @@ -54,6 +70,7 @@ export function BoardContextProvider(props: TBoardContextProviderProps) {
boards,
addBoard,
removeBoard,
reorderBoard,
}}
>
{props.children}
Expand Down
27 changes: 26 additions & 1 deletion src/LanesContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ADD_BOARD_DIALOG_HEIGHT } from "./AddBoardDialog";
import { useBoardContent } from "./useBoardContent";
import { useBoardContext } from "./BoardContext";
import { formatDate } from "./formatDate";
import { DragEvent } from "react";

export function LanesContainer() {
const { boardContext } = useBoardContext();
Expand Down Expand Up @@ -30,6 +31,25 @@ function Lane(props: TLaneProps) {
const { boardContext } = useBoardContext();
const { boardContent, isLoading, loadMore } = useBoardContent(props.board);

function onDragStart(event: DragEvent<HTMLDivElement>) {
event.dataTransfer.setData("text/plain", props.board);
}

function onDragEnter(event: DragEvent<HTMLDivElement>) {
event.preventDefault();
}

function onDragOver(event: DragEvent<HTMLDivElement>) {
event.preventDefault();
}

function onDrop(event: DragEvent<HTMLDivElement>) {
const board = event.dataTransfer.getData("text/plain");
if (board !== props.board) {
boardContext.reorderBoard(board, props.board);
}
}

return (
<div
className="Lane"
Expand All @@ -40,6 +60,11 @@ function Lane(props: TLaneProps) {
borderRight: "1px solid var(--neutral-60)",
overflowY: "scroll",
}}
draggable
onDragStart={onDragStart}
onDragEnter={onDragEnter}
onDragOver={onDragOver}
onDrop={onDrop}
>
<div
style={{
Expand All @@ -63,7 +88,7 @@ function Lane(props: TLaneProps) {
</button>
</div>

<div>
<div style={{ backgroundColor: "var(--neutral-90)" }}>
{boardContent.length > 0
? boardContent.map((item) => (
<div key={item.name}>
Expand Down

0 comments on commit 6bd14fb

Please sign in to comment.