Skip to content

Commit fba7eb3

Browse files
committed
fix: handletasks properly
1 parent f94cf01 commit fba7eb3

1 file changed

Lines changed: 21 additions & 6 deletions

File tree

src/components/Projects/KanbanBoardView.tsx

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,15 +218,31 @@ export const KanbanBoardView: React.FC<KanbanBoardProps> = ({
218218
}
219219
}
220220

221-
const handleAddTask = (newTask: Omit<Task, 'id' | 'createdAt' | 'updatedAt'>) => {
222-
const task: Task = {
221+
const handleAddTask = async (newTask: Omit<Task, 'id' | 'createdAt' | 'updatedAt'>) => {
222+
// Create a temporary task for immediate UI feedback
223+
const temporaryTask: Task = {
223224
...newTask,
224225
id: Date.now(),
225226
createdAt: new Date().toISOString(),
226227
updatedAt: new Date().toISOString(),
227228
}
228-
setTasks((prev) => [...prev, task])
229-
onAddTask?.(newTask)
229+
230+
// Add temporary task to state immediately for UX
231+
setTasks((prev) => [...prev, temporaryTask])
232+
233+
try {
234+
// Wait for the real task to be created on the server
235+
const realTask = await onAddTask?.(newTask)
236+
237+
if (realTask) {
238+
// Replace the temporary task with the real one
239+
setTasks((prev) => prev.map((task) => (task.id === temporaryTask.id ? realTask : task)))
240+
}
241+
} catch (error) {
242+
// Remove temporary task if creation failed
243+
setTasks((prev) => prev.filter((task) => task.id !== temporaryTask.id))
244+
throw error
245+
}
230246
}
231247

232248
const handleUpdateTask = async (taskId: string, updates: Partial<Task>): Promise<void> => {
@@ -238,7 +254,7 @@ export const KanbanBoardView: React.FC<KanbanBoardProps> = ({
238254
}
239255

240256
if (selectedTask?.id === Number(taskId)) {
241-
setSelectedTask((prev) => ({ ...prev, ...updates } as Task))
257+
setSelectedTask((prev) => ({ ...prev, ...updates }) as Task)
242258
}
243259
}
244260

@@ -308,7 +324,6 @@ export const KanbanBoardView: React.FC<KanbanBoardProps> = ({
308324
const columnTasks = getTasksByStatus(column.status)
309325
const tasksByEpic = getTasksByEpic(columnTasks)
310326

311-
312327
return (
313328
<KanbanColumn
314329
key={column.id}

0 commit comments

Comments
 (0)