-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAddFileForm.js
More file actions
45 lines (39 loc) · 1.23 KB
/
AddFileForm.js
File metadata and controls
45 lines (39 loc) · 1.23 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
import React, { useState } from "react";
import { addFile } from "../utils";
import Notifications from "./Notifications";
function AddFileForm({ contract, account }) {
const [selectedFile, setSelectedFile] = useState(null);
const [notification, setNotification] = useState(null);
const handleSubmit = async (event) => {
event.preventDefault();
if (selectedFile) {
try {
await addFile(selectedFile, contract, account);
setSelectedFile(null);
setNotification({ type: "success", message: "File added successfully" });
} catch (error) {
console.error(error);
setNotification({ type: "error", message: "Error adding file" });
}
} else {
setNotification({ type: "error", message: "Please select a file" });
}
};
const handleFileChange = (event) => {
setSelectedFile(event.target.files[0]);
};
return (
<div>
<h2>Add a new file</h2>
<form onSubmit={handleSubmit}>
<label>
Select File:
<input type="file" onChange={handleFileChange} />
</label>
<button type="submit">Add file</button>
</form>
{notification && <Notifications {...notification} />}
</div>
);
}
export default AddFileForm;