-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathUploadFile.tsx
More file actions
94 lines (87 loc) · 2.87 KB
/
UploadFile.tsx
File metadata and controls
94 lines (87 loc) · 2.87 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import React, { useState, useRef } from "react";
import { adminApi } from "../../api/apiClient";
import TypingAnimation from "../../components/Header/components/TypingAnimation.tsx";
import Layout from "../Layout/Layout.tsx";
const UploadFile: React.FC = () => {
const fileInputRef = useRef<HTMLInputElement | null>(null);
const [isLoading, setIsLoading] = useState(false);
const handleFileChange = async (
event: React.ChangeEvent<HTMLInputElement>
) => {
if (event.target.files) {
const file = event.target.files[0];
await uploadFile(file);
}
};
const uploadFile = async (file: File) => {
setIsLoading(true);
const formData = new FormData();
formData.append("file", file);
try {
const response = await adminApi.post(
`/api/v1/api/uploadFile`,
formData,
);
console.log("File uploaded successfully", response.data);
} catch (error) {
console.error("Error uploading file", error);
} finally {
setIsLoading(false);
}
};
const handleButtonClick = () => {
if (fileInputRef.current) {
fileInputRef.current.click();
}
};
return (
<Layout>
<div className="flex min-h-screen items-center justify-center">
<div className="w-full max-w-2xl rounded-lg border bg-white p-6 shadow-md">
<div className="flex h-full flex-col items-center justify-center rounded-lg border-2 border-dashed border-gray-300 p-12">
<div className="text-center">
<svg
className="mx-auto h-10 w-10 text-gray-400"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth="2"
d="M3 15a4 4 0 004 4h10a4 4 0 004-4V7a4 4 0 00-4-4H7a4 4 0 00-4 4v8z"
/>
</svg>
<p className="mt-1 text-sm text-gray-500">Import a PDF</p>
</div>
<div className="mt-4">
<input
type="file"
onChange={handleFileChange}
ref={fileInputRef}
className="hidden"
/>
<button
onClick={handleButtonClick}
className="w-full cursor-pointer rounded-md bg-black px-4 py-2 text-white"
disabled={isLoading}
>
{isLoading ? (
<div className="flex items-center justify-center">
<TypingAnimation />
Loading...
</div>
) : (
"Upload File"
)}
</button>
</div>
</div>
</div>
</div>
</Layout>
);
};
export default UploadFile;