Projekt

Obecné

Profil

Stáhnout (3.02 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {Button, Container, Paper, Typography} from '@mui/material'
2
import {Fragment, useEffect, useState} from 'react'
3
import { useDispatch, useSelector } from 'react-redux'
4
import { RootState } from '../redux/store'
5
import axiosInstance from "../../api/api"
6
import 'react-quill/dist/quill.snow.css'
7
import NotAuthorized from "../NotAuthorized/NotAuthorized"
8
import SingleFileSelectionForm from "../Reusables/SingleFileSelectionForm"
9
import * as yup from "yup"
10
import {useFormik} from "formik"
11

    
12

    
13
interface UploadValues {
14
    file?: File
15
}
16

    
17
const initialValues: UploadValues = {}
18

    
19

    
20
const ExternalSources = () => {
21

    
22
    const [filename, setFilename] = useState<string | undefined>(undefined)
23
    const [fileProcessing, setFileProcessing] = useState(false)
24

    
25
    const validationSchema = yup.object().shape({
26
        file: yup.mixed().required('File is required'),
27
    })
28

    
29
    const formik = useFormik({
30
        initialValues,
31
        validationSchema,
32
        onSubmit: async (values) => {
33
            setFileProcessing(true)
34
            const reader = new FileReader()
35
            reader.readAsText(values.file as File)
36
            reader.onload = async () => {
37
                const {data, status} = await axiosInstance.post(
38
                   '/external-catalog-item',
39
                   { file: reader.result as string }
40
                )
41
                if (status !== 200) {
42
                    console.log("Error in updating catalog")
43
                }
44
            }
45
        },
46
    })
47

    
48
    // Callback when user selects the file
49
    const onFileSelected = (event: any) => {
50
        const file = event.currentTarget.files[0]
51
        if (file) {
52
            setFilename(file.name)
53
            formik.setFieldValue('file', file)
54
        }
55
    }
56

    
57
    const onClose = () => {
58
        if (fileProcessing) {
59
            return
60
        }
61
        setFilename(undefined)
62
        formik.resetForm()
63
    }
64

    
65
    const onClearSelectedFile = () => {
66
        setFilename(undefined)
67
        formik.setFieldValue('file', undefined)
68
    }
69

    
70

    
71
    const isAdmin = useSelector(
72
        (state: RootState) => state.user.roles.includes("ROLE_ADMIN")
73
    )
74
    const handleClick = () => {
75

    
76
    }
77

    
78
    return (
79
        <Fragment>
80
            <Typography variant="h3" sx={{ mb: 2 }} fontWeight="bold">
81
                External Sources
82
            </Typography>
83
            {isAdmin ? (
84
            <Paper style={{ minHeight: '80vh' }} variant="outlined">
85
                <Container sx={{ mt: 4 }}>
86
                    <Typography sx={{ mb: 2 }}>
87
                        Select file with sources from ... source and then click submit to update all external sources.
88
                    </Typography>
89
                    <SingleFileSelectionForm
90
                        onFileSelected={onFileSelected}
91
                        onClearSelectedFile={onClearSelectedFile}
92
                        filename={filename}
93
                        formik={formik}
94
                    />
95
                </Container>
96
            </Paper>
97
            ) : <NotAuthorized />}
98
        </Fragment>
99
    )
100
}
101

    
102
export default ExternalSources
    (1-1/1)