From 95043a299a5c671d7e6d707d4d754e4c785ae8c6 Mon Sep 17 00:00:00 2001 From: Pablo Estevez Date: Mon, 18 May 2020 10:06:29 -0400 Subject: [PATCH] Fix import and export functions --- __mocks__/electron.js | 16 +++++++- src/js/components/FileTools.react.js | 58 +++++++++++++--------------- 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/__mocks__/electron.js b/__mocks__/electron.js index 0df9292..6d54507 100644 --- a/__mocks__/electron.js +++ b/__mocks__/electron.js @@ -25,9 +25,21 @@ const electron = { // Replace the function we will use in the dialog object dialog: { // Call directly the callback function passing a single valid file name - showOpenDialog: (options, callback) => callback([FILE_NAME]), + showOpenDialog: (options) => { + return new Promise((resolve, reject) => { + resolve({ + filePaths: [FILE_NAME], + }); + }); + }, // Call directly the callback function passing a valid file name - showSaveDialog: (options, callback) => callback(FILE_NAME), + showSaveDialog: (options) => { + return new Promise((resolve, reject) => { + resolve({ + filePath: FILE_NAME, + }); + }); + }, }, }, }; diff --git a/src/js/components/FileTools.react.js b/src/js/components/FileTools.react.js index 0ab5039..af9d4d4 100644 --- a/src/js/components/FileTools.react.js +++ b/src/js/components/FileTools.react.js @@ -9,7 +9,9 @@ */ const React = require('react'); -const { remote: { dialog: Dialog } } = require('electron'); +const { + remote: { dialog: Dialog }, +} = require('electron'); const Fs = require('fs'); const path = require('path'); @@ -40,41 +42,35 @@ class FileTools extends React.Component { }; handleExport = () => { - Dialog.showSaveDialog( - { - defaultPath: dialogDefaultPath, - filters: [dialogFilter], - }, - fileName => { - if (fileName) { - const contents = JSON.stringify( - RuleExporter.export(this.props.rules, this.props.settings) - ); - Fs.writeFile(fileName, contents, importExportEncoding, error => { - if (error) { - Dialog.showErrorBox('Unable to save file', error); - } - }); - } + Dialog.showSaveDialog({ + defaultPath: dialogDefaultPath, + filters: [dialogFilter], + }).then((e: { filePath: string }) => { + if (e.filePath) { + const contents = JSON.stringify( + RuleExporter.export(this.props.rules, this.props.settings) + ); + Fs.writeFile(e.filePath, contents, importExportEncoding, error => { + if (error) { + Dialog.showErrorBox('Unable to save file', error); + } + }); } - ); + }); }; handleImport = () => { - Dialog.showOpenDialog( - { - defaultPath: dialogDefaultPath, - filters: [dialogFilter], - properties: ['openFile'], - }, - (filePaths: Array) => { - if (filePaths && filePaths.length === 1) { - Fs.readFile(filePaths[0], importExportEncoding, (error, data) => { - this.loadFromExportedData(data); - }); - } + Dialog.showOpenDialog({ + defaultPath: dialogDefaultPath, + filters: [dialogFilter], + properties: ['openFile'], + }).then((e: { filePaths: Array }) => { + if (e.filePaths.length === 1) { + Fs.readFile(e.filePaths[0], importExportEncoding, (error, data) => { + this.loadFromExportedData(data); + }); } - ); + }); }; handleNew = () => {