Projekt

Obecné

Profil

Stáhnout (8.89 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { Button, Grid, Stack, Typography } from '@mui/material'
2
import { Fragment, useEffect, useState } from 'react'
3
import PlaintextUpload from './Upload/PlaintextUpload'
4
import FileUpload from './Upload/FileUpload'
5
import { useDispatch, useSelector } from 'react-redux'
6
import { RootState } from '../redux/store'
7
import {
8
    clear as clearTrackingTool,
9
    consumeErr as consumeError,
10
    toggleToggleables,
11
} from './trackingToolSlice'
12
import { showNotification } from '../Notification/notificationSlice'
13
import ClearIcon from '@mui/icons-material/Clear'
14
import ExternalPathExportButton from './ExternalPath/ExternalPathExportButton'
15
import ProcessedTextDisplay from './Controls/ProcessedTextDisplay'
16
import MapPointToggleables from './Controls/MapPointToggleables'
17
import ExternalPathImportDialog from './ExternalPath/ExternalPathImportDialog'
18
import Map from './Map/Map'
19
import ReorderableMapPointList from './Controls/ReorderableMapPointList'
20
import FilterListIcon from '@mui/icons-material/FilterList'
21
import FilterListOffIcon from '@mui/icons-material/FilterListOff'
22
import SaveDisplayedPath from './Controls/SaveDisplayedPath'
23
import { useNavigate } from 'react-router-dom'
24
import ConfirmationDialog from '../Reusables/ConfirmationDialog'
25
import ManageFilePaths from './Controls/ManageFilePaths'
26

    
27
// Page with tracking tool
28
const TrackingTool = () => {
29
    const pathDto = useSelector(
30
        // Path response from the API
31
        (state: RootState) => state.trackingTool.pathDto
32
    )
33

    
34
    // Use path as flag for undefined or present
35
    const path = useSelector(
36
        (state: RootState) => state.trackingTool.displayedPath
37
    )
38

    
39
    const toggleablesOpen = useSelector(
40
        (state: RootState) => state.trackingTool.toggleablesOpen
41
    )
42

    
43
    const userLoggedIn = useSelector(
44
        (state: RootState) => state.user.isLoggedIn
45
    )
46
    const trackingToolPermission = useSelector((state: RootState) => state.user.roles.includes('READ'))
47
    const navigate = useNavigate()
48
    useEffect(() => {
49
        if (!userLoggedIn || !trackingToolPermission) {
50
            navigate('/')
51
        }
52
    }, [userLoggedIn, navigate, trackingToolPermission])
53

    
54
    // Consume any error
55
    const dispatch = useDispatch()
56
    const err = useSelector((state: RootState) => state.trackingTool.lastError)
57
    useEffect(() => {
58
        if (!err) {
59
            return
60
        }
61
        const error = `${err}`
62
        dispatch(consumeError())
63
        dispatch(
64
            showNotification({
65
                message: error,
66
                severity: 'error',
67
            })
68
        )
69
    }, [err, dispatch])
70

    
71
    const [showClearPathDialog, setShowClearPathDialog] = useState(false)
72

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

    
79
            <Grid container>
80
                <Grid item xs={12} alignItems="center">
81
                    {pathDto && pathDto?.foundCatalogItems?.length === 0 && (
82
                        <Typography
83
                            variant="body1"
84
                            sx={{ mb: 2 }}
85
                            fontWeight="500"
86
                            align="center"
87
                            color="error.main"
88
                        >
89
                            Looks like no path / catalog items match the
90
                            uploaded text.
91
                        </Typography>
92
                    )}
93

    
94
                    {!pathDto && (
95
                        <Fragment>
96
                            <Stack
97
                                direction="row"
98
                                alignItems="center"
99
                                spacing={1}
100
                                sx={{ my: 1 }}
101
                            >
102
                                <Typography variant="h6" fontWeight="500">
103
                                    Upload:
104
                                </Typography>
105
                                <Fragment>
106
                                    <PlaintextUpload />
107
                                    <FileUpload />
108
                                </Fragment>
109
                            </Stack>
110
                        </Fragment>
111
                    )}
112
                    {path && (
113
                        <Grid
114
                            container
115
                            direction="row"
116
                            sx={{ my: 1 }}
117
                            spacing={1}
118
                            // justifyContent="space-between"
119
                        >
120
                            <Grid
121
                                container
122
                                item
123
                                xs={12}
124
                                md={6}
125
                                direction="row"
126
                                spacing={1}
127
                            >
128
                                <Grid item>
129
                                    <Button
130
                                        startIcon={
131
                                            toggleablesOpen ? (
132
                                                <FilterListOffIcon />
133
                                            ) : (
134
                                                <FilterListIcon />
135
                                            )
136
                                        }
137
                                        variant="contained"
138
                                        color="primary"
139
                                        onClick={() => {
140
                                            dispatch(toggleToggleables())
141
                                        }}
142
                                    >
143
                                        Filter
144
                                    </Button>
145
                                </Grid>
146
                                <Grid item>
147
                                    <Button
148
                                        startIcon={<ClearIcon />}
149
                                        variant="contained"
150
                                        onClick={() => {
151
                                            setShowClearPathDialog(true)
152
                                        }}
153
                                    >
154
                                        Clear Map
155
                                    </Button>
156
                                    <ConfirmationDialog
157
                                        open={showClearPathDialog}
158
                                        setOpen={setShowClearPathDialog}
159
                                        onConfirmCallback={() => {
160
                                            dispatch(clearTrackingTool())
161
                                        }}
162
                                        title="Clear Map"
163
                                        secondaryText="Are you sure you want to clear the map? All data will be discarded."
164
                                        maxWidth="xs"
165
                                    />
166
                                </Grid>
167
                            </Grid>
168
                            <Grid
169
                                item
170
                                container
171
                                md={6}
172
                                xs={12}
173
                                direction="row"
174
                                spacing={1}
175
                                justifyContent="flex-end"
176
                            >
177
                                <Grid item>
178
                                    <ManageFilePaths />
179
                                </Grid>
180
                                <Grid item>
181
                                    <ExternalPathImportDialog />
182
                                </Grid>
183
                                <Grid item>
184
                                    <ExternalPathExportButton />
185
                                </Grid>
186
                                <Grid item>
187
                                    <SaveDisplayedPath />
188
                                </Grid>
189
                            </Grid>
190
                        </Grid>
191
                    )}
192

    
193
                    {pathDto && (
194
                        <Fragment>
195
                            <MapPointToggleables />
196
                        </Fragment>
197
                    )}
198
                </Grid>
199

    
200
                <Grid
201
                    item
202
                    xs={12}
203
                    md={12}
204
                    style={{
205
                        minHeight: '60vh',
206
                        maxHeight: '100vh',
207
                        width: '100%',
208
                    }}
209
                >
210
                    <Map />
211
                </Grid>
212
                <Grid container sx={{ mt: 1, mb: 20 }} spacing={1}>
213
                    <Grid item xs={12} md={6} lg={8}>
214
                        <ProcessedTextDisplay />
215
                    </Grid>
216
                    <Grid item xs={12} md={6} lg={4}>
217
                        <ReorderableMapPointList />
218
                    </Grid>
219
                </Grid>
220
            </Grid>
221
        </Fragment>
222
    )
223
}
224

    
225
export default TrackingTool
(1-1/4)