Refactoring
This commit is contained in:
186
src/App.js
186
src/App.js
@@ -1,4 +1,4 @@
|
||||
import React, {Component} from 'react';
|
||||
import React from 'react';
|
||||
import axios from 'axios';
|
||||
import styles from './App.css';
|
||||
import Box from './components/UI/Box/Box';
|
||||
@@ -15,16 +15,12 @@ import ReleaseVersionDisplay from './components/ReleaseVersionDisplay/ReleaseVer
|
||||
import Text from './components/UI/Text/Text';
|
||||
import UpgradeIcon from './components/UpgradeIcon/UpgradeIcon';
|
||||
import UpgradeUI from './components/UpgradeUI/UpgradeUI';
|
||||
import IPCContainer from './containers/IPCContainer/IPCContainer';
|
||||
|
||||
const Constants = require('./constants');
|
||||
const Scheduler = require('node-schedule');
|
||||
|
||||
let ipcRenderer = null;
|
||||
if (!process.versions.hasOwnProperty('electron')) {
|
||||
ipcRenderer = ((window && window.require) ? window.require('electron').ipcRenderer : null);
|
||||
}
|
||||
|
||||
class App extends Component {
|
||||
class App extends IPCContainer {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
|
||||
@@ -36,20 +32,18 @@ class App extends Component {
|
||||
}
|
||||
}
|
||||
|
||||
if (ipcRenderer) {
|
||||
ipcRenderer.on(Constants.IPC_Check_Installed_Reply, this.onCheckInstalledReply);
|
||||
ipcRenderer.on(Constants.IPC_Download_File_Complete, this.onDownloadFileComplete);
|
||||
ipcRenderer.on(Constants.IPC_Download_File_Progress, this.onDownloadFileProgress);
|
||||
ipcRenderer.on(Constants.IPC_Extract_Release_Complete, this.onExtractReleaseComplete);
|
||||
ipcRenderer.on(Constants.IPC_Get_State_Reply, this.onGetStateReply);
|
||||
ipcRenderer.on(Constants.IPC_Grab_Releases_Reply, this.onGrabReleasesReply);
|
||||
ipcRenderer.on(Constants.IPC_Grab_UI_Releases_Reply, this.onGrabUiReleasesReply);
|
||||
ipcRenderer.on(Constants.IPC_Install_Dependency_Reply, this.onInstallDependencyReply);
|
||||
ipcRenderer.on(Constants.IPC_Install_Upgrade_Reply, this.onInstallUpgradeReply);
|
||||
this.setRequestHandler(Constants.IPC_Check_Installed_Reply, this.onCheckInstalledReply);
|
||||
this.setRequestHandler(Constants.IPC_Download_File_Complete, this.onDownloadFileComplete);
|
||||
this.setRequestHandler(Constants.IPC_Download_File_Progress, this.onDownloadFileProgress);
|
||||
this.setRequestHandler(Constants.IPC_Extract_Release_Complete, this.onExtractReleaseComplete);
|
||||
this.setRequestHandler(Constants.IPC_Get_State_Reply, this.onGetStateReply);
|
||||
this.setRequestHandler(Constants.IPC_Grab_Releases_Reply, this.onGrabReleasesReply);
|
||||
this.setRequestHandler(Constants.IPC_Grab_UI_Releases_Reply, this.onGrabUiReleasesReply);
|
||||
this.setRequestHandler(Constants.IPC_Install_Dependency_Reply, this.onInstallDependencyReply);
|
||||
this.setRequestHandler(Constants.IPC_Install_Upgrade_Reply, this.onInstallUpgradeReply);
|
||||
|
||||
ipcRenderer.send(Constants.IPC_Get_State, Constants.DATA_LOCATIONS[this.props.platform]);
|
||||
Scheduler.scheduleJob('23 11 * * *', this.updateCheckScheduledJob);
|
||||
}
|
||||
this.sendRequest(Constants.IPC_Get_State, Constants.DATA_LOCATIONS[this.props.platform]);
|
||||
Scheduler.scheduleJob('23 11 * * *', this.updateCheckScheduledJob);
|
||||
}
|
||||
|
||||
state = {
|
||||
@@ -106,18 +100,16 @@ class App extends Component {
|
||||
}, ()=> {
|
||||
const selectedVersion = this.getSelectedVersion();
|
||||
if (selectedVersion !== 'unavailable') {
|
||||
if (ipcRenderer) {
|
||||
let dependencies = [];
|
||||
if (this.state.LocationsLookup[selectedVersion] && this.state.LocationsLookup[selectedVersion].dependencies) {
|
||||
dependencies = this.state.LocationsLookup[selectedVersion].dependencies;
|
||||
}
|
||||
|
||||
ipcRenderer.send(Constants.IPC_Check_Installed, {
|
||||
Dependencies: dependencies,
|
||||
Directory: Constants.DATA_LOCATIONS[this.props.platform],
|
||||
Version: selectedVersion,
|
||||
});
|
||||
let dependencies = [];
|
||||
if (this.state.LocationsLookup[selectedVersion] && this.state.LocationsLookup[selectedVersion].dependencies) {
|
||||
dependencies = this.state.LocationsLookup[selectedVersion].dependencies;
|
||||
}
|
||||
|
||||
this.sendRequest(Constants.IPC_Check_Installed, {
|
||||
Dependencies: dependencies,
|
||||
Directory: Constants.DATA_LOCATIONS[this.props.platform],
|
||||
Version: selectedVersion,
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
@@ -128,9 +120,7 @@ class App extends Component {
|
||||
}
|
||||
|
||||
if (this.state.ErrorCritical) {
|
||||
if (ipcRenderer) {
|
||||
ipcRenderer.send(Constants.IPC_Shutdown);
|
||||
}
|
||||
this.sendRequest(Constants.IPC_Shutdown);
|
||||
} else {
|
||||
this.setState({
|
||||
DisplayError: false,
|
||||
@@ -139,20 +129,6 @@ class App extends Component {
|
||||
}
|
||||
};
|
||||
|
||||
componentWillUnmount = () => {
|
||||
if (ipcRenderer) {
|
||||
ipcRenderer.removeListener(Constants.IPC_Check_Installed_Reply, this.onCheckInstalledReply);
|
||||
ipcRenderer.removeListener(Constants.IPC_Download_File_Complete, this.onDownloadFileComplete);
|
||||
ipcRenderer.removeListener(Constants.IPC_Download_File_Progress, this.onDownloadFileProgress);
|
||||
ipcRenderer.removeListener(Constants.IPC_Extract_Release_Complete, this.onExtractReleaseComplete);
|
||||
ipcRenderer.removeListener(Constants.IPC_Get_State_Reply, this.onGetStateReply);
|
||||
ipcRenderer.removeListener(Constants.IPC_Grab_Releases_Reply, this.onGrabReleasesReply);
|
||||
ipcRenderer.removeListener(Constants.IPC_Grab_UI_Releases_Reply, this.onGrabUiReleasesReply);
|
||||
ipcRenderer.removeListener(Constants.IPC_Install_Dependency_Reply, this.onInstallDependencyReply);
|
||||
ipcRenderer.removeListener(Constants.IPC_Install_Upgrade_Reply, this.onInstallUpgradeReply);
|
||||
}
|
||||
};
|
||||
|
||||
extractFileNameFromURL = url => {
|
||||
const parts = url.split('/');
|
||||
return parts[parts.length - 1];
|
||||
@@ -161,7 +137,7 @@ class App extends Component {
|
||||
extractRelease = (data) => {
|
||||
if (data.Success) {
|
||||
const selectedVersion = this.getSelectedVersion();
|
||||
ipcRenderer.send(Constants.IPC_Extract_Release, {
|
||||
this.sendRequest(Constants.IPC_Extract_Release, {
|
||||
Directory: Constants.DATA_LOCATIONS[this.props.platform],
|
||||
Source: data.Destination,
|
||||
Version: selectedVersion,
|
||||
@@ -183,10 +159,8 @@ class App extends Component {
|
||||
|
||||
grabReleases = () => {
|
||||
if (this.props.platform !== 'unknown') {
|
||||
if (ipcRenderer) {
|
||||
ipcRenderer.send(Constants.IPC_Grab_Releases);
|
||||
ipcRenderer.send(Constants.IPC_Grab_UI_Releases);
|
||||
}
|
||||
this.sendRequest(Constants.IPC_Grab_Releases);
|
||||
this.sendRequest(Constants.IPC_Grab_UI_Releases);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -227,19 +201,17 @@ class App extends Component {
|
||||
};
|
||||
|
||||
handleDependencyDownload = (url) => {
|
||||
if (ipcRenderer) {
|
||||
this.setState({
|
||||
DownloadActive: true,
|
||||
DownloadingDependency: true,
|
||||
DownloadName: this.extractFileNameFromURL(url),
|
||||
}, ()=> {
|
||||
ipcRenderer.send(Constants.IPC_Download_File, {
|
||||
Directory: Constants.DATA_LOCATIONS[this.props.platform],
|
||||
Filename: this.state.DownloadName,
|
||||
URL: url,
|
||||
});
|
||||
this.setState({
|
||||
DownloadActive: true,
|
||||
DownloadingDependency: true,
|
||||
DownloadName: this.extractFileNameFromURL(url),
|
||||
}, ()=> {
|
||||
this.sendRequest(Constants.IPC_Download_File, {
|
||||
Directory: Constants.DATA_LOCATIONS[this.props.platform],
|
||||
Filename: this.state.DownloadName,
|
||||
URL: url,
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
handleMountLocationChanged = (storageType, location) => {
|
||||
@@ -269,38 +241,32 @@ class App extends Component {
|
||||
handleReleaseDownload = () => {
|
||||
const selectedVersion = this.getSelectedVersion();
|
||||
const fileName = selectedVersion + '.zip';
|
||||
if (ipcRenderer) {
|
||||
this.setState({
|
||||
DownloadActive: true,
|
||||
DownloadingRelease: true,
|
||||
DownloadName: fileName,
|
||||
}, () => {
|
||||
ipcRenderer.send(Constants.IPC_Download_File, {
|
||||
Directory: Constants.DATA_LOCATIONS[this.props.platform],
|
||||
Filename: this.state.DownloadName,
|
||||
URL: this.state.LocationsLookup[selectedVersion].urls[0],
|
||||
});
|
||||
this.setState({
|
||||
DownloadActive: true,
|
||||
DownloadingRelease: true,
|
||||
DownloadName: fileName,
|
||||
}, () => {
|
||||
this.sendRequest(Constants.IPC_Download_File, {
|
||||
Directory: Constants.DATA_LOCATIONS[this.props.platform],
|
||||
Filename: this.state.DownloadName,
|
||||
URL: this.state.LocationsLookup[selectedVersion].urls[0],
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
handleUIDownload = () => {
|
||||
if (ipcRenderer) {
|
||||
this.setState({
|
||||
DownloadActive: true,
|
||||
DownloadingUpgrade: true,
|
||||
DownloadName: 'UI Upgrade',
|
||||
}, ()=> {
|
||||
const url = this.state.UpgradeData.urls[0];
|
||||
ipcRenderer.send(Constants.IPC_Download_File, {
|
||||
Directory: Constants.DATA_LOCATIONS[this.props.platform],
|
||||
Filename: this.props.platform === 'win32' ? 'upgrade.exe' : this.extractFileNameFromURL(url),
|
||||
URL: url,
|
||||
});
|
||||
this.setState({
|
||||
DownloadActive: true,
|
||||
DownloadingUpgrade: true,
|
||||
DownloadName: 'UI Upgrade',
|
||||
}, ()=> {
|
||||
const url = this.state.UpgradeData.urls[0];
|
||||
this.sendRequest(Constants.IPC_Download_File, {
|
||||
Directory: Constants.DATA_LOCATIONS[this.props.platform],
|
||||
Filename: this.props.platform === 'win32' ? 'upgrade.exe' : this.extractFileNameFromURL(url),
|
||||
URL: url,
|
||||
});
|
||||
} else {
|
||||
this.setState({UpgradeDismissed: true});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
handleVersionChanged = (e) => {
|
||||
@@ -314,7 +280,7 @@ class App extends Component {
|
||||
|
||||
installDependency = data => {
|
||||
if (data.Success) {
|
||||
ipcRenderer.send(Constants.IPC_Install_Dependency, {
|
||||
this.sendRequest(Constants.IPC_Install_Dependency, {
|
||||
Source: data.Destination,
|
||||
URL: data.URL,
|
||||
});
|
||||
@@ -330,7 +296,7 @@ class App extends Component {
|
||||
|
||||
installUpgrade = data => {
|
||||
if (data.Success) {
|
||||
ipcRenderer.send(Constants.IPC_Install_Upgrade, {
|
||||
this.sendRequest(Constants.IPC_Install_Upgrade, {
|
||||
Source: data.Destination,
|
||||
});
|
||||
} else {
|
||||
@@ -404,7 +370,7 @@ class App extends Component {
|
||||
};
|
||||
|
||||
onExtractReleaseComplete = (event, arg) => {
|
||||
ipcRenderer.send(Constants.IPC_Delete_File, {
|
||||
this.sendRequest(Constants.IPC_Delete_File, {
|
||||
FilePath: arg.data.Source,
|
||||
});
|
||||
|
||||
@@ -521,7 +487,7 @@ class App extends Component {
|
||||
if (arg.data.Success && arg.data.Source.toLowerCase().endsWith('.dmg')) {
|
||||
this.waitForDependencyInstall(arg.data.URL);
|
||||
} else {
|
||||
ipcRenderer.send(Constants.IPC_Delete_File, {
|
||||
this.sendRequest(Constants.IPC_Delete_File, {
|
||||
FilePath: arg.data.Source,
|
||||
});
|
||||
this.checkVersionInstalled();
|
||||
@@ -529,7 +495,7 @@ class App extends Component {
|
||||
};
|
||||
|
||||
onInstallUpgradeReply = (event, arg) => {
|
||||
ipcRenderer.sendSync(Constants.IPC_Delete_File, {
|
||||
this.sendSyncRequest(Constants.IPC_Delete_File, {
|
||||
FilePath: arg.data.Source,
|
||||
});
|
||||
|
||||
@@ -541,20 +507,18 @@ class App extends Component {
|
||||
};
|
||||
|
||||
saveState = version => {
|
||||
if (ipcRenderer) {
|
||||
let state = {
|
||||
Release: this.state.Release,
|
||||
Version: version || this.state.Version,
|
||||
};
|
||||
for (const provider of Constants.PROVIDER_LIST) {
|
||||
state[provider] = this.state[provider];
|
||||
}
|
||||
|
||||
ipcRenderer.send(Constants.IPC_Save_State, {
|
||||
Directory: Constants.DATA_LOCATIONS[this.props.platform],
|
||||
State: state
|
||||
});
|
||||
let state = {
|
||||
Release: this.state.Release,
|
||||
Version: version || this.state.Version,
|
||||
};
|
||||
for (const provider of Constants.PROVIDER_LIST) {
|
||||
state[provider] = this.state[provider];
|
||||
}
|
||||
|
||||
this.sendRequest(Constants.IPC_Save_State, {
|
||||
Directory: Constants.DATA_LOCATIONS[this.props.platform],
|
||||
State: state
|
||||
});
|
||||
};
|
||||
|
||||
setErrorState = (error, action, critical) => {
|
||||
@@ -578,7 +542,7 @@ class App extends Component {
|
||||
});
|
||||
|
||||
const i = setInterval(()=> {
|
||||
const ret = ipcRenderer.sendSync(Constants.IPC_Check_Dependency_Installed, {
|
||||
const ret = this.sendSyncRequest(Constants.IPC_Check_Dependency_Installed, {
|
||||
File: dep.file,
|
||||
});
|
||||
if (ret.data.Exists || !ret.data.Success) {
|
||||
|
||||
Reference in New Issue
Block a user