Projekt

Obecné

Profil

Stáhnout (5.97 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, 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 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
    // Path response from the API
30
    const pathDto = useSelector(
31
        (state: RootState) => state.trackingTool.pathDto
32
    )
33
    const pathVariants = useSelector(
34
        (state: RootState) => state.trackingTool.pathVariants
35
    )
36
    const mapCenter = useSelector(
37
        (state: RootState) => state.trackingTool.mapCenter
38
    )
39

    
40
    // const map = useMap()
41

    
42
    // // Set the map center
43
    // useEffect(() => {
44
    //     map.flyTo(mapCenter, mapConfig.defaultZoom)
45
    // }, [map, mapCenter])
46

    
47
    // Consume any error
48
    const err = useSelector((state: RootState) => state.trackingTool.lastError)
49
    const dispatch = useDispatch()
50

    
51
    useEffect(() => {
52
        if (!err) {
53
            return
54
        }
55
        const error = `${err}`
56
        dispatch(consumeError())
57
        dispatch(
58
            showNotification({
59
                message: error,
60
                severity: 'error',
61
            })
62
        )
63
    }, [err, dispatch])
64

    
65
    return (
66
        <Fragment>
67
            <Typography variant="h3" sx={{ mb: 2 }} fontWeight="bold">
68
                Tracking Tool
69
            </Typography>
70

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

    
103
                    {pathDto && (
104
                        <Stack alignItems="flex-end">
105
                            <Button
106
                                startIcon={<ClearIcon />}
107
                                sx={{ mb: 1 }}
108
                                variant="contained"
109
                                onClick={() => dispatch(clear())}
110
                            >
111
                                Clear Map
112
                            </Button>
113
                        </Stack>
114
                    )}
115
                </Grid>
116

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

    
168
export default TrackingTool
(5-5/8)