1
|
import { Paper } from '@mui/material'
|
2
|
import { useEffect, useState, useCallback } from 'react'
|
3
|
import { DropResult } from 'react-beautiful-dnd'
|
4
|
import { useDispatch, useSelector } from 'react-redux'
|
5
|
import { RootState } from '../../redux/store'
|
6
|
import { moveMarkerToDestination } from '../trackingToolSlice'
|
7
|
import { PathVariant } from '../trackingToolUtils'
|
8
|
import DragDropCtxWrapper from './DragDropCtxWrapper'
|
9
|
|
10
|
const MapPointDraggableList = () => {
|
11
|
const dispatch = useDispatch()
|
12
|
|
13
|
// List of all paths
|
14
|
const paths = useSelector(
|
15
|
(state: RootState) => state.trackingTool.pathVariants
|
16
|
)
|
17
|
|
18
|
// Primary path index - i.e. the selected path
|
19
|
const primaryPathIdx = useSelector(
|
20
|
(state: RootState) => state.trackingTool.primaryPathIdx
|
21
|
)
|
22
|
|
23
|
// Selected path as local state
|
24
|
const [path, setPath] = useState<PathVariant | undefined>()
|
25
|
|
26
|
// Set localstate path whenever it changes in the store
|
27
|
useEffect(() => {
|
28
|
if (!paths || paths.length < primaryPathIdx) {
|
29
|
setPath(undefined)
|
30
|
return
|
31
|
}
|
32
|
|
33
|
setPath(paths[primaryPathIdx])
|
34
|
}, [paths, primaryPathIdx])
|
35
|
|
36
|
const onDragEnd = useCallback(({ destination, source }: DropResult) => {
|
37
|
if (!destination || !source || destination.index === source.index) {
|
38
|
return
|
39
|
}
|
40
|
|
41
|
dispatch(
|
42
|
moveMarkerToDestination({
|
43
|
source: source.index,
|
44
|
destination: destination.index,
|
45
|
})
|
46
|
)
|
47
|
}, [dispatch])
|
48
|
|
49
|
return (
|
50
|
<Paper variant="outlined">
|
51
|
<DragDropCtxWrapper items={path ?? []} onDragEnd={onDragEnd} />
|
52
|
</Paper>
|
53
|
)
|
54
|
}
|
55
|
|
56
|
export default MapPointDraggableList
|