-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathMakeMeJSON.js
More file actions
122 lines (117 loc) · 3.57 KB
/
MakeMeJSON.js
File metadata and controls
122 lines (117 loc) · 3.57 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import React, { useState } from "react";
import DragDrop from "./DragDrop";
import { Paper } from "@mui/material";
import { useHistory } from "react-router-dom";
import InlineFeedback from "./InlineFeedback";
import { ToastContainer, toast } from "react-toastify";
import "react-toastify/dist/ReactToastify.css";
import pdfTableExtractor from "../utilities/pdf-table-extractor";
export default function MakeMeJSON({
fileInfos,
setFileInfos,
setApiData,
existingInstruments,
ReactGA,
}) {
const [loading, setLoading] = useState(false);
const [parseError, setParseError] = useState(false);
const [matchError, setMatchError] = useState(false);
const [grouping, setGrouping] = useState("");
const history = useHistory();
function saveFile(blob, filename) {
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blob, filename);
} else {
const a = document.createElement("a");
document.body.appendChild(a);
const url = window.URL.createObjectURL(blob);
a.href = url;
a.download = filename;
a.click();
setTimeout(() => {
window.URL.revokeObjectURL(url);
document.body.removeChild(a);
}, 0);
}
}
function filesReceiver(fileList) {
const files = Array.from(fileList);
let frp = [];
files.forEach((file) => {
frp.push(
new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => {
if (file.name.split(".").pop().toLowerCase() === "pdf") {
const uint8array = Uint8Array.from(
atob(reader.result.split(",")[1]),
(c) => c.charCodeAt(0)
);
var typedarray = new Uint8Array(uint8array);
pdfTableExtractor(typedarray)
.then((tables) => {
resolve({
file_name: file.name,
file_type: file.name.split(".").pop().toLowerCase(),
tables: tables.pageTables,
});
})
.catch((e) => {
// PDF clientside parsing failed for some reason - defer to the server
console.log(e);
resolve({
file_name: file.name,
file_type: file.name.split(".").pop().toLowerCase(),
});
});
} else {
resolve({
file_name: file.name,
file_type: file.name.split(".").pop().toLowerCase(),
});
}
};
reader.onerror = (e) => {
reject(e);
};
reader.readAsDataURL(file);
})
);
});
Promise.all(frp)
.then((allFiles) => {
const json = JSON.stringify(allFiles, null, 2);
var blob = new Blob([json], { type: "application/json" });
saveFile(blob, "iMadeAJSON.json");
})
.catch((e) => {
console.log(e);
});
}
return (
<Paper
elevation={4}
sx={{
display: "flex",
flexDirection: "column",
width: "100%",
padding: "1rem",
}}
>
<ToastContainer />
<InlineFeedback
message="The file could not be parsed"
severity="error"
state={parseError}
setState={setParseError}
/>
<InlineFeedback
message="The match proceedure failed"
severity="error"
state={matchError}
setState={setMatchError}
/>
<DragDrop filesReceiver={filesReceiver} sx={{ margin: "2rem" }} />
</Paper>
);
}