mirror of
https://github.com/TurTaskProject/TurTaskWeb.git
synced 2025-12-19 22:14:07 +01:00
Fix task can't move to other column
This commit is contained in:
parent
53ce5c22fb
commit
b331fd97c3
@ -9,8 +9,17 @@ export function ColumnContainer({ column, createTask, tasks, deleteTask, updateT
|
|||||||
return tasks.map((task) => task.id);
|
return tasks.map((task) => task.id);
|
||||||
}, [tasks]);
|
}, [tasks]);
|
||||||
|
|
||||||
|
const { setNodeRef, attributes, listeners, transform, transition, isDragging } = useSortable({
|
||||||
|
id: column.id,
|
||||||
|
data: {
|
||||||
|
type: "Column",
|
||||||
|
column,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
|
ref={setNodeRef}
|
||||||
className="
|
className="
|
||||||
bg-[#f1f2f4]
|
bg-[#f1f2f4]
|
||||||
w-[280px]
|
w-[280px]
|
||||||
@ -21,6 +30,8 @@ export function ColumnContainer({ column, createTask, tasks, deleteTask, updateT
|
|||||||
">
|
">
|
||||||
{/* Column title */}
|
{/* Column title */}
|
||||||
<div
|
<div
|
||||||
|
{...attributes}
|
||||||
|
{...listeners}
|
||||||
className="
|
className="
|
||||||
ml-3
|
ml-3
|
||||||
text-md
|
text-md
|
||||||
|
|||||||
@ -1,12 +1,6 @@
|
|||||||
import { useMemo, useState, useEffect } from "react";
|
import { useMemo, useState, useEffect } from "react";
|
||||||
import { ColumnContainerCard } from "./columnContainerWrapper";
|
import { ColumnContainerCard } from "./columnContainerWrapper";
|
||||||
import {
|
import { DndContext, DragOverlay, PointerSensor, useSensor, useSensors } from "@dnd-kit/core";
|
||||||
DndContext,
|
|
||||||
DragOverlay,
|
|
||||||
PointerSensor,
|
|
||||||
useSensor,
|
|
||||||
useSensors,
|
|
||||||
} from "@dnd-kit/core";
|
|
||||||
import { SortableContext, arrayMove } from "@dnd-kit/sortable";
|
import { SortableContext, arrayMove } from "@dnd-kit/sortable";
|
||||||
import { createPortal } from "react-dom";
|
import { createPortal } from "react-dom";
|
||||||
import { TaskCard } from "./taskCard";
|
import { TaskCard } from "./taskCard";
|
||||||
@ -32,9 +26,7 @@ export function KanbanBoard() {
|
|||||||
|
|
||||||
// ---------------- Task Handlers ----------------
|
// ---------------- Task Handlers ----------------
|
||||||
const handleTaskUpdate = (tasks, updatedTask) => {
|
const handleTaskUpdate = (tasks, updatedTask) => {
|
||||||
const updatedTasks = tasks.map((task) =>
|
const updatedTasks = tasks.map((task) => (task.id === updatedTask.id ? updatedTask : task));
|
||||||
task.id === updatedTask.id ? updatedTask : task
|
|
||||||
);
|
|
||||||
setTasks(updatedTasks);
|
setTasks(updatedTasks);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -176,14 +168,8 @@ export function KanbanBoard() {
|
|||||||
justify-center
|
justify-center
|
||||||
overflow-x-auto
|
overflow-x-auto
|
||||||
overflow-y-hidden
|
overflow-y-hidden
|
||||||
"
|
">
|
||||||
>
|
<DndContext sensors={sensors} onDragStart={onDragStart} onDragEnd={onDragEnd} onDragOver={onDragOver}>
|
||||||
<DndContext
|
|
||||||
sensors={sensors}
|
|
||||||
onDragStart={onDragStart}
|
|
||||||
onDragEnd={onDragEnd}
|
|
||||||
onDragOver={onDragOver}
|
|
||||||
>
|
|
||||||
<div className="flex gap-4">
|
<div className="flex gap-4">
|
||||||
<div className="flex gap-4">
|
<div className="flex gap-4">
|
||||||
{!isLoading ? (
|
{!isLoading ? (
|
||||||
@ -195,9 +181,7 @@ export function KanbanBoard() {
|
|||||||
createTask={createTask}
|
createTask={createTask}
|
||||||
deleteTask={deleteTask}
|
deleteTask={deleteTask}
|
||||||
updateTask={updateTask}
|
updateTask={updateTask}
|
||||||
tasks={(tasks || []).filter(
|
tasks={(tasks || []).filter((task) => task.columnId === col.id)}
|
||||||
(task) => task.columnId === col.id
|
|
||||||
)}
|
|
||||||
/>
|
/>
|
||||||
))}{" "}
|
))}{" "}
|
||||||
</SortableContext>
|
</SortableContext>
|
||||||
@ -210,11 +194,7 @@ export function KanbanBoard() {
|
|||||||
{createPortal(
|
{createPortal(
|
||||||
<DragOverlay className="bg-white" dropAnimation={null} zIndex={20}>
|
<DragOverlay className="bg-white" dropAnimation={null} zIndex={20}>
|
||||||
{/* Render the active task as a draggable overlay */}
|
{/* Render the active task as a draggable overlay */}
|
||||||
<TaskCard
|
<TaskCard task={activeTask} deleteTask={deleteTask} updateTask={updateTask} />
|
||||||
task={activeTask}
|
|
||||||
deleteTask={deleteTask}
|
|
||||||
updateTask={updateTask}
|
|
||||||
/>
|
|
||||||
</DragOverlay>,
|
</DragOverlay>,
|
||||||
document.body
|
document.body
|
||||||
)}
|
)}
|
||||||
@ -240,8 +220,6 @@ export function KanbanBoard() {
|
|||||||
if (!over) return; // If not dropped over anything, exit
|
if (!over) return; // If not dropped over anything, exit
|
||||||
|
|
||||||
const activeId = active.id;
|
const activeId = active.id;
|
||||||
const overId = over.id;
|
|
||||||
|
|
||||||
const isActiveATask = active.data.current?.type === "Task";
|
const isActiveATask = active.data.current?.type === "Task";
|
||||||
const isOverAColumn = over.data.current?.type === "Column";
|
const isOverAColumn = over.data.current?.type === "Column";
|
||||||
|
|
||||||
@ -250,8 +228,7 @@ export function KanbanBoard() {
|
|||||||
setTasks((tasks) => {
|
setTasks((tasks) => {
|
||||||
const activeIndex = tasks.findIndex((t) => t.id === activeId);
|
const activeIndex = tasks.findIndex((t) => t.id === activeId);
|
||||||
|
|
||||||
// Extract the column ID from overId
|
const columnId = over.data.current.column.id;
|
||||||
const columnId = extractColumnId(overId);
|
|
||||||
|
|
||||||
tasks[activeIndex].columnId = columnId;
|
tasks[activeIndex].columnId = columnId;
|
||||||
|
|
||||||
@ -259,7 +236,7 @@ export function KanbanBoard() {
|
|||||||
axiosInstance
|
axiosInstance
|
||||||
.put(`todo/change_task_list_board/`, {
|
.put(`todo/change_task_list_board/`, {
|
||||||
todo_id: activeId,
|
todo_id: activeId,
|
||||||
new_list_board_id: over.data.current.task.columnId,
|
new_list_board_id: columnId,
|
||||||
new_index: 0,
|
new_index: 0,
|
||||||
})
|
})
|
||||||
.then((response) => {})
|
.then((response) => {})
|
||||||
@ -271,15 +248,6 @@ export function KanbanBoard() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper function to extract the column ID from the element ID
|
|
||||||
function extractColumnId(elementId) {
|
|
||||||
// Implement logic to extract the column ID from elementId
|
|
||||||
// For example, if elementId is in the format "column-123", you can do:
|
|
||||||
const parts = elementId.split("-");
|
|
||||||
return parts.length === 2 ? parseInt(parts[1], 10) : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle the drag-over event
|
// Handle the drag-over event
|
||||||
function onDragOver(event) {
|
function onDragOver(event) {
|
||||||
const { active, over } = event;
|
const { active, over } = event;
|
||||||
@ -306,39 +274,15 @@ export function KanbanBoard() {
|
|||||||
tasks[activeIndex].columnId = tasks[overIndex].columnId;
|
tasks[activeIndex].columnId = tasks[overIndex].columnId;
|
||||||
return arrayMove(tasks, activeIndex, overIndex - 1);
|
return arrayMove(tasks, activeIndex, overIndex - 1);
|
||||||
}
|
}
|
||||||
axiosInstance
|
|
||||||
.put(`todo/change_task_list_board/`, {
|
|
||||||
todo_id: activeId,
|
|
||||||
new_list_board_id: over.data.current.task.columnId,
|
|
||||||
new_index: 0,
|
|
||||||
})
|
|
||||||
.then((response) => {})
|
|
||||||
.catch((error) => {
|
|
||||||
console.error("Error updating task columnId:", error);
|
|
||||||
});
|
|
||||||
return arrayMove(tasks, activeIndex, overIndex);
|
return arrayMove(tasks, activeIndex, overIndex);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const isOverAColumn = over.data.current?.type === "Column";
|
const isOverAColumn = over.data.current?.type === "Column";
|
||||||
// Move the Task to a different column and update columnId
|
// Move the Task to a different column and update columnId
|
||||||
if (
|
if (isActiveATask && isOverAColumn && tasks.some((task) => task.columnId !== overId)) {
|
||||||
isActiveATask &&
|
|
||||||
isOverAColumn &&
|
|
||||||
tasks.some((task) => task.columnId !== overId)
|
|
||||||
) {
|
|
||||||
setTasks((tasks) => {
|
setTasks((tasks) => {
|
||||||
const activeIndex = tasks.findIndex((t) => t.id === activeId);
|
const activeIndex = tasks.findIndex((t) => t.id === activeId);
|
||||||
axiosInstance
|
|
||||||
.put(`todo/change_task_list_board/`, {
|
|
||||||
todo_id: activeId,
|
|
||||||
new_list_board_id: over.data.current.task.columnId,
|
|
||||||
new_index: 0,
|
|
||||||
})
|
|
||||||
.then((response) => {})
|
|
||||||
.catch((error) => {
|
|
||||||
console.error("Error updating task columnId:", error);
|
|
||||||
});
|
|
||||||
tasks[activeIndex].columnId = overId;
|
tasks[activeIndex].columnId = overId;
|
||||||
return arrayMove(tasks, activeIndex, activeIndex);
|
return arrayMove(tasks, activeIndex, activeIndex);
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user