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 formData = new FormData()
|
35
|
// @ts-ignore
|
36
|
formData.append("file", values.file)
|
37
|
|
38
|
const {data, status} = await axiosInstance.post(
|
39
|
'/external-catalog-items',
|
40
|
formData,
|
41
|
{headers: {
|
42
|
'Content-Type': 'multipart/form-data'
|
43
|
}}
|
44
|
)
|
45
|
if (status !== 200) {
|
46
|
console.log("Error in updating catalog")
|
47
|
}
|
48
|
console.log("ex cat post status: " + status)
|
49
|
|
50
|
},
|
51
|
})
|
52
|
|
53
|
// Callback when user selects the file
|
54
|
const onFileSelected = (event: any) => {
|
55
|
const file = event.currentTarget.files[0]
|
56
|
if (file) {
|
57
|
setFilename(file.name)
|
58
|
formik.setFieldValue('file', file)
|
59
|
}
|
60
|
}
|
61
|
|
62
|
const onClose = () => {
|
63
|
if (fileProcessing) {
|
64
|
return
|
65
|
}
|
66
|
setFilename(undefined)
|
67
|
formik.resetForm()
|
68
|
}
|
69
|
|
70
|
const onClearSelectedFile = () => {
|
71
|
setFilename(undefined)
|
72
|
formik.setFieldValue('file', undefined)
|
73
|
}
|
74
|
|
75
|
|
76
|
const isAdmin = useSelector(
|
77
|
(state: RootState) => state.user.roles.includes("ROLE_ADMIN")
|
78
|
)
|
79
|
const handleClick = () => {
|
80
|
|
81
|
}
|
82
|
|
83
|
return (
|
84
|
<Fragment>
|
85
|
<Typography variant="h3" sx={{ mb: 2 }} fontWeight="bold">
|
86
|
External Sources
|
87
|
</Typography>
|
88
|
{isAdmin ? (
|
89
|
<Paper style={{ minHeight: '80vh' }} variant="outlined">
|
90
|
<Container sx={{ mt: 4 }}>
|
91
|
<Typography sx={{ mb: 2 }}>
|
92
|
Please upload CSV file from CIGS available at https://zenodo.org/record/5642899#.YoeJLXXP1hE
|
93
|
(please check version, there might be newer).
|
94
|
</Typography>
|
95
|
<Typography sx={{ mb: 3 }}>
|
96
|
Files from Pleiades (available at https://pleiades.stoa.org/
|
97
|
, downloads at http://atlantides.org/downloads/pleiades/dumps/
|
98
|
), ANE (available at https://www.lingfil.uu.se/research/assyriology/earth/
|
99
|
, downloads at https://zenodo.org/record/6384045
|
100
|
) and GeoNames (available at https://www.geonames.org/
|
101
|
) will be added automatically.
|
102
|
</Typography>
|
103
|
<SingleFileSelectionForm
|
104
|
onFileSelected={onFileSelected}
|
105
|
onClearSelectedFile={onClearSelectedFile}
|
106
|
filename={filename}
|
107
|
formik={formik}
|
108
|
/>
|
109
|
</Container>
|
110
|
</Paper>
|
111
|
) : <NotAuthorized />}
|
112
|
</Fragment>
|
113
|
)
|
114
|
}
|
115
|
|
116
|
export default ExternalSources
|