Skip to content

Commit

Permalink
fix: await for files before dwnld zip
Browse files Browse the repository at this point in the history
  • Loading branch information
swouf committed Feb 13, 2023
1 parent 59ca417 commit 9d4e035
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
"ts-loader": "9.4.2",
"uglifyjs-webpack-plugin": "~2.2.0",
"wait-on": "7.0.1",
"webpack": "~5.75.0",
"webpack-cli": "~5.0.1"
},
"release": {
Expand Down
18 changes: 12 additions & 6 deletions src/components/common/AppDataToolbar.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import saveAs from 'file-saver';
import { t } from 'i18next';
import zip from 'jszip';
import JSZip from 'jszip';

import React, { FC, useState } from 'react';

Expand All @@ -17,6 +17,8 @@ import { hooks } from '../../config/queryClient';
import { showErrorToast } from '../../utils/toasts';
import { useAppDataContext } from '../context/AppDataContext';

const zip = new JSZip();

const AppDataToolbar: FC = () => {
const { appDataArray } = useAppDataContext();

Expand All @@ -38,10 +40,13 @@ const AppDataToolbar: FC = () => {
});

const getAllFiles = async (appDataFiles: AppData[]): Promise<void> => {
appDataFiles.forEach((appDataFile) => {
const name: string = (appDataFile.data?.name as string) ?? appDataFile.id;
getFile(appDataFile.id).then((file) => zip.file(name, file));
});
await Promise.all(
appDataFiles.map(async (appDataFile) => {
const name: string =
(appDataFile.data?.name as string) ?? appDataFile.id;
await getFile(appDataFile.id).then((file) => zip.file(name, file));
}),
);
};

const handleDownloadAll = async (): Promise<void> => {
Expand All @@ -52,7 +57,8 @@ const AppDataToolbar: FC = () => {
getAllFiles(appDataFiles.toArray())
.then(() => {
zip.generateAsync({ type: 'blob' }).then((archive) => {
saveAs(archive, `uploaded_files_${Date().toString()}`);
const d = new Date();
saveAs(archive, `uploaded_files_${d.toISOString()}`);
setIsLoading(false);
});
})
Expand Down
3 changes: 2 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -10080,6 +10080,7 @@ __metadata:
uglifyjs-webpack-plugin: ~2.2.0
uuid: 9.0.0
wait-on: 7.0.1
webpack: ~5.75.0
webpack-cli: ~5.0.1
languageName: unknown
linkType: soft
Expand Down Expand Up @@ -19206,7 +19207,7 @@ __metadata:
languageName: node
linkType: hard

"webpack@npm:^5.64.4":
"webpack@npm:^5.64.4, webpack@npm:~5.75.0":
version: 5.75.0
resolution: "webpack@npm:5.75.0"
dependencies:
Expand Down

0 comments on commit 9d4e035

Please sign in to comment.