-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathUploaderWrapper.tsx
More file actions
79 lines (76 loc) · 2.5 KB
/
UploaderWrapper.tsx
File metadata and controls
79 lines (76 loc) · 2.5 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
import { useState } from "react";
import { useDropzone } from "react-dropzone";
import { Button } from "@chakra-ui/button";
import { Box, Text } from "@chakra-ui/react";
import useThemeStore from "../../stores/theme";
import { UploaderWrapperProps } from "./types";
import { PiArrowCounterClockwise, PiFile } from "react-icons/pi";
export default function UploaderWrapper({ onSuccess, setDataError, ...props }: UploaderWrapperProps) {
const [loading, setLoading] = useState(false);
const theme = useThemeStore((state) => state.theme);
const { getRootProps, getInputProps, isDragActive, open } = useDropzone({
noClick: true,
noKeyboard: true,
maxFiles: 1,
// maxSize: 1 * Math.pow(1024, 3),
accept: {
"application/vnd.ms-excel": [".xls"],
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": [".xlsx"],
"text/csv": [".csv"],
},
onDropRejected: (fileRejections) => {
setLoading(false);
// const errorMessage = fileRejections.map((fileRejection) => fileRejection.errors[0].message).join(", ");
const errorMessage = fileRejections[0].errors[0].message;
setDataError(errorMessage);
},
onDropAccepted: async ([file]) => {
setLoading(true);
onSuccess(file);
setLoading(false);
},
});
return (
<Box padding="15px" border="1px solid var(--color-border)" borderRadius="var(--border-radius-2)">
<Box
{...getRootProps()}
width="100%"
height="100%"
display="flex"
justifyContent="center"
alignItems="center"
flexDirection="column"
flex={1}
border="2px dashed var(--color-border)"
borderRadius="var(--border-radius-2)">
<input {...getInputProps()} />
{isDragActive ? (
<Text>Drop your file here</Text>
) : loading ? (
<Text>Loading...</Text>
) : (
<>
<Text>Drop your file here</Text>
<Text>or</Text>
<Button
leftIcon={<PiFile />}
onClick={open}
mt="6px"
colorScheme={"secondary"}
variant={theme === "light" ? "outline" : "solid"}
_hover={
theme === "light"
? {
background: "var(--color-border)",
color: "var(--color-text)",
}
: undefined
}>
Browse files
</Button>
</>
)}
</Box>
</Box>
);
}