0
\$\begingroup\$

I have implemented a "quick" save for my electron app. There has to be a better way to achieve what I am doing.

The MessageBox will always be triggered on a save as command. If the user selects save (Ctrl+S), I'm basically keeping a variable to hold if a file has been loaded from the file system or not. If a file has been loaded I take the array from fs and convert into a string and use that as the filename for the quick save.

If no file has been uploaded, this means my gantt area is clear and the SaveDialog box will be triggered since this isn't a loaded file. A little code to further explain. Before the quicksave function the filename is either undefined or a string from the fs array and stored in quickSaveFilename.

// dirty quicksave
const quickSave = () => {
 let saveData = gantt.serialize();
 saveData = JSON.stringify(saveData, null, '\t');
 if (quickSaveFileName === undefined) {
 dialog.showSaveDialog(
 {
 defaultPath: `C:\\Users\\${process.env.USERNAME}\\Documents\\`,
 filters: [
 {
 name: 'json',
 extensions: ['json'],
 },
 ],
 },
 (filename) => {
 if (filename === undefined) {
 return;
 }
 fs.writeFile(filename, content, (error) => {
 if (error) {
 dialog.showErrorBox(
 'Save Failed',
 `An error occured saving the file ${error.message}`,
 );
 return;
 }
 dialog.showMessageBox({
 type: 'none',
 title: 'Ganttron',
 message: 'The chart was successfully saved',
 buttons: ['OK'],
 });
 });
 });
 } else {
 fs.writeFile(quickSaveFileName, saveData, (error) => {
 if (error) {
 dialog.showErrorBox(
 'Save Failed',
 `An error occured saving the file ${error.message}`,
 );
 }
 dialog.showMessageBox({
 type: 'none',
 title: 'Ganttron',
 message: 'The chart was successfully saved',
 buttons: ['OK'],
 });
 });
 }
};

Is there a more efficient way of doing this in electron?

200_success
146k22 gold badges190 silver badges479 bronze badges
asked Aug 26, 2017 at 4:34
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

We can make the code simpler with a saveFile function

function saveFile(filename, data) {
 fs.writeFile(filename, content, (error) => {
 if (error) {
 dialog.showErrorBox(
 'Save Failed',
 `An error occured saving the file ${error.message}`,
 );
 return;
 }
 dialog.showMessageBox({
 type: 'none',
 title: 'Ganttron',
 message: 'The chart was successfully saved',
 buttons: ['OK'],
 });
 });
}
const quickSave = () => {
 let saveData = gantt.serialize();
 saveData = JSON.stringify(saveData, null, '\t');
 if (quickSaveFileName === undefined) {
 dialog.showSaveDialog({
 defaultPath: `C:\\Users\\${process.env.USERNAME}\\Documents\\`,
 filters: [{
 name: 'json',
 extensions: ['json'],
 }, ],
 },
 (filename) => {
 if (filename === undefined) {
 return;
 }
 saveFile(quickSaveFileName, saveData);
 });
 } else {
 saveFile(quickSaveFileName, saveData);
 }
};
answered Sep 16, 2017 at 16:28
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.