Projekt

Obecné

Profil

Stáhnout (6.21 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {
2
    Button,
3
    Card,
4
    CardContent,
5
    Grid,
6
    Stack,
7
    Typography,
8
} from "@mui/material"
9
import { Fragment, useEffect, useRef, useState } from "react"
10
import { MapContainer, TileLayer, useMap } from "react-leaflet"
11
import mapConfig from "../../config/mapConfig"
12
import TextPath from "react-leaflet-textpath"
13
import PlaintextUpload from "./PlaintextUpload"
14
import FileUpload from "./FileUpload"
15
import L, { Map } from "leaflet"
16
import DeleteIcon from "@mui/icons-material/Delete"
17
import { PathDto } from "../../swagger/data-contracts"
18
import { formatHtmlStringToReactDom } from "../../utils/formatting/HtmlUtils"
19
import MapPath from "./MapPath"
20
import EditIcon from "@mui/icons-material/Edit"
21
import { useDispatch, useSelector } from "react-redux"
22
import { RootState } from "../redux/store"
23
import { clear, consumeErr as consumeError } from "./trackingToolSlice"
24
import { showNotification } from "../Notification/notificationSlice"
25
import ClearIcon from "@mui/icons-material/Clear"
26

    
27
// Page with tracking tool
28
const TrackingTool = () => {
29
    const dispatch = useDispatch()
30

    
31
    // Path response from the API
32
    const pathDto = useSelector(
33
        (state: RootState) => state.trackingTool.pathDto
34
    )
35
    const pathVariants = useSelector(
36
        (state: RootState) => state.trackingTool.pathVariants
37
    )
38
    const mapCenter = useSelector(
39
        (state: RootState) => state.trackingTool.mapCenter
40
    )
41

    
42
    // Consume any error
43
    const err = useSelector((state: RootState) => state.trackingTool.lastError)
44
    useEffect(() => {
45
        if (!err) {
46
            return
47
        }
48
        const error = `${err}`
49
        dispatch(consumeError())
50
        dispatch(
51
            showNotification({
52
                message: error,
53
                severity: "error",
54
            })
55
        )
56
    }, [err, dispatch])
57

    
58
    const mapRef = useRef<Map | undefined>(undefined)
59
    useEffect(() => {
60
        if (!mapRef || !mapRef.current) {
61
            console.log("No map ref")
62
            return
63
        }
64

    
65
        const map = mapRef.current
66
        map.setView(mapCenter, mapConfig.defaultZoom, {
67
            animate: true,
68
        })
69
    }, [mapCenter, mapRef])
70

    
71
    return (
72
        <Fragment>
73
            <Typography variant="h3" sx={{ mb: 2 }} fontWeight="bold">
74
                Tracking Tool
75
            </Typography>
76

    
77
            <Grid container>
78
                <Grid item xs={12}>
79
                    {pathDto && pathDto?.foundCatalogItems?.length === 0 && (
80
                        <Typography
81
                            variant="body1"
82
                            sx={{ mb: 2 }}
83
                            fontWeight="500"
84
                            align="center"
85
                            color="error.main"
86
                        >
87
                            Looks like no path / catalog items match this query.
88
                        </Typography>
89
                    )}
90
                    {!pathDto && (
91
                        <Stack
92
                            direction="row"
93
                            alignItems="flex-start"
94
                            spacing={2}
95
                            sx={{ mt: 1 }}
96
                        >
97
                            <Typography
98
                                variant="h5"
99
                                sx={{ mb: 2 }}
100
                                fontWeight="500"
101
                            >
102
                                Upload:
103
                            </Typography>
104
                            <PlaintextUpload />
105
                            <FileUpload />
106
                        </Stack>
107
                    )}
108

    
109
                    {pathDto && (
110
                        <Stack alignItems="flex-end">
111
                            <Button
112
                                startIcon={<ClearIcon />}
113
                                sx={{ mb: 1 }}
114
                                variant="contained"
115
                                onClick={() => dispatch(clear())}
116
                            >
117
                                Clear Map
118
                            </Button>
119
                        </Stack>
120
                    )}
121
                </Grid>
122

    
123
                <Grid
124
                    item
125
                    xs={12}
126
                    md={12}
127
                    style={{
128
                        minHeight: "60vh",
129
                        maxHeight: "100vh",
130
                        width: "100%",
131
                    }}
132
                >
133
                    <MapContainer
134
                        center={[mapCenter[0], mapCenter[1]]}
135
                        zoom={mapConfig.defaultZoom}
136
                        style={{ height: "100%", minHeight: "100%" }}
137
                        whenCreated={(map) => { mapRef.current = map }}
138
                    >
139
                        <TileLayer
140
                            attribution={mapConfig.attribution}
141
                            url={mapConfig.url}
142
                        />
143
                        {pathVariants?.map((pathVariant, idx) => (
144
                            <MapPath idx={idx} />
145
                        ))}
146
                    </MapContainer>
147
                    {pathDto && (
148
                        <Fragment>
149
                            <Card variant="outlined" sx={{ mt: 2 }}>
150
                                <CardContent>
151
                                    <Stack direction="column">
152
                                        <Typography
153
                                            variant="h5"
154
                                            sx={{ mb: 1 }}
155
                                            fontWeight="600"
156
                                        >
157
                                            Processed Text
158
                                        </Typography>
159
                                        <Typography variant="body2">
160
                                            {formatHtmlStringToReactDom(
161
                                                pathDto.text ?? ""
162
                                            )}
163
                                        </Typography>
164
                                    </Stack>
165
                                </CardContent>
166
                            </Card>
167
                        </Fragment>
168
                    )}
169
                </Grid>
170
            </Grid>
171
        </Fragment>
172
    )
173
}
174

    
175
export default TrackingTool
(6-6/9)