-
Notifications
You must be signed in to change notification settings - Fork 569
/
Copy pathFileDropzone.tsx
52 lines (49 loc) · 1.66 KB
/
FileDropzone.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { FunctionComponent, useCallback } from "react";
import { useDropzone } from "react-dropzone-esm";
import { DocumentDownloadIcon } from "@heroicons/react/outline";
export const FileDropzone: FunctionComponent = ({ children }) => {
const onDrop = useCallback((acceptedFiles) => {
acceptedFiles.forEach((file: Blob) => {
const reader = new FileReader();
reader.onabort = () => console.log("file reading was aborted");
reader.onerror = () => console.log("file reading has failed");
reader.onload = () => {
if (typeof reader.result === "string") {
let json = JSON.parse(reader.result);
// dataSourceDispatch(setJSONAction("Needs title", json));
} else {
// dataSourceDispatch(setErrorAction("Can't read file"));
}
};
reader.readAsText(file);
});
}, []);
const { getRootProps, isDragActive } = useDropzone({
onDrop,
multiple: false,
maxFiles: 1,
accept: "application/json, text/*",
noDragEventsBubbling: true,
});
return (
<div
{...getRootProps()}
className={"absolute w-screen h-screen m-0 p-0 left-0 top-0"}
>
<div
className={`${
isDragActive ? "" : "hidden"
} absolute w-screen h-screen bg-black bg-opacity-50 flex justify-center items-center`}
>
<div className={"text-center"}>
{/*<input {...getInputProps()} />*/}
<DocumentDownloadIcon className={"w-72 h-72 text-white"} />
<p className={"text-white text-2xl"}>
Drag 'n' drop some files here, or click to select files
</p>
</div>
</div>
<div>{children}</div>
</div>
);
};