From df4a678a154f7ad9f8c55cb2923e1dcab3ac6f22 Mon Sep 17 00:00:00 2001 From: Tony Giorgio Date: Tue, 16 May 2023 18:01:51 -0500 Subject: [PATCH] Make imports await on file upload --- src/components/ImportExport.tsx | 39 ++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/components/ImportExport.tsx b/src/components/ImportExport.tsx index 983b569..8d6e780 100644 --- a/src/components/ImportExport.tsx +++ b/src/components/ImportExport.tsx @@ -24,27 +24,36 @@ export function ImportExport() { try { const file: File = files()[0].file; - fileReader.readAsText(file, "UTF-8"); - fileReader.onload = e => { - const text = e.target?.result?.toString(); - if (text) { - // This should throw if there's a parse error, so we won't end up clearing - JSON.parse(text); + const text = await new Promise((resolve, reject) => { + fileReader.onload = e => { + const result = e.target?.result?.toString(); + if (result) { + resolve(result); + } else { + reject(new Error("No text found in file")); + } + }; + fileReader.onerror = e => reject(new Error("File read error")); + fileReader.readAsText(file, "UTF-8"); + }); - MutinyWallet.import_json(text); + // This should throw if there's a parse error, so we won't end up clearing + JSON.parse(text); - window.location.href = "/" - - } else { - throw new Error("No text found in file") - } + MutinyWallet.import_json(text); + if (state.mutiny_wallet) { + await state.mutiny_wallet.stop(); } + + window.location.href = "/"; + } catch (e) { showToast(eify(e)); + } finally { + setConfirmOpen(false); + setConfirmLoading(false); } - setConfirmOpen(false); - setConfirmLoading(false); } async function uploadFile() { @@ -72,4 +81,4 @@ export function ImportExport() { ) -} \ No newline at end of file +}