From f968c0221c8dc273283d1a26733197d910d96f72 Mon Sep 17 00:00:00 2001 From: irismarie Date: Thu, 11 Feb 2021 11:45:29 +0100 Subject: [PATCH] download files on first click, more meaningful zipfile name --- src/scenario/scenario.js | 61 ++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/src/scenario/scenario.js b/src/scenario/scenario.js index 293910d..5397bb5 100644 --- a/src/scenario/scenario.js +++ b/src/scenario/scenario.js @@ -116,7 +116,9 @@ class Scenario extends React.Component { modalResultsData: {}, modalResultsIndex: prevState.modalResultsIndex, newResultModal: false, - filesToDownload: [], + filesToDownload: prevState.filesToDownload, + zipfiles: prevState.zipfiles || false, + resultNodl: prevState.resultNodl, editOutputSignalsModal: prevState.editOutputSignalsModal || false, editInputSignalsModal: prevState.editInputSignalsModal || false, @@ -159,25 +161,28 @@ class Scenario extends React.Component { componentDidUpdate(prevProps, prevState) { // check whether file data has been loaded - if (this.state.filesToDownload.length > 0 ) { - if (this.state.filesToDownload.length === 1) { - let fileToDownload = FileStore.getState().filter(file => file.id === this.state.filesToDownload[0]) - if (fileToDownload.length === 1 && fileToDownload[0].data) { - const blob = new Blob([fileToDownload[0].data], {type: fileToDownload[0].type}); - FileSaver.saveAs(blob, fileToDownload[0].name); - this.setState({ filesToDownload: [] }); - } - } else { // zip and save several files - let filesToDownload = FileStore.getState().filter(file => this.state.filesToDownload.includes(file.id) && file.data); - if (filesToDownload.length === this.state.filesToDownload.length) { // all requested files have been loaded - var zip = new JSZip(); - filesToDownload.forEach(file => { - zip.file(file.name, file.data); - }); - zip.generateAsync({type: "blob"}).then(function(content) { - saveAs(content, "results.zip"); - }); - this.setState({ filesToDownload: [] }); + if (this.state.filesToDownload && this.state.filesToDownload.length > 0 ) { + if (this.state.files != prevState.files) { + if (!this.state.zipfiles) { + let fileToDownload = FileStore.getState().filter(file => file.id === this.state.filesToDownload[0]) + if (fileToDownload.length === 1 && fileToDownload[0].data) { + const blob = new Blob([fileToDownload[0].data], {type: fileToDownload[0].type}); + FileSaver.saveAs(blob, fileToDownload[0].name); + this.setState({ filesToDownload: [] }); + } + } else { // zip and save one or more files (download all button) + let filesToDownload = FileStore.getState().filter(file => this.state.filesToDownload.includes(file.id) && file.data); + if (filesToDownload.length === this.state.filesToDownload.length) { // all requested files have been loaded + var zip = new JSZip(); + filesToDownload.forEach(file => { + zip.file(file.name, file.data); + }); + let zipname = "result_" + this.state.resultNodl + "_" + Date.now(); + zip.generateAsync({type: "blob"}).then(function(content) { + saveAs(content, zipname); + }); + this.setState({ filesToDownload: [] }); + } } } } @@ -637,21 +642,19 @@ class Scenario extends React.Component { closeEditResultsModal() { this.setState({ editResultsModal: false }); - - AppDispatcher.dispatch({ - type: 'results/start-load', - token: this.state.sessionToken, - param: '?scenarioID=' + this.state.scenario.id - }) } downloadResultData(param) { let toDownload = []; + let zip = false; - if (typeof(param) === 'object') { + if (typeof(param) === 'object') { // download all files toDownload = param.resultFileIDs; - } else { + zip = true; + this.setState({ filesToDownload: toDownload, zipfiles: zip, resultNodl: param.id }); + } else { // download one file toDownload.push(param); + this.setState({ filesToDownload: toDownload, zipfiles: zip}); } toDownload.forEach(fileid => { @@ -661,8 +664,6 @@ class Scenario extends React.Component { token: this.state.sessionToken }); }); - - this.setState({ filesToDownload: toDownload }); } closeDeleteResultsModal(confirmDelete) {