feat(drag-and-drop): enhance task grouping updates and socket event handling
- Updated the `useDragAndDrop` hook to emit specific grouping field change events (phase, priority, status) when tasks are moved between groups. - Refactored the task management slice to prevent direct updates to task grouping fields during drag-and-drop operations, ensuring these updates are handled via socket events after backend confirmation. - Introduced a new socket handler for task sort order changes to update task properties based on backend responses, improving synchronization between frontend and backend task states.
This commit is contained in:
@@ -989,6 +989,71 @@ export const useTaskSocketHandlers = () => {
|
||||
}
|
||||
}, [dispatch]);
|
||||
|
||||
// Handler for task sort order change events
|
||||
const handleTaskSortOrderChange = useCallback((data: any[]) => {
|
||||
try {
|
||||
if (!Array.isArray(data) || data.length === 0) return;
|
||||
|
||||
// DEBUG: Log the data received from the backend
|
||||
console.log('[TASK_SORT_ORDER_CHANGE] Received data:', data);
|
||||
|
||||
// Get canonical lists from Redux
|
||||
const state = store.getState();
|
||||
const priorityList = state.priorityReducer?.priorities || [];
|
||||
const phaseList = state.phaseReducer?.phaseList || [];
|
||||
const statusList = state.taskStatusReducer?.status || [];
|
||||
|
||||
// The backend sends an array of tasks with updated sort orders and possibly grouping fields
|
||||
data.forEach((taskData: any) => {
|
||||
const currentTask = state.taskManagement.entities[taskData.id];
|
||||
if (currentTask) {
|
||||
let updatedTask: Task = {
|
||||
...currentTask,
|
||||
order: taskData.sort_order || taskData.current_sort_order || currentTask.order,
|
||||
updatedAt: new Date().toISOString(),
|
||||
updated_at: new Date().toISOString(),
|
||||
};
|
||||
|
||||
// Update grouping fields if present
|
||||
if (typeof taskData.priority_id !== 'undefined') {
|
||||
const found = priorityList.find(p => p.id === taskData.priority_id);
|
||||
if (found) {
|
||||
updatedTask.priority = found.name;
|
||||
// updatedTask.priority_id = found.id; // Only if Task type has priority_id
|
||||
} else {
|
||||
updatedTask.priority = taskData.priority_id || '';
|
||||
// updatedTask.priority_id = taskData.priority_id;
|
||||
}
|
||||
}
|
||||
if (typeof taskData.phase_id !== 'undefined') {
|
||||
const found = phaseList.find(p => p.id === taskData.phase_id);
|
||||
if (found) {
|
||||
updatedTask.phase = found.name;
|
||||
// updatedTask.phase_id = found.id; // Only if Task type has phase_id
|
||||
} else {
|
||||
updatedTask.phase = taskData.phase_id || '';
|
||||
// updatedTask.phase_id = taskData.phase_id;
|
||||
}
|
||||
}
|
||||
if (typeof taskData.status_id !== 'undefined') {
|
||||
const found = statusList.find(s => s.id === taskData.status_id);
|
||||
if (found) {
|
||||
updatedTask.status = found.name;
|
||||
// updatedTask.status_id = found.id; // Only if Task type has status_id
|
||||
} else {
|
||||
updatedTask.status = taskData.status_id || '';
|
||||
// updatedTask.status_id = taskData.status_id;
|
||||
}
|
||||
}
|
||||
|
||||
dispatch(updateTask(updatedTask));
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('Error handling task sort order change event:', error);
|
||||
}
|
||||
}, [dispatch]);
|
||||
|
||||
// Register socket event listeners
|
||||
useEffect(() => {
|
||||
if (!socket) return;
|
||||
@@ -1022,6 +1087,7 @@ export const useTaskSocketHandlers = () => {
|
||||
{ event: SocketEvents.TASK_CUSTOM_COLUMN_UPDATE.toString(), handler: handleCustomColumnUpdate },
|
||||
{ event: SocketEvents.TASK_TIMER_START.toString(), handler: handleTimerStart },
|
||||
{ event: SocketEvents.TASK_TIMER_STOP.toString(), handler: handleTimerStop },
|
||||
{ event: SocketEvents.TASK_SORT_ORDER_CHANGE.toString(), handler: handleTaskSortOrderChange },
|
||||
|
||||
];
|
||||
|
||||
@@ -1056,6 +1122,7 @@ export const useTaskSocketHandlers = () => {
|
||||
handleCustomColumnUpdate,
|
||||
handleTimerStart,
|
||||
handleTimerStop,
|
||||
handleTaskSortOrderChange,
|
||||
|
||||
]);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user