From 6e404940e86b8fc1b59224c6b7d80ac1e18b642f Mon Sep 17 00:00:00 2001 From: soup-bowl Date: Sun, 26 Nov 2023 20:40:58 +0000 Subject: [PATCH] Allow multiple json uploads (why not?) --- src/pages/scratchpad.tsx | 61 +++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/src/pages/scratchpad.tsx b/src/pages/scratchpad.tsx index 3f496ed..87fdb39 100644 --- a/src/pages/scratchpad.tsx +++ b/src/pages/scratchpad.tsx @@ -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); + } } });