Skip to content
This repository has been archived by the owner on Apr 20, 2023. It is now read-only.

Commit

Permalink
Merge pull request #132 from facebook/fix/import-export
Browse files Browse the repository at this point in the history
Fix import and export functions
  • Loading branch information
pestevez authored May 18, 2020
2 parents 979faf3 + 95043a2 commit ee16407
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 33 deletions.
16 changes: 14 additions & 2 deletions __mocks__/electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
});
},
},
},
};
Expand Down
58 changes: 27 additions & 31 deletions src/js/components/FileTools.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -40,41 +42,35 @@ class FileTools extends React.Component<Props> {
};

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<string>) => {
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<string> }) => {
if (e.filePaths.length === 1) {
Fs.readFile(e.filePaths[0], importExportEncoding, (error, data) => {
this.loadFromExportedData(data);
});
}
);
});
};

handleNew = () => {
Expand Down

0 comments on commit ee16407

Please sign in to comment.