Skip to content

Commit 9882df7

Browse files
committed
solns
1 parent 01259f3 commit 9882df7

File tree

2 files changed

+51
-18
lines changed

2 files changed

+51
-18
lines changed

src/App.tsx

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { useEffect, useState } from "react";
22
import "./App.css";
3+
import { fetchTasks, updateTasks } from "./api";
34
import KanbanColumn from "./components/KanbanColumn";
45
import { Column, DraggableTask, DraggedTaskInfo, Task } from "./types";
56

@@ -13,38 +14,60 @@ export default function App() {
1314

1415
// Fetch Tasks
1516
useEffect(() => {
16-
// TODO: Pull state from json-server
17-
// Hint: You may want to use the fetchTasks function from api/index.tsx
17+
(async () => {
18+
const tasks = await fetchTasks();
19+
setKanbanColumns(tasks);
20+
})();
1821
}, []);
1922

20-
// Hint: You will need these states for dragging and dropping tasks
2123
const [draggedTaskInfo, setDraggedTaskInfo] = useState<DraggedTaskInfo | null>(null);
2224
const [hoveredColumn, setHoveredColumn] = useState<Column | null>(null);
2325

2426
const handleTaskDragStart = (task: Task, column: Column) => {
25-
// TODO: Implement functionality for when the drag starts
27+
setDraggedTaskInfo({ task, column });
2628
};
2729

2830
const handleTaskDragOver = (e: React.DragEvent, column: Column) => {
2931
e.preventDefault();
30-
// TODO: Implement functionality for when an item is being dragged over a column
31-
// Hint: Remember to check if the item is being dragged over a new column
32+
if (column !== hoveredColumn) {
33+
setHoveredColumn(column);
34+
}
3235
};
3336

3437
const handleTaskDrop = (column: Column) => {
35-
// TODO: Implement functionality for when the item is dropped
36-
// Hint: Make sure to handle the cases when the item is dropped in the same column or in a new column
38+
if (draggedTaskInfo) {
39+
// If the task is dropped in a different column, remove it from the old one and add it to the new one.
40+
if (column !== draggedTaskInfo.column) {
41+
const taskIndex = kanbanColumns[draggedTaskInfo.column].findIndex(
42+
(task) => task.id === draggedTaskInfo.task.id
43+
);
44+
if (taskIndex > -1) {
45+
const newColumnTasks = [...kanbanColumns[column], draggedTaskInfo.task];
46+
const oldColumnTasks = [...kanbanColumns[draggedTaskInfo.column]];
47+
oldColumnTasks.splice(taskIndex, 1);
48+
const newState = { ...kanbanColumns, [column]: newColumnTasks, [draggedTaskInfo.column]: oldColumnTasks };
49+
setKanbanColumns(newState);
50+
// Update the tasks in the db
51+
updateTasks(newState).catch((error) => console.error(error));
52+
}
53+
}
54+
}
55+
56+
setDraggedTaskInfo(null); // Reset dragInfo state
57+
setHoveredColumn(null); // Reset overColumn state
3758
};
3859

3960
const getTasksForColumn = (column: Column): DraggableTask[] => {
40-
// TODO: Handle the bug where card dragged over itself shows duplicate
41-
// Hint: Consider how you can use the dragInfo and overColumn states to prevent this
42-
return [{ id: "1", name: "Task 1", isDragging: false }];
61+
let tasks = kanbanColumns[column];
62+
if (draggedTaskInfo && hoveredColumn === column) {
63+
tasks = [...tasks, { ...draggedTaskInfo.task, isDragging: true }];
64+
}
65+
return tasks;
4366
};
4467

4568
const handleTaskDragEnd = () => {
46-
// TODO: Implement functionality for when the drag ends
47-
// Hint: Remember to handle the case when the item is released back to its current column
69+
setDraggedTaskInfo(null); // Reset dragInfo state
70+
setHoveredColumn(null); // Reset overColumn state
4871
};
4972

5073
return (

src/api/index.tsx

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
const apiUrl = "http://localhost:3001";
22

3-
export const fetchKanbanTasks = async () => {
4-
// TODO: Implement functionality to fetch tasks from the server
3+
export const fetchTasks = async () => {
4+
const response = await fetch(`${apiUrl}/tasks`);
5+
const data = await response.json();
6+
return data;
57
};
68

7-
export const updateKanbanTasks = async (tasks: any) => {
8-
// TODO: Save the new order of the items when tasks are modified to the server
9-
// Hint: You may want to use the fetch API with a "PUT" method
9+
export const updateTasks = async (tasks: any) => {
10+
const response = await fetch(`${apiUrl}/tasks`, {
11+
method: "PUT",
12+
headers: {
13+
"Content-Type": "application/json",
14+
},
15+
body: JSON.stringify(tasks),
16+
});
17+
18+
const data = await response.json();
19+
return data;
1020
};

0 commit comments

Comments
 (0)