-
Notifications
You must be signed in to change notification settings - Fork 0
/
DropZone.tsx
67 lines (63 loc) · 1.9 KB
/
DropZone.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { useState } from "react";
import { useDropzone } from "react-dropzone";
import useUploadFile from "./useUploadFile";
import { FileIcon } from "../Icons";
import FileSuccess from "./FileSuccess";
const DropZone: React.FC = () => {
const [files, setFiles] = useState<Array<File>>([]);
const { getRootProps, getInputProps, isDragActive } = useDropzone({
onDrop: (files: File[]) => {
setFiles(files);
},
maxFiles: 1,
});
const [progress, isUploaded] = useUploadFile(files[files.length - 1]);
let DropZoneCartTitle: React.ReactNode;
let DropZoneProgress: React.ReactNode;
if (!isUploaded && progress) {
DropZoneCartTitle = <p className="p-2">Downloading</p>;
DropZoneProgress = <h5 className="drop-zone__progress">{progress}%</h5>;
} else if (isUploaded) {
DropZoneCartTitle = <p className="p-2">{files[files.length - 1].name} </p>;
DropZoneProgress = <FileSuccess />;
} else {
DropZoneCartTitle = (
<p className="p-2">
<span>Select a file </span>or drag in form
</p>
);
DropZoneProgress = <div></div>;
}
console.log(progress);
return (
<div
className={`drop-zone__container ${isDragActive && "drop-zone--on-drag"}`}
{...getRootProps()}
>
<input type="text" style={{ width: 0, height: 0 }} {...getInputProps()} />
{isDragActive ? (
<FileIcon />
) : (
<div className="drop-zone__text">
<div>
{DropZoneCartTitle}
<p className="p-3 drop-zone__description">
PNG, jpg, gif files up to 10 MB in size are available for download
</p>
</div>
{DropZoneProgress}
</div>
)}
{!isUploaded && (
<div
style={{
backgroundColor: "#6E41E2",
width: `${progress}%`,
height: "4px",
}}
/>
)}
</div>
);
};
export default DropZone;