import { DraggableLocation } from 'react-beautiful-dnd' ; export const moveItemWithinArray = ( arr , item , newIndex ) => { const arrClone = [ ... arr ] ; const oldIndex = arrClone . findIndex ( i => i . id === item . id ) ; arrClone . splice ( newIndex , 0 , arrClone . splice ( oldIndex , 1 ) [ 0 ] ) ; return arrClone ; } ; export const insertItemIntoArray = ( arr , item , index ) => { const arrClone = [ ... arr ] ; arrClone . splice ( index , 0 , item ) ; return arrClone ; } ; export const updateArrayItemById = ( arr , itemId , fields ) => { const arrClone = [ ... arr ] ; const item = arrClone . find ( ( { id } ) => id === itemId ) ; if ( item ) { const itemIndex = arrClone . indexOf ( item ) ; arrClone . splice ( itemIndex , 1 , { ... item , ... fields } ) ; } return arrClone ; } ; export const getNewDraggablePosition = ( afterDropDraggables , draggableIndex ) => { const prevDraggable = afterDropDraggables [ draggableIndex - 1 ] ; const nextDraggable = afterDropDraggables [ draggableIndex + 1 ] ; if ( ! prevDraggable && ! nextDraggable ) { return 65535 ; } if ( ! prevDraggable ) { return nextDraggable . position / 2.0 ; } if ( ! nextDraggable ) { return prevDraggable . position * 2.0 ; } const newPos = ( prevDraggable . position + nextDraggable . position ) / 2.0 ; return newPos ; } ; export const getSortedDraggables = ( draggables ) => { return draggables . sort ( ( a , b ) => a . position - b . position ) ; } ; export const isPositionChanged = ( source , destination ) => { if ( ! destination ) return false ; const isSameList = destination . droppableId === source . droppableId ; const isSamePosition = destination . index === source . index ; return ! isSameList || ! isSamePosition ; } ; export const getAfterDropDraggableList = ( beforeDropDraggables , droppedDraggable , isList , isSameList , destination ) => { if ( isList ) { return moveItemWithinArray ( beforeDropDraggables , droppedDraggable , destination . index ) ; } return isSameList ? moveItemWithinArray ( beforeDropDraggables , droppedDraggable , destination . index ) : insertItemIntoArray ( beforeDropDraggables , droppedDraggable , destination . index ) ; } ;