Skip to content

Commit

Permalink
fix: fix importAllFromFileList
Browse files Browse the repository at this point in the history
  • Loading branch information
isuke committed Sep 20, 2023
1 parent 02ce222 commit 5dcb58e
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/stores/ioStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,28 @@ export default {
if (payload.callback) payload.callback()
},
importAllFromFileList: ({ dispatch }: { dispatch: Dispatch }, payload: { fileList: FileList; callback: () => void }) => {
const fileNames: string[] = []
const files: File[] = []
const promiseList: Promise<{ filename: string; content: string }>[] = []

Array.from(payload.fileList).forEach((file) => {
fileNames.push(file.name)
files.push(file)
promiseList.push(
new Promise<{ filename: string; content: string }>((resolve, _reject) => {
const reader = new FileReader()
reader.onload = () => resolve({ filename: file.name, content: reader.result as string })
reader.readAsText(file)
}),
)
})
dispatch("importAll", {
fileNames: fileNames,
files: files,
callback: payload.callback,

Promise.all(promiseList).then((result) => {
const files: { [name: string]: string } = {}

result.forEach((r) => {
if (/\.json$/.test(r.filename) || /\.advancedfilter$/.test(r.filename)) {
files[r.filename] = r.content
}
})

dispatch("importAll", { files: files, callback: payload.callback })
})
},
importAllFromZip: async ({ dispatch }: { dispatch: Dispatch }, payload: { file: File; callback?: () => void }) => {
Expand Down

0 comments on commit 5dcb58e

Please sign in to comment.