Projekt

Obecné

Profil

Stáhnout (6.16 KB) Statistiky
| Větev: | Tag: | Revize:
1 48690561 Vaclav Honzik
import { Fragment, useEffect, useState } from 'react'
2 a7ae217f Vaclav Honzik
import { useDispatch, useSelector } from 'react-redux'
3 812b9f90 Vaclav Honzik
import { RootState } from '../../redux/store'
4 48690561 Vaclav Honzik
import { MapPoint, isMapPointDisplayable } from '../trackingToolUtils'
5 a7ae217f Vaclav Honzik
import TextPath from 'react-leaflet-textpath'
6 48690561 Vaclav Honzik
import { updateMapMarker } from '../trackingToolSlice'
7 a7ae217f Vaclav Honzik
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 812b9f90 Vaclav Honzik
import { formatHtmlStringToReactDom } from '../../../utils/formatting/HtmlUtils'
12
import { DialogCatalogItemDetail as CatalogItemDetailDialog } from '../../Catalog/CatalogItemDetail'
13 b70813cb Vaclav Honzik
14 de12c6be Vaclav Honzik
type EdgeElement = any
15 48690561 Vaclav Honzik
const MapPath = () => {
16 dd270a41 Vaclav Honzik
    const dispatch = useDispatch()
17 48690561 Vaclav Honzik
    const path = useSelector(
18
        (state: RootState) => state.trackingTool.displayedPath
19 8c57f958 Vaclav Honzik
    )
20 48690561 Vaclav Honzik
    // Color of the path
21
    const pathColor = '#346eeb'
22 8c57f958 Vaclav Honzik
23 de12c6be Vaclav Honzik
    // List of all active map points
24
    const [displayableMapPoints, setDisplayableMapPoints] = useState<
25
        MapPoint[]
26
    >([])
27 8c57f958 Vaclav Honzik
    useEffect(() => {
28 48690561 Vaclav Honzik
        if (!path) {
29
            setDisplayableMapPoints([])
30
            return
31
        }
32
33 de12c6be Vaclav Honzik
        // Set all displayable vertices
34 a7ae217f Vaclav Honzik
        setDisplayableMapPoints(
35
            path.filter((mapPoint) => isMapPointDisplayable(mapPoint))
36
        )
37 de12c6be Vaclav Honzik
    }, [path])
38 b70813cb Vaclav Honzik
39 de12c6be Vaclav Honzik
    // List of all edges in the path
40
    const [edges, setEdges] = useState<EdgeElement[]>([])
41 dd270a41 Vaclav Honzik
    useEffect(() => {
42 de12c6be Vaclav Honzik
        // Get all active map points
43
        const activeMapPoints = displayableMapPoints.filter(
44 f41a4cd3 Vaclav Honzik
            (item) => item.addToPath && !item.hidden
45 dd270a41 Vaclav Honzik
        )
46 b70813cb Vaclav Honzik
        if (activeMapPoints.length < 2) {
47 7b864a5c Vaclav Honzik
            setEdges([])
48 de12c6be Vaclav Honzik
            return
49 b70813cb Vaclav Honzik
        }
50
51 de12c6be Vaclav Honzik
        // Build edges
52
        const edges = []
53 b70813cb Vaclav Honzik
        for (let i = 0; i < activeMapPoints.length - 1; i += 1) {
54 de12c6be Vaclav Honzik
            const [start, end] = [
55
                activeMapPoints[i].catalogItem,
56
                activeMapPoints[i + 1].catalogItem,
57
            ]
58 b70813cb Vaclav Honzik
            edges.push(
59
                <TextPath
60 7d477849 Vaclav Honzik
                    key={`${activeMapPoints[i].id}-${
61
                        activeMapPoints[i + 1].id
62
                    }`}
63 b70813cb Vaclav Honzik
                    positions={[
64 de12c6be Vaclav Honzik
                        [start.latitude, start.longitude],
65
                        [end.latitude, end.longitude],
66 b70813cb Vaclav Honzik
                    ]}
67
                    text="►"
68
                    attributes={{
69 a7ae217f Vaclav Honzik
                        'font-size': 19,
70 48690561 Vaclav Honzik
                        fill: pathColor,
71 b70813cb Vaclav Honzik
                    }}
72
                    repeat
73
                    center
74 04fdedc6 Vaclav Honzik
                    weight={0}
75 b70813cb Vaclav Honzik
                />
76
            )
77
        }
78 de12c6be Vaclav Honzik
        setEdges(edges)
79 48690561 Vaclav Honzik
    }, [dispatch, displayableMapPoints])
80 b70813cb Vaclav Honzik
81 de12c6be Vaclav Honzik
    // 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 c0b66eaf Vaclav Honzik
                    key={item.id}
89 de12c6be Vaclav Honzik
                    position={[
90
                        item.catalogItem.latitude as number,
91
                        item.catalogItem.longitude as number,
92
                    ]}
93 c0b66eaf Vaclav Honzik
                    mapPoint={item}
94 de12c6be Vaclav Honzik
                    updatePositionCallbackFn={(position: LatLngTuple) => {
95
                        dispatch(
96 48690561 Vaclav Honzik
                            updateMapMarker({
97
                                ...item,
98
                                catalogItem: {
99
                                    ...item.catalogItem,
100
                                    latitude: position[0],
101
                                    longitude: position[1],
102 a7ae217f Vaclav Honzik
                                },
103 de12c6be Vaclav Honzik
                            })
104
                        )
105
                    }}
106
                >
107 11fca75a Vaclav Honzik
                    <Fragment>
108
                        <Tooltip>
109
                            {/* <Typography> */}
110 a7ae217f Vaclav Honzik
                            {item.catalogItem.name ?? ''}
111 11fca75a Vaclav Honzik
                            {/* </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 f41a4cd3 Vaclav Honzik
                                                checked={item.addToPath}
129 11fca75a Vaclav Honzik
                                                onChange={() => {
130
                                                    dispatch(
131
                                                        updateMapMarker({
132 7d477849 Vaclav Honzik
                                                            ...item,
133 48690561 Vaclav Honzik
                                                            addToPath:
134
                                                                !item.addToPath,
135 11fca75a Vaclav Honzik
                                                        })
136
                                                    )
137
                                                }}
138
                                            />
139
                                        }
140
                                        labelPlacement="end"
141
                                        label="Active"
142
                                    />
143
                                    <CatalogItemDetailDialog
144 a7ae217f Vaclav Honzik
                                        itemId={item.catalogItem.id ?? ''}
145 11fca75a Vaclav Honzik
                                    />
146
                                </Stack>
147
                            </Fragment>
148
                        </Popup>
149
                    </Fragment>
150 de12c6be Vaclav Honzik
                </MapMarker>
151
            ))
152
        )
153 48690561 Vaclav Honzik
    }, [dispatch, displayableMapPoints])
154 b70813cb Vaclav Honzik
155
    return (
156
        <Fragment>
157 de12c6be Vaclav Honzik
            {vertices}
158
            {edges}
159 b70813cb Vaclav Honzik
        </Fragment>
160
    )
161
}
162
163
export default MapPath