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/FileUpload.tsx
5 5
    Link,
6 6
    Stack,
7 7
    Typography,
8
} from '@mui/material'
9
import { useFormik } from 'formik'
10
import { Fragment, useState } from 'react'
11
import * as yup from 'yup'
12
import axiosInstance from '../../api/api'
13
import ButtonOpenableDialog from '../Reusables/ButtonOpenableDialog'
14
import AttachmentIcon from '@mui/icons-material/Attachment'
15
import DeleteIcon from '@mui/icons-material/Delete'
16
import SendIcon from '@mui/icons-material/Send'
8
} from "@mui/material"
9
import { useFormik } from "formik"
10
import { Fragment, useState } from "react"
11
import * as yup from "yup"
12
import axiosInstance from "../../api/api"
13
import ButtonOpenableDialog from "../Reusables/ButtonOpenableDialog"
14
import AttachmentIcon from "@mui/icons-material/Attachment"
15
import DeleteIcon from "@mui/icons-material/Delete"
16
import SendIcon from "@mui/icons-material/Send"
17
import { useDispatch } from "react-redux"
18
import { sendTextForProcessing } from "./trackingToolThunks"
19

  
20
interface UploadValues {
21
    file?: File
22
}
23

  
24
const initialValues: UploadValues = {}
17 25

  
18 26
const FileUpload = () => {
27
    const dispatch = useDispatch()
28

  
19 29
    const [filename, setFilename] = useState<string | undefined>(undefined)
30
    const [fileProcessing, setFileProcessing] = useState(false)
20 31

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

  
25 36
    const formik = useFormik({
26
        initialValues: {
27
            file: undefined,
28
        },
37
        initialValues,
29 38
        validationSchema,
30 39
        onSubmit: async (values) => {
31
            
40
            setFileProcessing(true)
41
            const reader = new FileReader()
42
            reader.readAsText(values.file as File)
43
            reader.onload = async () => {
44
                dispatch(sendTextForProcessing(reader.result as string))
45
                setFileProcessing(false)
46
            }
32 47
        },
33 48
    })
34 49

  
......
37 52
        const file = event.currentTarget.files[0]
38 53
        if (file) {
39 54
            setFilename(file.name)
40
            formik.setFieldValue('file', file)
55
            formik.setFieldValue("file", file)
41 56
        }
42 57
    }
43 58

  
44 59
    const onClose = () => {
60
        if (fileProcessing) {
61
            return
62
        }
45 63
        setFilename(undefined)
46 64
        formik.resetForm()
47 65
    }
48 66

  
49 67
    const onClearSelectedFile = () => {
50 68
        setFilename(undefined)
51
        formik.setFieldValue('file', undefined)
69
        formik.setFieldValue("file", undefined)
52 70
    }
53 71

  
54 72
    return (
......
91 109
                        <Fragment>
92 110
                            <Stack direction="row" spacing={1}>
93 111
                                <Typography
94
                                    sx={{
95
                                        // textOverflow: 'ellipsis',
96
                                        // overflow: 'hidden',
97
                                    }}
112
                                    sx={
113
                                        {
114
                                            // textOverflow: 'ellipsis',
115
                                            // overflow: 'hidden',
116
                                        }
117
                                    }
98 118
                                    variant="body1"
99 119
                                >
100
                                    Selected File:{' '}
120
                                    Selected File:{" "}
101 121
                                </Typography>
102 122
                                <Typography
103 123
                                    sx={{
104
                                        textOverflow: 'ellipsis',
105
                                        overflow: 'hidden',
124
                                        textOverflow: "ellipsis",
125
                                        overflow: "hidden",
106 126
                                    }}
107 127
                                    // color="text.secondary"
108 128
                                    component={Link}
......
117 137
                                justifyContent="flex-end"
118 138
                                alignItems="center"
119 139
                                spacing={2}
120
                                sx={{mt: 2}}
140
                                sx={{ mt: 2 }}
121 141
                            >
122 142
                                <Button
123 143
                                    // sx={{ mb: 2, mt: 1 }}
......
128 148
                                >
129 149
                                    Remove Selection
130 150
                                </Button>
131
                                <Button size="small" type="submit" variant="contained" startIcon={<SendIcon />}>
132
                                Submit
133
                            </Button>
151
                                <Button
152
                                    size="small"
153
                                    type="submit"
154
                                    variant="contained"
155
                                    startIcon={<SendIcon />}
156
                                >
157
                                    Submit
158
                                </Button>
134 159
                            </Stack>
135
                            
136
                            
137 160
                        </Fragment>
138 161
                    )}
139 162
                </form>

Také k dispozici: Unified diff