Revize 8c57f958
Přidáno uživatelem Václav Honzík před asi 2 roky(ů)
frontend/src/features/TrackingTool/TrackingTool.tsx | ||
---|---|---|
1 |
import { Card, CardContent, Grid, Stack, Typography } from '@mui/material' |
|
1 |
import { |
|
2 |
Button, |
|
3 |
Card, |
|
4 |
CardContent, |
|
5 |
Grid, |
|
6 |
Stack, |
|
7 |
Typography, |
|
8 |
} from '@mui/material' |
|
2 | 9 |
import { Fragment, useEffect, useState } from 'react' |
3 |
import { MapContainer, TileLayer } from 'react-leaflet' |
|
10 |
import { MapContainer, TileLayer, useMap } from 'react-leaflet'
|
|
4 | 11 |
import mapConfig from '../../config/mapConfig' |
5 | 12 |
import TextPath from 'react-leaflet-textpath' |
6 | 13 |
import PlaintextUpload from './PlaintextUpload' |
... | ... | |
9 | 16 |
import DeleteIcon from '@mui/icons-material/Delete' |
10 | 17 |
import { PathDto } from '../../swagger/data-contracts' |
11 | 18 |
import { formatHtmlStringToReactDom } from '../../utils/formatting/HtmlUtils' |
12 |
import buildPathVariants, { PathVariant } from './buildPathVariants' |
|
13 | 19 |
import MapPath from './MapPath' |
20 |
import EditIcon from '@mui/icons-material/Edit' |
|
21 |
import { useSelector } from 'react-redux' |
|
22 |
import { RootState } from '../redux/store' |
|
14 | 23 |
|
15 | 24 |
// Page with tracking tool |
16 | 25 |
const TrackingTool = () => { |
17 | 26 |
// Path response from the API |
18 |
const [pathDto, setPathDto] = useState<PathDto | undefined>(undefined) |
|
27 |
const pathDto = useSelector((state: RootState) => state.trackingTool.pathDto) |
|
28 |
const pathVariants = useSelector((state: RootState) => state.trackingTool.pathVariants) |
|
29 |
const mapCenter = useSelector((state: RootState) => state.trackingTool.mapCenter) |
|
19 | 30 |
|
20 |
// Whether the path variants are building |
|
21 |
const [buildingPathVariants, setBuildingPathVariants] = |
|
22 |
useState<boolean>(false) |
|
31 |
// const map = useMap() |
|
23 | 32 |
|
24 |
// List of all computed path variants |
|
25 |
const [pathVariants, setPathVariants] = useState<PathVariant[] | undefined>( |
|
26 |
undefined |
|
27 |
) |
|
28 |
|
|
29 |
// latitude longitude pair of where the map is to be centered |
|
30 |
const [mapCenter, setMapCenter] = useState<number[]>([ |
|
31 |
mapConfig.defaultCoordinates[0], |
|
32 |
mapConfig.defaultCoordinates[1], |
|
33 |
]) |
|
34 |
|
|
35 |
const [primaryPathIdx, setActivePathIdx] = useState(0) |
|
36 |
|
|
37 |
// TODO make some sort of selection list to select active paths? |
|
38 |
const [activePaths, setActivePaths] = useState<Set<number>>(new Set()) |
|
33 |
// // Set the map center |
|
34 |
// useEffect(() => { |
|
35 |
// map.flyTo(mapCenter, mapConfig.defaultZoom) |
|
36 |
// }, [map, mapCenter]) |
|
39 | 37 |
|
40 |
// Returns tuple of average latitude and longitude |
|
41 |
const calculateMapCenter = (pathVariant: PathVariant) => [ |
|
42 |
pathVariant |
|
43 |
.map((item) => item.latitude ?? 0) |
|
44 |
.reduce((a, b) => a + b, 0) / pathVariant.length, |
|
45 |
pathVariant |
|
46 |
.map((item) => item.longitude ?? 0) |
|
47 |
.reduce((a, b) => a + b, 0) / pathVariant.length, |
|
48 |
] |
|
49 |
|
|
50 |
useEffect(() => { |
|
51 |
if (!pathVariants || pathVariants.length === 0) { |
|
52 |
return |
|
53 |
} |
|
54 |
// TODO calculate only for path that has some non-null coordinates |
|
55 |
setMapCenter(calculateMapCenter(pathVariants[0])) |
|
56 |
}, [pathVariants]) |
|
57 |
|
|
58 |
useEffect(() => { |
|
59 |
if (!pathDto) { |
|
60 |
return |
|
61 |
} |
|
62 |
|
|
63 |
setBuildingPathVariants(true) |
|
64 |
buildPathVariants(pathDto).then((pathVariants) => { |
|
65 |
setActivePaths(new Set(pathVariants.map((_, idx) => idx))) |
|
66 |
setPathVariants(pathVariants) |
|
67 |
setBuildingPathVariants(false) |
|
68 |
}) |
|
69 |
}, [pathDto]) |
|
70 | 38 |
|
71 | 39 |
return ( |
72 | 40 |
<Fragment> |
... | ... | |
92 | 60 |
)} |
93 | 61 |
</Typography> |
94 | 62 |
</Stack> |
63 |
<Stack justifyItems="flex-end" alignSelf="flex-end" alignItems="flex-end"> |
|
64 |
<Button size="small" variant="outlined" startIcon={<EditIcon />}>Edit</Button> |
|
65 |
</Stack> |
|
95 | 66 |
</CardContent> |
96 | 67 |
</Card> |
97 | 68 |
</Fragment> |
... | ... | |
123 | 94 |
> |
124 | 95 |
{pathDto ? 'Update path' : 'Show Path'} |
125 | 96 |
</Typography> |
126 |
<PlaintextUpload setPaths={setPathDto} />
|
|
97 |
<PlaintextUpload /> |
|
127 | 98 |
<FileUpload /> |
128 | 99 |
</Stack> |
129 | 100 |
</Grid> |
... | ... | |
153 | 124 |
key={idx} |
154 | 125 |
pathVariant={pathVariant} |
155 | 126 |
idx={idx} |
156 |
setPrimary={setActivePathIdx} |
|
157 |
primaryIdx={primaryPathIdx} |
|
158 |
active={activePaths.has(idx)} |
|
159 | 127 |
/> |
160 | 128 |
))} |
161 | 129 |
</MapContainer> |
Také k dispozici: Unified diff
refactor to use Redux instead of localstate
re #9629