From 6c25da3280d6ae2ff52f05a824fc32cb287c2634 Mon Sep 17 00:00:00 2001 From: Aminejvm Date: Fri, 5 Mar 2021 17:37:46 +0100 Subject: [PATCH] iframe download error handling --- common/user-behaviors.js | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/common/user-behaviors.js b/common/user-behaviors.js index 5d34fdbc..574035d4 100644 --- a/common/user-behaviors.js +++ b/common/user-behaviors.js @@ -332,23 +332,25 @@ export const downloadZip = async (file) => { } }; -const _nativeDownload = (file) => { +const _nativeDownload = ({ url, onError }) => { const iframe = document.createElement("iframe"); iframe.style.display = "none"; + iframe.src = url; + + const ERROR_MESSAGE = "SLATE_DOWNLOAD_ERROR"; + const hanldeIframeErrors = (e) => { + if (e.data === ERROR_MESSAGE && onError) { + onError(e.data); + } + }; + window.addEventListener("message", hanldeIframeErrors); + iframe.onload = (e) => window.removeEventListener("message", hanldeIframeErrors); - iframe.src = file.url; document.body.appendChild(iframe); - // var element = document.createElement("a"); - // element.setAttribute("href", file.url); - // element.setAttribute("download", file.name); - - // element.style.display = "none"; - // document.body.appendChild(element); - // element.click(); - // document.body.removeChild(element); }; export const compressAndDownloadFiles = async ({ files, name = "slate.zip", resourceURI }) => { + const errorMessage = "Something went wrong with the download. Please try again"; try { if (!(files && files.length > 0)) return; Events.dispatchMessage({ message: "We're preparing your files to download", status: "INFO" }); @@ -373,13 +375,16 @@ export const compressAndDownloadFiles = async ({ files, name = "slate.zip", reso const res = await Actions.createZipToken({ files: downloadFiles, resourceURI }); const downloadLink = Actions.downloadZip({ token: res.data.token, name, resourceURI }); - _nativeDownload({ - name, + await _nativeDownload({ url: downloadLink, + onError: (err) => + Events.dispatchMessage({ + message: errorMessage, + }), }); } catch (e) { console.error(e); - Events.dispatchMessage({ message: "Something went wrong with the download. Please try again" }); + Events.dispatchMessage({ message: errorMessage }); } };