Projekt

Obecné

Profil

Stáhnout (6.16 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { Fragment, useEffect, useState } from 'react'
2
import { useDispatch, useSelector } from 'react-redux'
3
import { RootState } from '../../redux/store'
4
import { MapPoint, isMapPointDisplayable } from '../trackingToolUtils'
5
import TextPath from 'react-leaflet-textpath'
6
import { updateMapMarker } from '../trackingToolSlice'
7
import MapMarker from './MapMarker'
8
import { LatLngTuple } from 'leaflet'
9
import { Popup, Tooltip } from 'react-leaflet'
10
import { Checkbox, FormControlLabel, Stack, Typography } from '@mui/material'
11
import { formatHtmlStringToReactDom } from '../../../utils/formatting/HtmlUtils'
12
import { DialogCatalogItemDetail as CatalogItemDetailDialog } from '../../Catalog/CatalogItemDetail'
13

    
14
type EdgeElement = any
15
const MapPath = () => {
16
    const dispatch = useDispatch()
17
    const path = useSelector(
18
        (state: RootState) => state.trackingTool.displayedPath
19
    )
20
    // Color of the path
21
    const pathColor = '#346eeb'
22

    
23
    // List of all active map points
24
    const [displayableMapPoints, setDisplayableMapPoints] = useState<
25
        MapPoint[]
26
    >([])
27
    useEffect(() => {
28
        if (!path) {
29
            setDisplayableMapPoints([])
30
            return
31
        }
32

    
33
        // Set all displayable vertices
34
        setDisplayableMapPoints(
35
            path.filter((mapPoint) => isMapPointDisplayable(mapPoint))
36
        )
37
    }, [path])
38

    
39
    // List of all edges in the path
40
    const [edges, setEdges] = useState<EdgeElement[]>([])
41
    useEffect(() => {
42
        // Get all active map points
43
        const activeMapPoints = displayableMapPoints.filter(
44
            (item) => item.addToPath && !item.hidden
45
        )
46
        if (activeMapPoints.length < 2) {
47
            setEdges([])
48
            return
49
        }
50

    
51
        // Build edges
52
        const edges = []
53
        for (let i = 0; i < activeMapPoints.length - 1; i += 1) {
54
            const [start, end] = [
55
                activeMapPoints[i].catalogItem,
56
                activeMapPoints[i + 1].catalogItem,
57
            ]
58
            edges.push(
59
                <TextPath
60
                    key={`${activeMapPoints[i].id}-${
61
                        activeMapPoints[i + 1].id
62
                    }`}
63
                    positions={[
64
                        [start.latitude, start.longitude],
65
                        [end.latitude, end.longitude],
66
                    ]}
67
                    text="►"
68
                    attributes={{
69
                        'font-size': 19,
70
                        fill: pathColor,
71
                    }}
72
                    repeat
73
                    center
74
                    weight={0}
75
                />
76
            )
77
        }
78
        setEdges(edges)
79
    }, [dispatch, displayableMapPoints])
80

    
81
    // List of vertices to display
82
    const [vertices, setVertices] = useState<JSX.Element[]>([])
83
    useEffect(() => {
84
        // Iterate over all displayable map points and map them to MapMarker
85
        setVertices(
86
            displayableMapPoints.map((item) => (
87
                <MapMarker
88
                    key={item.id}
89
                    position={[
90
                        item.catalogItem.latitude as number,
91
                        item.catalogItem.longitude as number,
92
                    ]}
93
                    mapPoint={item}
94
                    updatePositionCallbackFn={(position: LatLngTuple) => {
95
                        dispatch(
96
                            updateMapMarker({
97
                                ...item,
98
                                catalogItem: {
99
                                    ...item.catalogItem,
100
                                    latitude: position[0],
101
                                    longitude: position[1],
102
                                },
103
                            })
104
                        )
105
                    }}
106
                >
107
                    <Fragment>
108
                        <Tooltip>
109
                            {/* <Typography> */}
110
                            {item.catalogItem.name ?? ''}
111
                            {/* </Typography> */}
112
                        </Tooltip>
113
                        <Popup>
114
                            <Fragment>
115
                                <Stack direction="column" sx={{ m: 0 }}>
116
                                    <Typography
117
                                        variant="h6"
118
                                        fontWeight="bold"
119
                                        fontSize={16}
120
                                    >
121
                                        {formatHtmlStringToReactDom(
122
                                            item.catalogItem.name as string
123
                                        )}
124
                                    </Typography>
125
                                    <FormControlLabel
126
                                        control={
127
                                            <Checkbox
128
                                                checked={item.addToPath}
129
                                                onChange={() => {
130
                                                    dispatch(
131
                                                        updateMapMarker({
132
                                                            ...item,
133
                                                            addToPath:
134
                                                                !item.addToPath,
135
                                                        })
136
                                                    )
137
                                                }}
138
                                            />
139
                                        }
140
                                        labelPlacement="end"
141
                                        label="Active"
142
                                    />
143
                                    <CatalogItemDetailDialog
144
                                        itemId={item.catalogItem.id ?? ''}
145
                                    />
146
                                </Stack>
147
                            </Fragment>
148
                        </Popup>
149
                    </Fragment>
150
                </MapMarker>
151
            ))
152
        )
153
    }, [dispatch, displayableMapPoints])
154

    
155
    return (
156
        <Fragment>
157
            {vertices}
158
            {edges}
159
        </Fragment>
160
    )
161
}
162

    
163
export default MapPath
(3-3/5)