Skip to content

Commit

Permalink
Allow multiple json uploads (why not?)
Browse files Browse the repository at this point in the history
  • Loading branch information
soup-bowl committed Nov 26, 2023
1 parent 861da0c commit 6e40494
Showing 1 changed file with 32 additions and 29 deletions.
61 changes: 32 additions & 29 deletions src/pages/scratchpad.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,39 +59,42 @@ const ScratchpadPage = () => {
const fileInput = document.createElement("input");
fileInput.type = "file";
fileInput.accept = ".json";
fileInput.multiple = true;

fileInput.addEventListener("change", (event) => {
const files = (event.target as HTMLInputElement).files;
if (files && files.length > 0) {
const file = files[0];
const reader = new FileReader();

reader.onload = (e) => {
const text = (e.target?.result || "") as string;
try {
const data = JSON.parse(text) as IPossibleScratchpadItem[];

let collection = getScratches() ?? [];
let count = 0;
data.forEach((item) => {
if (collection.find(exist => exist.id === item.id) === undefined) {
collection?.push(createItemViaPossible(item));
count++;
}
});

saveScratches(collection);
setScratches(collection);
enqueueSnackbar(`Imported ${count.toString()} scratches`);
} catch (error) {
console.error("Error parsing file:", error);
enqueueSnackbar("An error occurred processing the import");
} finally {
handleCloseMenu();
}
};

reader.readAsText(file);
for (let i = 0; i < files.length; ++i) {
const file = files[i];
const reader = new FileReader();

reader.onload = (e) => {
const text = (e.target?.result || "") as string;
try {
const data = JSON.parse(text) as IPossibleScratchpadItem[];

let collection = getScratches() ?? [];
let count = 0;
data.forEach((item) => {
if (collection.find(exist => exist.id === item.id) === undefined) {
collection?.push(createItemViaPossible(item));
count++;
}
});

saveScratches(collection);
setScratches(collection);
enqueueSnackbar(`Imported ${count.toString()} scratches`);
} catch (error) {
console.error("Error parsing file:", error);
enqueueSnackbar("An error occurred processing the import");
} finally {
handleCloseMenu();
}
};

reader.readAsText(file);
}
}
});

Expand Down

0 comments on commit 6e40494

Please sign in to comment.