Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 11fca75a

Přidáno uživatelem Václav Honzík před asi 2 roky(ů)

autozoom + file upload

re #9741

Zobrazit rozdíly:

frontend/src/features/TrackingTool/TrackingTool.tsx
1 1
import {
2
  Button,
3
  Card,
4
  CardContent,
5
  Grid,
6
  Stack,
7
  Typography,
2
    Button,
3
    Card,
4
    CardContent,
5
    Grid,
6
    Stack,
7
    Typography,
8 8
} from "@mui/material"
9
import { Fragment, useEffect, useState } from "react"
9
import { Fragment, useEffect, useRef, useState } from "react"
10 10
import { MapContainer, TileLayer, useMap } from "react-leaflet"
11 11
import mapConfig from "../../config/mapConfig"
12 12
import TextPath from "react-leaflet-textpath"
13 13
import PlaintextUpload from "./PlaintextUpload"
14 14
import FileUpload from "./FileUpload"
15
import L from "leaflet"
15
import L, { Map } from "leaflet"
16 16
import DeleteIcon from "@mui/icons-material/Delete"
17 17
import { PathDto } from "../../swagger/data-contracts"
18 18
import { formatHtmlStringToReactDom } from "../../utils/formatting/HtmlUtils"
......
26 26

  
27 27
// Page with tracking tool
28 28
const TrackingTool = () => {
29
  // Path response from the API
30
  const pathDto = useSelector((state: RootState) => state.trackingTool.pathDto)
31
  const pathVariants = useSelector(
32
    (state: RootState) => state.trackingTool.pathVariants
33
  )
34
  const mapCenter = useSelector(
35
    (state: RootState) => state.trackingTool.mapCenter
36
  )
29
    const dispatch = useDispatch()
37 30

  
38
  // Consume any error
39
  const err = useSelector((state: RootState) => state.trackingTool.lastError)
40
  const dispatch = useDispatch()
41

  
42
  useEffect(() => {
43
    if (!err) {
44
      return
45
    }
46
    const error = `${err}`
47
    dispatch(consumeError())
48
    dispatch(
49
      showNotification({
50
        message: error,
51
        severity: "error",
52
      })
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
53 37
    )
54
  }, [err, dispatch])
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])
55 57

  
56
  return (
57
    <Fragment>
58
      <Typography variant="h3" sx={{ mb: 2 }} fontWeight="bold">
59
        Tracking Tool
60
      </Typography>
58
    const mapRef = useRef<Map | undefined>(undefined)
59
    useEffect(() => {
60
        if (!mapRef || !mapRef.current) {
61
            console.log("No map ref")
62
            return
63
        }
61 64

  
62
      <Grid container>
63
        <Grid item xs={12}>
64
          {pathDto && pathDto?.foundCatalogItems?.length === 0 && (
65
            <Typography
66
              variant="body1"
67
              sx={{ mb: 2 }}
68
              fontWeight="500"
69
              align="center"
70
              color="error.main"
71
            >
72
              Looks like no path / catalog items match this query.
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
73 75
            </Typography>
74
          )}
75
          {!pathDto && (
76
            <Stack
77
              direction="row"
78
              alignItems="flex-start"
79
              spacing={2}
80
              sx={{ mt: 1 }}
81
            >
82
              <Typography variant="h5" sx={{ mb: 2 }} fontWeight="500">
83
                Upload:
84
              </Typography>
85
              <PlaintextUpload />
86
              <FileUpload />
87
            </Stack>
88
          )}
89 76

  
90
          {pathDto && (
91
            <Stack alignItems="flex-end">
92
              <Button
93
                startIcon={<ClearIcon />}
94
                sx={{ mb: 1 }}
95
                variant="contained"
96
                onClick={() => dispatch(clear())}
97
              >
98
                Clear Map
99
              </Button>
100
            </Stack>
101
          )}
102
        </Grid>
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>
103 122

  
104
        <Grid
105
          item
106
          xs={12}
107
          md={12}
108
          style={{
109
            minHeight: "60vh",
110
            maxHeight: "100vh",
111
            width: "100%",
112
          }}
113
        >
114
          <MapContainer
115
            center={[mapCenter[0], mapCenter[1]]}
116
            zoom={mapConfig.defaultZoom}
117
            style={{ height: "100%", minHeight: "100%" }}
118
          >
119
            <TileLayer
120
              attribution={mapConfig.attribution}
121
              url={mapConfig.url}
122
            />
123
            {pathVariants?.map((pathVariant, idx) => (
124
              <MapPath idx={idx} />
125
            ))}
126
          </MapContainer>
127
          {pathDto && (
128
            <Fragment>
129
              <Card variant="outlined" sx={{ mt: 2 }}>
130
                <CardContent>
131
                  <Stack direction="column">
132
                    <Typography variant="h5" sx={{ mb: 1 }} fontWeight="600">
133
                      Processed Text
134
                    </Typography>
135
                    <Typography variant="body2">
136
                      {formatHtmlStringToReactDom(pathDto.text ?? "")}
137
                    </Typography>
138
                  </Stack>
139
                </CardContent>
140
              </Card>
141
            </Fragment>
142
          )}
143
        </Grid>
144
      </Grid>
145
    </Fragment>
146
  )
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
    )
147 173
}
148 174

  
149 175
export default TrackingTool

Také k dispozici: Unified diff