YuJian920
3 years ago
8 changed files with 179 additions and 15 deletions
@ -0,0 +1,59 @@
@@ -0,0 +1,59 @@
|
||||
import { useCallback } from "react"; |
||||
import { DropResult } from "react-beautiful-dnd"; |
||||
import { |
||||
useKanbans, |
||||
useKanbanSearchParams, |
||||
useReorderKanban, |
||||
} from "./useKanban"; |
||||
import { useReorderTask, useTasks, useTasksSearchParams } from "./useTask"; |
||||
|
||||
const useDragEnd = () => { |
||||
const { data: kanbans } = useKanbans(useKanbanSearchParams()); |
||||
const { mutate: reorderKanban } = useReorderKanban(); |
||||
const { data: allTasks = [] } = useTasks(useTasksSearchParams()); |
||||
const { mutate: reorderTask } = useReorderTask(); |
||||
return useCallback( |
||||
({ source, destination, type }: DropResult) => { |
||||
if (!destination) return; |
||||
|
||||
if (type === "COLUMN") { |
||||
const fromId = kanbans?.[source.index].id; |
||||
const toId = kanbans?.[destination.index].id; |
||||
if (!fromId || !toId || fromId === toId) return; |
||||
|
||||
const dndType = source.index > destination.index ? "before" : "after"; |
||||
reorderKanban({ fromId, referenceId: toId, type: dndType }); |
||||
} |
||||
|
||||
if (type === "ROW") { |
||||
const fromKanbanId = +source.droppableId; |
||||
const toKanbanId = +destination.droppableId; |
||||
|
||||
if (fromKanbanId === toKanbanId) return; |
||||
|
||||
const fromTask = allTasks.filter( |
||||
(task) => task.kanbanId === fromKanbanId |
||||
)[source.index]; |
||||
const toTask = allTasks.filter((task) => task.kanbanId === toKanbanId)[ |
||||
destination.index |
||||
]; |
||||
|
||||
if (fromTask?.id === toTask?.id) return; |
||||
|
||||
reorderTask({ |
||||
fromId: fromTask?.id, |
||||
referenceId: toTask?.id, |
||||
fromKanbanId, |
||||
toKanbanId, |
||||
type: |
||||
fromKanbanId === toKanbanId && destination.index > source.index |
||||
? "after" |
||||
: "before", |
||||
}); |
||||
} |
||||
}, |
||||
[kanbans, reorderKanban, allTasks, reorderTask] |
||||
); |
||||
}; |
||||
|
||||
export default useDragEnd; |
@ -0,0 +1,56 @@
@@ -0,0 +1,56 @@
|
||||
/** |
||||
* 在本地对排序进行乐观更新 |
||||
* @param fromId 要排序的项目的id |
||||
* @param type 'before' | 'after' |
||||
* @param referenceId 参照id |
||||
* @param list 要排序的列表, 比如tasks, kanbans |
||||
*/ |
||||
export const reorder = ({ |
||||
fromId, |
||||
type, |
||||
referenceId, |
||||
list, |
||||
}: { |
||||
list: { id: number }[]; |
||||
fromId: number; |
||||
type: "after" | "before"; |
||||
referenceId: number; |
||||
}) => { |
||||
const copiedList = [...list]; |
||||
// 找到fromId对应项目的下标
|
||||
const movingItemIndex = copiedList.findIndex((item) => item.id === fromId); |
||||
if (!referenceId) { |
||||
return insertAfter([...copiedList], movingItemIndex, copiedList.length - 1); |
||||
} |
||||
const targetIndex = copiedList.findIndex((item) => item.id === referenceId); |
||||
const insert = type === "after" ? insertAfter : insertBefore; |
||||
return insert([...copiedList], movingItemIndex, targetIndex); |
||||
}; |
||||
|
||||
/** |
||||
* 在list中,把from放在to的前边 |
||||
* @param list |
||||
* @param from |
||||
* @param to |
||||
*/ |
||||
const insertBefore = (list: unknown[], from: number, to: number) => { |
||||
const toItem = list[to]; |
||||
const removedItem = list.splice(from, 1)[0]; |
||||
const toIndex = list.indexOf(toItem); |
||||
list.splice(toIndex, 0, removedItem); |
||||
return list; |
||||
}; |
||||
|
||||
/** |
||||
* 在list中,把from放在to的后面 |
||||
* @param list |
||||
* @param from |
||||
* @param to |
||||
*/ |
||||
const insertAfter = (list: unknown[], from: number, to: number) => { |
||||
const toItem = list[to]; |
||||
const removedItem = list.splice(from, 1)[0]; |
||||
const toIndex = list.indexOf(toItem); |
||||
list.splice(toIndex + 1, 0, removedItem); |
||||
return list; |
||||
}; |
Loading…
Reference in new issue