Projekt

Obecné

Profil

Stáhnout (6.03 KB) Statistiky
| Větev: | Tag: | Revize:
1 0d90d67b Vaclav Honzik
import {
2
    Button,
3
    DialogContent,
4
    DialogTitle,
5
    Link,
6
    Stack,
7
    Typography,
8 11fca75a Vaclav Honzik
} 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 = {}
25 0d90d67b Vaclav Honzik
26
const FileUpload = () => {
27 11fca75a Vaclav Honzik
    const dispatch = useDispatch()
28
29 0d90d67b Vaclav Honzik
    const [filename, setFilename] = useState<string | undefined>(undefined)
30 11fca75a Vaclav Honzik
    const [fileProcessing, setFileProcessing] = useState(false)
31 0d90d67b Vaclav Honzik
32
    const validationSchema = yup.object().shape({
33 11fca75a Vaclav Honzik
        file: yup.mixed().required("File is required"),
34 0d90d67b Vaclav Honzik
    })
35
36
    const formik = useFormik({
37 11fca75a Vaclav Honzik
        initialValues,
38 0d90d67b Vaclav Honzik
        validationSchema,
39
        onSubmit: async (values) => {
40 11fca75a Vaclav Honzik
            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
            }
47 0d90d67b Vaclav Honzik
        },
48
    })
49
50
    // Callback when user selects the file
51
    const onFileSelected = (event: any) => {
52
        const file = event.currentTarget.files[0]
53
        if (file) {
54
            setFilename(file.name)
55 11fca75a Vaclav Honzik
            formik.setFieldValue("file", file)
56 0d90d67b Vaclav Honzik
        }
57
    }
58
59
    const onClose = () => {
60 11fca75a Vaclav Honzik
        if (fileProcessing) {
61
            return
62
        }
63 0d90d67b Vaclav Honzik
        setFilename(undefined)
64
        formik.resetForm()
65
    }
66
67
    const onClearSelectedFile = () => {
68
        setFilename(undefined)
69 11fca75a Vaclav Honzik
        formik.setFieldValue("file", undefined)
70 0d90d67b Vaclav Honzik
    }
71
72
    return (
73
        <ButtonOpenableDialog
74 2d82649d Vaclav Honzik
            buttonText="File"
75 0d90d67b Vaclav Honzik
            buttonColor="primary"
76
            buttonVariant="contained"
77
            onCloseCallback={onClose}
78
            maxWidth="xs"
79
        >
80
            <DialogTitle>Upload New File</DialogTitle>
81
            <DialogContent>
82
                <form onSubmit={formik.handleSubmit}>
83
                    {!filename ? (
84
                        <Fragment>
85
                            <Stack
86
                                direction="row"
87
                                justifyContent="flex-end"
88
                                alignItems="center"
89
                            >
90
                                <Button
91
                                    variant="contained"
92
                                    color="primary"
93
                                    component="label"
94
                                    // size="small"
95
                                    startIcon={<AttachmentIcon />}
96
                                >
97
                                    Select File
98
                                    <input
99
                                        id="file"
100
                                        name="file"
101
                                        type="file"
102
                                        hidden
103
                                        onChange={onFileSelected}
104
                                    />
105
                                </Button>
106
                            </Stack>
107
                        </Fragment>
108
                    ) : (
109
                        <Fragment>
110
                            <Stack direction="row" spacing={1}>
111
                                <Typography
112 11fca75a Vaclav Honzik
                                    sx={
113
                                        {
114
                                            // textOverflow: 'ellipsis',
115
                                            // overflow: 'hidden',
116
                                        }
117
                                    }
118 0d90d67b Vaclav Honzik
                                    variant="body1"
119
                                >
120 11fca75a Vaclav Honzik
                                    Selected File:{" "}
121 0d90d67b Vaclav Honzik
                                </Typography>
122
                                <Typography
123
                                    sx={{
124 11fca75a Vaclav Honzik
                                        textOverflow: "ellipsis",
125
                                        overflow: "hidden",
126 0d90d67b Vaclav Honzik
                                    }}
127
                                    // color="text.secondary"
128
                                    component={Link}
129
                                    // download={(formik.values?.file as File).}
130
                                    // align="right"
131
                                >
132
                                    {filename}
133
                                </Typography>
134
                            </Stack>
135
                            <Stack
136
                                direction="row"
137
                                justifyContent="flex-end"
138
                                alignItems="center"
139
                                spacing={2}
140 11fca75a Vaclav Honzik
                                sx={{ mt: 2 }}
141 0d90d67b Vaclav Honzik
                            >
142
                                <Button
143
                                    // sx={{ mb: 2, mt: 1 }}
144
                                    variant="contained"
145
                                    size="small"
146
                                    endIcon={<DeleteIcon />}
147
                                    onClick={onClearSelectedFile}
148
                                >
149
                                    Remove Selection
150
                                </Button>
151 11fca75a Vaclav Honzik
                                <Button
152
                                    size="small"
153
                                    type="submit"
154
                                    variant="contained"
155
                                    startIcon={<SendIcon />}
156
                                >
157
                                    Submit
158
                                </Button>
159 0d90d67b Vaclav Honzik
                            </Stack>
160
                        </Fragment>
161
                    )}
162
                </form>
163
            </DialogContent>
164
        </ButtonOpenableDialog>
165
    )
166
}
167
168
export default FileUpload