Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(api,web): fix kanban position persistence and task numbering

Ivan Bobchenkov59d57711bd79
diff --git a/apps/api/src/task/controllers/create-task.ts b/apps/api/src/task/controllers/create-task.ts
index c5413e86d..5bcc2e2e5 100644
--- a/apps/api/src/task/controllers/create-task.ts
+++ b/apps/api/src/task/controllers/create-task.ts
@@ -1,4 +1,4 @@
-import { and, eq } from "drizzle-orm";
+import { and, eq, max } from "drizzle-orm";
import { HTTPException } from "hono/http-exception";
import db from "../../database";
import { columnTable, taskTable, userTable } from "../../database/schema";
@@ -36,6 +36,20 @@ async function createTask({
),
});
+ const [maxPositionResult] = await db
+ .select({ maxPosition: max(taskTable.position) })
+ .from(taskTable)
+ .where(
+ and(
+ eq(taskTable.projectId, projectId),
+ column?.id
+ ? eq(taskTable.columnId, column.id)
+ : eq(taskTable.status, status || "to-do"),
+ ),
+ );
+
+ const nextPosition = (maxPositionResult?.maxPosition ?? 0) + 1;
+
const [createdTask] = await db
.insert(taskTable)
.values({
@@ -48,6 +62,7 @@ async function createTask({
description: description || "",
priority: priority || "",
number: nextTaskNumber + 1,
+ position: nextPosition,
})
.returning();
diff --git a/apps/api/src/task/controllers/get-next-task-number.ts b/apps/api/src/task/controllers/get-next-task-number.ts
index bfe85a680..a361fdaca 100644
--- a/apps/api/src/task/controllers/get-next-task-number.ts
+++ b/apps/api/src/task/controllers/get-next-task-number.ts
@@ -1,14 +1,14 @@
-import { count, eq } from "drizzle-orm";
+import { eq, max } from "drizzle-orm";
import db from "../../database";
import { taskTable } from "../../database/schema";
async function getNextTaskNumber(projectId: string) {
- const [task] = await db
- .select({ count: count() })
+ const [result] = await db
+ .select({ maxNumber: max(taskTable.number) })
.from(taskTable)
.where(eq(taskTable.projectId, projectId));
- return task ? task.count : 0;
+ return result?.maxNumber ?? 0;
}
export default getNextTaskNumber;
diff --git a/apps/web/src/components/backlog-list-view/index.tsx b/apps/web/src/components/backlog-list-view/index.tsx
index 6ba6651e3..7d5d826f6 100644
--- a/apps/web/src/components/backlog-list-view/index.tsx
+++ b/apps/web/src/components/backlog-list-view/index.tsx
@@ -232,7 +232,7 @@ function BacklogListView({ project }: BacklogListViewProps) {
finalTasks.forEach((t, index) => {
updateTask({
...t,
- position: index + 1,
+ position: index,
});
});
} else {
@@ -253,7 +253,19 @@ function BacklogListView({ project }: BacklogListViewProps) {
updateTask({
...t,
status: targetSection,
- position: index + 1,
+ position: index,
+ });
+ });
+
+ const sourceTasks =
+ activeTask.status === "planned"
+ ? draft.plannedTasks || []
+ : draft.archivedTasks || [];
+
+ sourceTasks.forEach((t, index) => {
+ updateTask({
+ ...t,
+ position: index,
});
});
}
diff --git a/apps/web/src/components/kanban-board/index.tsx b/apps/web/src/components/kanban-board/index.tsx
index 0cd2a7138..9fb875035 100644
--- a/apps/web/src/components/kanban-board/index.tsx
+++ b/apps/web/src/components/kanban-board/index.tsx
@@ -154,23 +154,27 @@ function KanbanBoard({ project }: KanbanBoardProps) {
destinationColumn.tasks.splice(destinationIndex, 0, task);
destinationColumn.tasks.forEach((t, index) => {
- updateTask({ ...t, position: index + 1 });
+ updateTask({ ...t, position: index });
});
queryClient.invalidateQueries({
queryKey: ["projects", project.workspaceId],
});
} else {
- const updatedTask = { ...task, status: destinationColumn.id };
+ task.status = destinationColumn.id;
const destinationIndex =
overId === destinationColumn.id
? destinationColumn.tasks.length
- : destinationColumn.tasks.findIndex((t) => t.id === overId);
+ : destinationColumn.tasks.findIndex((t) => t.id === overId) + 1;
- destinationColumn.tasks.splice(destinationIndex + 1, 0, updatedTask);
+ destinationColumn.tasks.splice(destinationIndex, 0, task);
destinationColumn.tasks.forEach((t, index) => {
- updateTask({ ...t, position: index + 1 });
+ updateTask({ ...t, status: destinationColumn.id, position: index });
+ });
+
+ sourceColumn.tasks.forEach((t, index) => {
+ updateTask({ ...t, position: index });
});
}
});
diff --git a/apps/web/src/components/list-view/index.tsx b/apps/web/src/components/list-view/index.tsx
index 56db5f8ba..2be6248c0 100644
--- a/apps/web/src/components/list-view/index.tsx
+++ b/apps/web/src/components/list-view/index.tsx
@@ -194,18 +194,30 @@ function ListView({ project }: ListViewProps) {
updateTask({
...t,
status: destinationColumn.id,
- position: index + 1,
+ position: index,
});
});
} else {
task.status = destinationColumn.id;
- destinationColumn.tasks.push(task);
+ const destinationIndex =
+ overId === destinationColumn.id
+ ? destinationColumn.tasks.length
+ : destinationColumn.tasks.findIndex((t) => t.id === overId) + 1;
+
+ destinationColumn.tasks.splice(destinationIndex, 0, task);
destinationColumn.tasks.forEach((t, index) => {
updateTask({
...t,
status: destinationColumn.id,
- position: index + 1,
+ position: index,
+ });
+ });
+
+ sourceColumn.tasks.forEach((t, index) => {
+ updateTask({
+ ...t,
+ position: index,
});
});
}
diff --git a/apps/web/src/hooks/queries/task/use-get-tasks.ts b/apps/web/src/hooks/queries/task/use-get-tasks.ts
index dd5aaae9c..e70c5e333 100644
--- a/apps/web/src/hooks/queries/task/use-get-tasks.ts
+++ b/apps/web/src/hooks/queries/task/use-get-tasks.ts
@@ -5,7 +5,7 @@ export function useGetTasks(projectId: string) {
return useQuery({
queryKey: ["tasks", projectId],
queryFn: () => getTasks(projectId),
- refetchInterval: 5000,
+ refetchInterval: 30000,
enabled: !!projectId,
});
}