diff --git a/web/src/shared/components/Lists/index.tsx b/web/src/shared/components/Lists/index.tsx index 7b36639..f5e8469 100644 --- a/web/src/shared/components/Lists/index.tsx +++ b/web/src/shared/components/Lists/index.tsx @@ -1,53 +1,16 @@ import React, { useState } from 'react'; import { DragDropContext, Droppable, Draggable, DropResult } from 'react-beautiful-dnd'; -import { moveItemWithinArray, insertItemIntoArray } from 'shared/utils/arrays'; import List, { ListCards } from 'shared/components/List'; import Card from 'shared/components/Card'; import { Container } from './Styles'; import CardComposer from 'shared/components/CardComposer'; - -const getNewDraggablePosition = (afterDropDraggables: any, draggableIndex: any) => { - const prevDraggable = afterDropDraggables[draggableIndex - 1]; - const nextDraggable = afterDropDraggables[draggableIndex + 1]; - if (!prevDraggable && !nextDraggable) { - return 1; - } - if (!prevDraggable) { - return nextDraggable.position - 1; - } - if (!nextDraggable) { - return prevDraggable.position + 1; - } - const newPos = (prevDraggable.position + nextDraggable.position) / 2.0; - return newPos; -}; - -const getSortedDraggables = (draggables: any) => { - return draggables.sort((a: any, b: any) => a.position - b.position); -}; - -const isPositionChanged = (source: any, destination: any) => { - if (!destination) return false; - const isSameList = destination.droppableId === source.droppableId; - const isSamePosition = destination.index === source.index; - return !isSameList || !isSamePosition; -}; - -const getAfterDropDraggableList = ( - beforeDropDraggables: any, - droppedDraggable: any, - isList: any, - isSameList: any, - destination: any, -) => { - if (isList) { - return moveItemWithinArray(beforeDropDraggables, droppedDraggable, destination.index); - } - return isSameList - ? moveItemWithinArray(beforeDropDraggables, droppedDraggable, destination.index) - : insertItemIntoArray(beforeDropDraggables, droppedDraggable, destination.index); -}; +import { + isPositionChanged, + getSortedDraggables, + getNewDraggablePosition, + getAfterDropDraggableList, +} from 'shared/utils/draggables'; interface Columns { [key: string]: TaskGroup; diff --git a/web/src/shared/utils/draggables.ts b/web/src/shared/utils/draggables.ts new file mode 100644 index 0000000..db9605d --- /dev/null +++ b/web/src/shared/utils/draggables.ts @@ -0,0 +1,44 @@ +import { moveItemWithinArray, insertItemIntoArray } from 'shared/utils/arrays'; + +export const getNewDraggablePosition = (afterDropDraggables: any, draggableIndex: any) => { + const prevDraggable = afterDropDraggables[draggableIndex - 1]; + const nextDraggable = afterDropDraggables[draggableIndex + 1]; + if (!prevDraggable && !nextDraggable) { + return 1; + } + if (!prevDraggable) { + return nextDraggable.position - 1; + } + if (!nextDraggable) { + return prevDraggable.position + 1; + } + const newPos = (prevDraggable.position + nextDraggable.position) / 2.0; + return newPos; +}; + +export const getSortedDraggables = (draggables: any) => { + return draggables.sort((a: any, b: any) => a.position - b.position); +}; + +export const isPositionChanged = (source: any, destination: any) => { + if (!destination) return false; + const isSameList = destination.droppableId === source.droppableId; + const isSamePosition = destination.index === source.index; + return !isSameList || !isSamePosition; +}; + +export const getAfterDropDraggableList = ( + beforeDropDraggables: any, + droppedDraggable: any, + isList: any, + isSameList: any, + destination: any, +) => { + if (isList) { + return moveItemWithinArray(beforeDropDraggables, droppedDraggable, destination.index); + } + return isSameList + ? moveItemWithinArray(beforeDropDraggables, droppedDraggable, destination.index) + : insertItemIntoArray(beforeDropDraggables, droppedDraggable, destination.index); +}; +