Projekt

Obecné

Profil

Stáhnout (1.6 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { Paper } from '@mui/material'
2
import { useEffect, useState } from 'react'
3
import { DropResult } from 'react-beautiful-dnd'
4
import { useDispatch, useSelector } from 'react-redux'
5
import { RootState } from '../../redux/store'
6
import { swapMapMarkerIndices } from '../trackingToolSlice'
7
import { PathVariant } from '../Map/pathUtils'
8
import DraggableList from './DraggableList'
9

    
10
const DraggableMarkerList = () => {
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 = ({ destination, source }: DropResult) => {
37
        if (!destination || !source || destination.index === source.index) {
38
            return
39
        }
40

    
41
        dispatch(
42
            swapMapMarkerIndices({
43
                source: source.index,
44
                destination: destination.index,
45
            })
46
        )
47
    }
48

    
49
    return (
50
        <Paper variant="outlined">
51
            <DraggableList items={path ?? []} onDragEnd={onDragEnd} />
52
        </Paper>
53
    )
54
}
55

    
56
export default DraggableMarkerList
(3-3/3)