-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathJsonSelection.tsx
60 lines (53 loc) · 1.93 KB
/
JsonSelection.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
import { type ChangeEvent, type FC, useState } from 'react';
import { Form, ProgressBar } from 'react-bootstrap';
type JsonSelectionProps = {
fileContents?: string;
setFileContents: (file?: string) => void;
};
/**
* Form component to select a JSON file for upload
*
* @param props
* @param props.fileContents string containing the json file
* @param props.setFileContents callback to update the string containing the json
*/
const JsonSelection: FC<JsonSelectionProps> = ({ fileContents, setFileContents }) => {
const [progress, setProgress] = useState(100.0);
function loadFile(file?: File) {
if (file === undefined) {
setFileContents(undefined);
return;
}
const reader = new FileReader();
reader.addEventListener('load', (e) => {
if (e.target?.result) {
// readAsText guarantees string
setFileContents(e.target.result as string);
} else {
setFileContents(undefined);
}
setProgress(100.0);
});
reader.addEventListener('progress', (e) => {
setProgress((e.loaded / e.total) * 100);
});
reader.readAsText(file);
}
return (
<div>
<Form.Group>
<Form.Label>Please select result JSON file</Form.Label>
<Form.Control
type="file"
onChange={(e: ChangeEvent<HTMLInputElement>) =>
loadFile(e.target.files ? e.target.files[0] : undefined)
}
/>
</Form.Group>
{progress !== 100.0 && <ProgressBar now={progress} label={`${progress}%`} />}
{/* TODO: display result with formatting etc? */}
{/*props.fileContents !== undefined ? props.fileContents : <div className="text-muted">No file loaded.</div>*/}
</div>
);
};
export default JsonSelection;