Skip to content

Commit

Permalink
fix: review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
eole1712 committed Sep 29, 2021
1 parent db8c718 commit ab803d9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
Expand Up @@ -162,12 +162,11 @@ export class QueryCollectionEffects {
switchMap(() => {
return from(openFiles({ accept: ".agc" }));
}),
pipe(filter<string[]>((e) => e !== undefined)),
switchMap((data: string[]) =>
data.map((elem) =>
tap((data) => {
data.forEach((elem) =>
this.collectionService.importCollectionDataFromJson(elem)
)
),
}),
tap(() =>
this.notifyService.success("Successfully imported collection.")
),
Expand Down
37 changes: 18 additions & 19 deletions packages/altair-app/src/app/modules/altair/utils/index.ts
Expand Up @@ -64,24 +64,6 @@ export const getFileStr = (files: FileList) => {
fileReader.readAsText(files[0]);
});
};

export const getFilesStr = (files: FileList) => {
return Promise.all(
Array.from(Array(files.length).keys()).map(
(_, index) =>
new Promise<string>((resolve, reject) => {
const fileReader = new FileReader();
fileReader.onload = function (e: any) {
const contents: string = e.target.result;

// Resolve file content
resolve(contents);
};
fileReader.readAsText(files[index]);
})
)
);
};
interface FileDialogOptions {
readonly multiple?: boolean;
readonly accept?: string|ReadonlyArray<string>;
Expand All @@ -94,12 +76,29 @@ export const openFile = async (opts: FileDialogOptions = {}) => {
debug.log('There was an issue while opening the file: ', err);
}
};

export const getFileContent = async (file: File) => {
return new Promise<string>((resolve, reject) => {
const fileReader = new FileReader();
fileReader.onload = function (e: any) {
const contents: string = e.target.result;

// Resolve file content
resolve(contents);
};
fileReader.readAsText(file);
})
}

export const openFiles = async (opts: FileDialogOptions = {}) => {
try {
const files = await fileDialog({ ...opts, multiple: true });
return getFilesStr(files);

return Promise.all(Array.from(Array(files.length).map((_, i) => getFileContent(files[i]))));
} catch (err) {
debug.log('There was an issue while opening the files: ', err);

return Promise.all([]);
}
};

Expand Down

0 comments on commit ab803d9

Please sign in to comment.