From 9d4e03507b4ab5dd58e346e88a6432baf588e8cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20La=20Scala?= Date: Mon, 13 Feb 2023 15:18:55 +0100 Subject: [PATCH] fix: await for files before dwnld zip --- package.json | 1 + src/components/common/AppDataToolbar.tsx | 18 ++++++++++++------ yarn.lock | 3 ++- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 31039598..8aae2a5a 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/components/common/AppDataToolbar.tsx b/src/components/common/AppDataToolbar.tsx index b3afaac0..7b46ff7b 100644 --- a/src/components/common/AppDataToolbar.tsx +++ b/src/components/common/AppDataToolbar.tsx @@ -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'; @@ -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(); @@ -38,10 +40,13 @@ const AppDataToolbar: FC = () => { }); const getAllFiles = async (appDataFiles: AppData[]): Promise => { - 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 => { @@ -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); }); }) diff --git a/yarn.lock b/yarn.lock index 988cc5dd..3314b764 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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 @@ -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: