Projekt

Obecné

Profil

Stáhnout (3.39 KB) Statistiky
| Větev: | Tag: | Revize:
1
import { Button, Dialog, DialogContent, DialogTitle } from '@mui/material'
2
import { useFormik } from 'formik'
3
import { Fragment, useState } from 'react'
4
import { useDispatch, useSelector } from 'react-redux'
5
import { RootState } from '../../redux/store'
6
import ButtonOpenableDialog from '../../Reusables/ButtonOpenableDialog'
7
import { PathVariant } from '../pathUtils'
8
import * as yup from 'yup'
9
import { showNotification } from '../../Notification/notificationSlice'
10
import SingleFileSelectionForm from '../../Reusables/SingleFileSelectionForm'
11
import { mergeWithCurrentPath } from '../trackingToolSlice'
12
import { parseGeoJsonToPathVariant } from './GeoJsonIo'
13

    
14
const GeoJsonImportDialog = () => {
15
    const dispatch = useDispatch()
16

    
17
    const [filename, setFilename] = useState<string | undefined>(undefined)
18
    const [fileProcessing, setFileProcessing] = useState(false)
19
    const [open, setOpen] = useState(false)
20

    
21
    const validationSchema = yup.object().shape({
22
        file: yup.mixed().required('File is required'),
23
    })
24

    
25
    const initialValues: { file?: File } = {
26
        file: undefined,
27
    }
28

    
29
    // Callback when user selects the file
30
    const onFileSelected = (event: any) => {
31
        const file = event.currentTarget.files[0]
32
        if (file) {
33
            setFilename(file.name)
34
            formik.setFieldValue('file', file)
35
        }
36
    }
37

    
38
    const onClose = () => {
39
        if (fileProcessing) {
40
            return
41
        }
42
        setFilename(undefined)
43
        formik.resetForm()
44
        setOpen(false)
45
    }
46

    
47
    const onClearSelectedFile = () => {
48
        setFilename(undefined)
49
        formik.setFieldValue('file', undefined)
50
    }
51

    
52
    const formik = useFormik({
53
        initialValues,
54
        validationSchema,
55
        onSubmit: async (values) => {
56
            setFileProcessing(true)
57
            const reader = new FileReader()
58
            reader.readAsText(values.file as File)
59
            reader.onload = async () => {
60
                try {
61
                    const pathVariant = parseGeoJsonToPathVariant(reader.result as string)
62
                    console.log(pathVariant)
63
                    // Merge current path variant with the new one
64
                    dispatch(mergeWithCurrentPath(pathVariant))
65
                    onClose()
66
                } catch (e: any) {
67
                    dispatch(
68
                        showNotification({
69
                            message: e.message,
70
                            // message: 'Error importing GeoJson, the file has invalid format',
71
                            severity: 'error',
72
                            autohideSecs: 5,
73
                        })
74
                    )
75
                }
76
                setFileProcessing(false)
77
            }
78
        },
79
    })
80

    
81
    return (
82
        <ButtonOpenableDialog
83
            buttonText="Import"
84
            buttonColor="primary"
85
            buttonVariant="contained"
86
            onCloseCallback={onClose}
87
            maxWidth="xs"
88
            open={open}
89
            setOpen={setOpen}
90
        >
91
            <DialogTitle>Import Path</DialogTitle>
92
            <DialogContent>
93
                <SingleFileSelectionForm
94
                    onFileSelected={onFileSelected}
95
                    onClearSelectedFile={onClearSelectedFile}
96
                    filename={filename}
97
                    formik={formik}
98
                />
99
            </DialogContent>
100
        </ButtonOpenableDialog>
101
    )
102
}
103

    
104
export default GeoJsonImportDialog
(3-3/5)