-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Modularized Effects from Routes
- Loading branch information
1 parent
ab4422b
commit c086e4d
Showing
7 changed files
with
179 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { remote } from 'electron'; | ||
import { save } from '../utils/FileAccess'; | ||
|
||
const SaveBeforeQuittingDialog = (setQuitAnyways) => { | ||
return { | ||
title: 'Save before quitting?', | ||
text: 'Do you want to save your changes before quitting?', | ||
onConfirm: (dispatch) => { | ||
dispatch(save()); | ||
setQuitAnyways(false); | ||
remote.getCurrentWindow().close(); | ||
}, | ||
onReject: () => { | ||
setQuitAnyways(true); | ||
remote.getCurrentWindow().close(); | ||
}, | ||
onCancel: () => { | ||
setQuitAnyways(false); | ||
}, | ||
}; | ||
}; | ||
|
||
export default SaveBeforeQuittingDialog; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { ipcRenderer } from 'electron'; | ||
import { EffectCallback } from 'react'; | ||
import { BACKUP_START, BACKUP_STOP } from '../constants/BackupIPC'; | ||
|
||
const BackupEffect = ( | ||
workspacePath: string, | ||
saveBackups: boolean | ||
): EffectCallback => { | ||
return () => { | ||
// Start Backup | ||
if (workspacePath.length > 0 && saveBackups) { | ||
ipcRenderer.send(BACKUP_START, workspacePath); | ||
} | ||
return () => { | ||
// Stop Backup | ||
ipcRenderer.send(BACKUP_STOP); | ||
}; | ||
}; | ||
}; | ||
|
||
export default BackupEffect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { ipcRenderer } from 'electron'; | ||
import { UpdateInfo } from 'electron-updater'; | ||
import { | ||
CHECK_FOR_UPDATE_PENDING, | ||
CHECK_FOR_UPDATE_SUCCESS, | ||
REQUEST_FILE_PATH, | ||
} from '../constants/ipc'; | ||
import { version as currentAppVersion } from '../package.json'; | ||
|
||
const CheckForUpdatesEffect = (updaterDialog) => { | ||
return () => { | ||
ipcRenderer.on( | ||
CHECK_FOR_UPDATE_SUCCESS, | ||
(_event, info: UpdateInfo | undefined) => { | ||
const version = info && info.version; | ||
if (version && version !== currentAppVersion) { | ||
// Show updater dialog | ||
updaterDialog(false); | ||
} | ||
} | ||
); | ||
|
||
// Check for updates at start | ||
ipcRenderer.send(CHECK_FOR_UPDATE_PENDING); | ||
// Get file path | ||
ipcRenderer.send(REQUEST_FILE_PATH); | ||
|
||
return () => { | ||
ipcRenderer.removeAllListeners(CHECK_FOR_UPDATE_SUCCESS); | ||
}; | ||
}; | ||
}; | ||
|
||
export default CheckForUpdatesEffect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { ipcRenderer } from 'electron'; | ||
import * as Path from 'path'; | ||
import { RECEIVE_FILE_PATH } from '../constants/ipc'; | ||
import ConfirmationDialog from '../dialogs/ConfirmationDialog'; | ||
import UnsavedChangesDialog from '../dialogs/UnsavedChangesDialog'; | ||
import { workspaceSetPath } from '../features/workspace/workspaceSlice'; | ||
import { reloadState } from '../utils/FileAccess'; | ||
|
||
const LoadNewFileEffect = (dispatch, showModal, unsavedChanges) => { | ||
return () => { | ||
const loadNewFile = (_event, path) => { | ||
if (Path.extname(path) === '.cor') { | ||
if (unsavedChanges) { | ||
showModal(ConfirmationDialog, UnsavedChangesDialog(path)); | ||
} else { | ||
dispatch(workspaceSetPath(path)); | ||
dispatch(reloadState()); | ||
} | ||
} | ||
}; | ||
|
||
ipcRenderer.on(RECEIVE_FILE_PATH, loadNewFile); | ||
return () => { | ||
ipcRenderer.removeListener(RECEIVE_FILE_PATH, loadNewFile); | ||
}; | ||
}; | ||
}; | ||
|
||
export default LoadNewFileEffect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { ipcRenderer } from 'electron'; | ||
import { REQUEST_FILE_PATH } from '../constants/ipc'; | ||
|
||
const RequestFilePathEffect = () => { | ||
return () => { | ||
// Get file path | ||
ipcRenderer.send(REQUEST_FILE_PATH); | ||
}; | ||
}; | ||
|
||
export default RequestFilePathEffect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { EffectCallback } from 'react'; | ||
import ConfirmationDialog from '../dialogs/ConfirmationDialog'; | ||
import SaveBeforeQuittingDialog from '../dialogs/SaveBeforeQuittingDialog'; | ||
|
||
const SaveBeforeQuittingEffect = ( | ||
quitAnyways, | ||
setQuitAnyways, | ||
reload, | ||
setReload, | ||
showModal, | ||
unsavedChanges | ||
): EffectCallback => { | ||
return () => { | ||
const beforeQuit = (e: BeforeUnloadEvent) => { | ||
if (reload) { | ||
setReload(false); | ||
} else if (unsavedChanges && !quitAnyways) { | ||
e.returnValue = false; | ||
e.preventDefault(); | ||
showModal(ConfirmationDialog, SaveBeforeQuittingDialog(setQuitAnyways)); | ||
} | ||
}; | ||
window.addEventListener('beforeunload', beforeQuit, true); | ||
return () => { | ||
window.removeEventListener('beforeunload', beforeQuit, true); | ||
}; | ||
}; | ||
}; | ||
|
||
export default SaveBeforeQuittingEffect; |