Projekt

Obecné

Profil

Stáhnout (3.25 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

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

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

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

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

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

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

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

    
99
export default GeoJsonImportDialog
(3-3/5)