1
|
import { Fragment, FunctionComponent, 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
|
|
10
|
// CatalogItemDto wrapper to keep track whether the item is active or not
|
11
|
class DisplayableMapPoint {
|
12
|
constructor(
|
13
|
public readonly catalogItem: CatalogItemDto,
|
14
|
public active: boolean = true
|
15
|
) {}
|
16
|
}
|
17
|
|
18
|
export interface MapPathProps {
|
19
|
pathVariant: PathVariant // aka CatalogItemDto[]
|
20
|
active: boolean // whether this component is active
|
21
|
idx: number // index of path in the list
|
22
|
primaryIdx: number // reference to index of path which has primary color
|
23
|
setPrimary: (idx: number) => void // callback to set the primary path
|
24
|
}
|
25
|
|
26
|
// Blue
|
27
|
export const primaryPathColor = '#346eeb'
|
28
|
|
29
|
// Grey
|
30
|
export const secondaryPathColor = '#878e9c'
|
31
|
|
32
|
// Map path component
|
33
|
const MapPath: FunctionComponent<MapPathProps> = ({
|
34
|
pathVariant,
|
35
|
active,
|
36
|
primaryIdx,
|
37
|
idx,
|
38
|
setPrimary
|
39
|
}) => {
|
40
|
// List of all map points that belong to the path
|
41
|
const [mapPoints, setMapPoints] = useState<DisplayableMapPoint[]>(
|
42
|
pathVariant
|
43
|
.filter((item) => item.latitude && item.longitude)
|
44
|
.map((item) => new DisplayableMapPoint(item))
|
45
|
)
|
46
|
|
47
|
|
48
|
const getActiveMapPoints = () => mapPoints.filter((item) => item.active)
|
49
|
|
50
|
// Builds all edges of the path
|
51
|
const buildEdges = () => {
|
52
|
const activeMapPoints = getActiveMapPoints()
|
53
|
if (activeMapPoints.length < 2) {
|
54
|
return null
|
55
|
}
|
56
|
|
57
|
// Create path edges
|
58
|
const edges: any[] = []
|
59
|
for (let i = 0; i < activeMapPoints.length - 1; i += 1) {
|
60
|
edges.push(
|
61
|
<TextPath
|
62
|
positions={[
|
63
|
[
|
64
|
activeMapPoints[i].catalogItem.latitude,
|
65
|
activeMapPoints[i].catalogItem.longitude,
|
66
|
],
|
67
|
[
|
68
|
activeMapPoints[i + 1].catalogItem.latitude,
|
69
|
activeMapPoints[i + 1].catalogItem.longitude,
|
70
|
],
|
71
|
]}
|
72
|
text="►"
|
73
|
attributes={{
|
74
|
'font-size': 25,
|
75
|
// Set to primaryPathColor if primary index in the tracking tool is equal to this index
|
76
|
fill: primaryIdx === idx ? primaryPathColor : secondaryPathColor,
|
77
|
}}
|
78
|
onClick={() => {
|
79
|
setPrimary(idx)
|
80
|
console.log('hehehe')
|
81
|
}
|
82
|
}
|
83
|
repeat
|
84
|
center
|
85
|
weight={9}
|
86
|
/>
|
87
|
)
|
88
|
}
|
89
|
|
90
|
// Return the path
|
91
|
return edges
|
92
|
}
|
93
|
|
94
|
/**
|
95
|
* Creates a list of all vertices in the path
|
96
|
*/
|
97
|
const buildVertices = () => {
|
98
|
return mapPoints.map((mapPoint, idx) => (
|
99
|
<Marker
|
100
|
key={idx}
|
101
|
position={[
|
102
|
mapPoint.catalogItem.latitude as number,
|
103
|
mapPoint.catalogItem.longitude as number,
|
104
|
]}
|
105
|
>
|
106
|
<Popup>
|
107
|
<Fragment>
|
108
|
<Stack direction="column" sx={{ m: 0 }}>
|
109
|
<Typography
|
110
|
variant="h6"
|
111
|
fontWeight="bold"
|
112
|
fontSize={16}
|
113
|
>
|
114
|
{formatHtmlStringToReactDom(mapPoint.catalogItem.name as string)}
|
115
|
</Typography>
|
116
|
<FormControlLabel
|
117
|
control={
|
118
|
<Checkbox
|
119
|
checked={mapPoint.active}
|
120
|
onChange={() => {
|
121
|
mapPoint.active = !mapPoint.active
|
122
|
setMapPoints([...mapPoints])
|
123
|
}}
|
124
|
/>
|
125
|
}
|
126
|
labelPlacement="end"
|
127
|
label="Active"
|
128
|
/>
|
129
|
<CatalogItemDetailDialog itemId={mapPoint.catalogItem.id ?? ''} />
|
130
|
</Stack>
|
131
|
</Fragment>
|
132
|
</Popup>
|
133
|
</Marker>
|
134
|
))
|
135
|
}
|
136
|
|
137
|
return (
|
138
|
<Fragment>
|
139
|
{active && <Fragment>
|
140
|
{buildVertices()}
|
141
|
{buildEdges()}
|
142
|
</Fragment>}
|
143
|
</Fragment>
|
144
|
)
|
145
|
}
|
146
|
|
147
|
export default MapPath
|