1
|
import { Fragment, FunctionComponent, useEffect, useState } from 'react'
|
2
|
import { CatalogItemDto } from '../../swagger/data-contracts'
|
3
|
import { PathVariant } from './buildPathVariants'
|
4
|
import TextPath from 'react-leaflet-textpath'
|
5
|
import { Marker, Popup } from 'react-leaflet'
|
6
|
import { Checkbox, FormControlLabel, Stack, Typography } from '@mui/material'
|
7
|
import { formatHtmlStringToReactDom } from '../../utils/formatting/HtmlUtils'
|
8
|
import { DialogCatalogItemDetail as CatalogItemDetailDialog } from '../Catalog/CatalogItemDetail'
|
9
|
import { useDispatch, useSelector } from 'react-redux'
|
10
|
import { RootState } from '../redux/store'
|
11
|
|
12
|
// CatalogItemDto wrapper to keep track whether the item is active or not
|
13
|
class DisplayableMapPoint {
|
14
|
constructor(
|
15
|
public readonly catalogItem: CatalogItemDto,
|
16
|
public active: boolean = true
|
17
|
) {}
|
18
|
}
|
19
|
|
20
|
export interface MapPathProps {
|
21
|
pathVariant: PathVariant // aka CatalogItemDto[]
|
22
|
idx: number // index of path in the list
|
23
|
}
|
24
|
|
25
|
// Blue
|
26
|
export const primaryPathColor = '#346eeb'
|
27
|
|
28
|
// Grey
|
29
|
export const secondaryPathColor = '#878e9c'
|
30
|
|
31
|
// Map path component
|
32
|
const MapPath: FunctionComponent<MapPathProps> = ({ idx, pathVariant }) => {
|
33
|
// List of all map points that belong to the path
|
34
|
const [mapPoints, setMapPoints] = useState<DisplayableMapPoint[]>(
|
35
|
pathVariant
|
36
|
.filter((item) => item.latitude && item.longitude)
|
37
|
.map((item) => new DisplayableMapPoint(item))
|
38
|
)
|
39
|
|
40
|
// Set of all active paths
|
41
|
const activePaths = useSelector(
|
42
|
(state: RootState) => state.trackingTool.activePaths
|
43
|
)
|
44
|
// Index of the primary path
|
45
|
const primaryPathIdx = useSelector(
|
46
|
(state: RootState) => state.trackingTool.primaryPathIdx
|
47
|
)
|
48
|
|
49
|
// Whether the path is active or not
|
50
|
const [active, setActive] = useState(false)
|
51
|
useEffect(() => {
|
52
|
setActive(activePaths.has(idx))
|
53
|
}, [activePaths, idx])
|
54
|
|
55
|
const getActiveMapPoints = () => mapPoints.filter((item) => item.active)
|
56
|
|
57
|
const dispatch = useDispatch()
|
58
|
|
59
|
// Builds all edges of the path
|
60
|
const buildEdges = () => {
|
61
|
const activeMapPoints = getActiveMapPoints()
|
62
|
if (activeMapPoints.length < 2) {
|
63
|
return null
|
64
|
}
|
65
|
|
66
|
// Create path edges
|
67
|
const edges: any[] = []
|
68
|
for (let i = 0; i < activeMapPoints.length - 1; i += 1) {
|
69
|
edges.push(
|
70
|
<TextPath
|
71
|
positions={[
|
72
|
[
|
73
|
activeMapPoints[i].catalogItem.latitude,
|
74
|
activeMapPoints[i].catalogItem.longitude,
|
75
|
],
|
76
|
[
|
77
|
activeMapPoints[i + 1].catalogItem.latitude,
|
78
|
activeMapPoints[i + 1].catalogItem.longitude,
|
79
|
],
|
80
|
]}
|
81
|
text="►"
|
82
|
attributes={{
|
83
|
'font-size': 25,
|
84
|
// Set to primaryPathColor if primary index in the tracking tool is equal to this index
|
85
|
fill:
|
86
|
primaryPathIdx === idx
|
87
|
? primaryPathColor
|
88
|
: secondaryPathColor,
|
89
|
}}
|
90
|
onClick={() => {
|
91
|
dispatch(setPrimaryIdx(idx))
|
92
|
}}
|
93
|
repeat
|
94
|
center
|
95
|
weight={9}
|
96
|
/>
|
97
|
)
|
98
|
}
|
99
|
|
100
|
// Return the path
|
101
|
return edges
|
102
|
}
|
103
|
|
104
|
/**
|
105
|
* Creates a list of all vertices in the path
|
106
|
*/
|
107
|
const buildVertices = () => {
|
108
|
return mapPoints.map((mapPoint, idx) => (
|
109
|
<Marker
|
110
|
key={idx}
|
111
|
position={[
|
112
|
mapPoint.catalogItem.latitude as number,
|
113
|
mapPoint.catalogItem.longitude as number,
|
114
|
]}
|
115
|
>
|
116
|
<Popup>
|
117
|
<Fragment>
|
118
|
<Stack direction="column" sx={{ m: 0 }}>
|
119
|
<Typography
|
120
|
variant="h6"
|
121
|
fontWeight="bold"
|
122
|
fontSize={16}
|
123
|
>
|
124
|
{formatHtmlStringToReactDom(
|
125
|
mapPoint.catalogItem.name as string
|
126
|
)}
|
127
|
</Typography>
|
128
|
<FormControlLabel
|
129
|
control={
|
130
|
<Checkbox
|
131
|
checked={mapPoint.active}
|
132
|
onChange={() => {
|
133
|
mapPoint.active = !mapPoint.active
|
134
|
setMapPoints([...mapPoints])
|
135
|
}}
|
136
|
/>
|
137
|
}
|
138
|
labelPlacement="end"
|
139
|
label="Active"
|
140
|
/>
|
141
|
<CatalogItemDetailDialog
|
142
|
itemId={mapPoint.catalogItem.id ?? ''}
|
143
|
/>
|
144
|
</Stack>
|
145
|
</Fragment>
|
146
|
</Popup>
|
147
|
</Marker>
|
148
|
))
|
149
|
}
|
150
|
|
151
|
return (
|
152
|
<Fragment>
|
153
|
{active && (
|
154
|
<Fragment>
|
155
|
{buildVertices()}
|
156
|
{buildEdges()}
|
157
|
</Fragment>
|
158
|
)}
|
159
|
</Fragment>
|
160
|
)
|
161
|
}
|
162
|
|
163
|
export default MapPath
|
164
|
function setPrimaryIdx(idx: number): any {
|
165
|
throw new Error('Function not implemented.')
|
166
|
}
|