Redux changes and refactoring

This commit is contained in:
Scott E. Graves
2019-06-07 00:30:59 -05:00
parent 1dc99567f3
commit 31585fc912
6 changed files with 179 additions and 140 deletions

View File

@@ -17,17 +17,22 @@ import UpgradeIcon from './components/UpgradeIcon/UpgradeIcon';
import UpgradeUI from './components/UpgradeUI/UpgradeUI'; import UpgradeUI from './components/UpgradeUI/UpgradeUI';
import {setProviderState} from './redux/actions/mount_actions'; import {setProviderState} from './redux/actions/mount_actions';
import {detectUIUpgrade, loadReleases, setActiveRelease, setDismissUIUpgrade, setReleaseUpgradeAvailable} from './redux/actions/release_version_actions'; import {detectUIUpgrade, loadReleases, setActiveRelease, setDismissUIUpgrade, setReleaseUpgradeAvailable} from './redux/actions/release_version_actions';
import {downloadItem, setAllowDownload} from './redux/actions/download_actions';
const Constants = require('./constants'); const Constants = require('./constants');
const Scheduler = require('node-schedule'); const Scheduler = require('node-schedule');
const DownloadTypes = {
Dependency: 'dependency',
Release: 'release',
Upgrade: 'upgrade',
};
class App extends IPCContainer { class App extends IPCContainer {
constructor(props) { constructor(props) {
super(props); super(props);
this.setRequestHandler(Constants.IPC_Check_Installed_Reply, this.onCheckInstalledReply); 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_Extract_Release_Complete, this.onExtractReleaseComplete);
this.setRequestHandler(Constants.IPC_Get_State_Reply, this.onGetStateReply); this.setRequestHandler(Constants.IPC_Get_State_Reply, this.onGetStateReply);
this.setRequestHandler(Constants.IPC_Install_Dependency_Reply, this.onInstallDependencyReply); this.setRequestHandler(Constants.IPC_Install_Dependency_Reply, this.onInstallDependencyReply);
@@ -35,39 +40,29 @@ class App extends IPCContainer {
} }
state = { state = {
AllowDownload: false,
DisplayError: false, DisplayError: false,
Error: null, Error: null,
ErrorAction: null, ErrorAction: null,
ErrorCritical: false, ErrorCritical: false,
DownloadActive: false,
DownloadProgress: 0.0,
DownloadingDependency: false,
DownloadName: '',
DownloadingRelease: false,
DownloadingUpgrade: false,
ExtractActive: false, ExtractActive: false,
MissingDependencies: [], MissingDependencies: [],
InstalledVersion: 'none', InstalledVersion: 'none',
}; };
checkVersionInstalled = () => { checkVersionInstalled = () => {
this.setState({ this.props.setAllowDownload(false);
AllowDownload: false, const selectedVersion = this.getSelectedVersion();
}, ()=> { if (selectedVersion !== 'unavailable') {
const selectedVersion = this.getSelectedVersion(); let dependencies = [];
if (selectedVersion !== 'unavailable') { if (this.props.LocationsLookup[selectedVersion] && this.props.LocationsLookup[selectedVersion].dependencies) {
let dependencies = []; dependencies = this.props.LocationsLookup[selectedVersion].dependencies;
if (this.props.LocationsLookup[selectedVersion] && this.props.LocationsLookup[selectedVersion].dependencies) {
dependencies = this.props.LocationsLookup[selectedVersion].dependencies;
}
this.sendRequest(Constants.IPC_Check_Installed, {
Dependencies: dependencies,
Version: selectedVersion,
});
} }
});
this.sendRequest(Constants.IPC_Check_Installed, {
Dependencies: dependencies,
Version: selectedVersion,
});
}
}; };
closeErrorDisplay = () => { closeErrorDisplay = () => {
@@ -121,14 +116,6 @@ class App extends IPCContainer {
Version: selectedVersion, Version: selectedVersion,
}); });
} }
this.setState({
DownloadActive: false,
DownloadProgress: 0.0,
DownloadingRelease: false,
ExtractActive: data.Success,
DownloadName: '',
});
}; };
detectUpgrades = () => { detectUpgrades = () => {
@@ -144,46 +131,20 @@ class App extends IPCContainer {
this.props.VersionLookup[Constants.RELEASE_TYPES[this.props.Release]][this.props.ReleaseVersion]; this.props.VersionLookup[Constants.RELEASE_TYPES[this.props.Release]][this.props.ReleaseVersion];
}; };
handleDependencyDownload = (url) => { handleDownloadDependency = url => {
this.setState({ this.props.downloadItem(this.extractFileNameFromURL(url), DownloadTypes.Dependency, url, this.onDownloadFileComplete);
DownloadActive: true,
DownloadingDependency: true,
DownloadName: this.extractFileNameFromURL(url),
}, ()=> {
this.sendRequest(Constants.IPC_Download_File, {
Filename: this.state.DownloadName,
URL: url,
});
});
}; };
handleReleaseDownload = () => { handleDownloadRelease = () => {
const selectedVersion = this.getSelectedVersion(); const selectedVersion = this.getSelectedVersion();
const fileName = selectedVersion + '.zip'; const fileName = selectedVersion + '.zip';
this.setState({ this.props.downloadItem(fileName, DownloadTypes.Release, this.props.LocationsLookup[selectedVersion].urls[0], this.onDownloadFileComplete);
DownloadActive: true,
DownloadingRelease: true,
DownloadName: fileName,
}, () => {
this.sendRequest(Constants.IPC_Download_File, {
Filename: this.state.DownloadName,
URL: this.props.LocationsLookup[selectedVersion].urls[0],
});
});
}; };
handleUIDownload = () => { handleDownloadUpgrade = () => {
this.setState({ const url = this.props.UpgradeData.urls[0];
DownloadActive: true, const name = this.props.Platform === 'win32' ? 'upgrade.exe' : this.extractFileNameFromURL(url);
DownloadingUpgrade: true, this.props.downloadItem(name, DownloadTypes.Upgrade, url, this.onDownloadFileComplete);
DownloadName: 'UI Upgrade',
}, ()=> {
const url = this.props.UpgradeData.urls[0];
this.sendRequest(Constants.IPC_Download_File, {
Filename: this.props.Platform === 'win32' ? 'upgrade.exe' : this.extractFileNameFromURL(url),
URL: url,
});
});
}; };
installDependency = data => { installDependency = data => {
@@ -193,13 +154,6 @@ class App extends IPCContainer {
URL: data.URL, URL: data.URL,
}); });
} }
this.setState({
DownloadActive: false,
DownloadProgress: 0.0,
DownloadingDependency: data.Success,
DownloadName: '',
});
}; };
installUpgrade = data => { installUpgrade = data => {
@@ -212,13 +166,6 @@ class App extends IPCContainer {
SkipVerification: !!data.SkipVerification, SkipVerification: !!data.SkipVerification,
Source: data.Destination, Source: data.Destination,
}); });
} else {
this.setState({
DownloadActive: false,
DownloadProgress: 0.0,
DownloadingUpgrade: false,
DownloadName: '',
});
} }
}; };
@@ -238,10 +185,8 @@ class App extends IPCContainer {
} }
} }
this.props.setReleaseUpgradeAvailable(upgradeAvailable); this.props.setReleaseUpgradeAvailable(upgradeAvailable);
this.props.setAllowDownload(true);
this.setState({ this.setState({
AllowDownload: true,
DownloadingDependency: false,
MissingDependencies: arg.data.Dependencies, MissingDependencies: arg.data.Dependencies,
InstalledVersion: installedVersion, InstalledVersion: installedVersion,
}); });
@@ -254,28 +199,17 @@ class App extends IPCContainer {
} }
}; };
onDownloadFileComplete = (event, arg) => { onDownloadFileComplete = (name, type, url, result) => {
if (this.state.DownloadingRelease) { switch (type) {
this.extractRelease(arg.data); case DownloadTypes.Dependency: this.installDependency(result); break;
} else if (this.state.DownloadingDependency) { case DownloadTypes.Release: this.extractRelease(result); break;
this.installDependency(arg.data); case DownloadTypes.Upgrade: this.installUpgrade(result); break;
} else if (this.state.DownloadingUpgrade) { default:
this.installUpgrade(arg.data); this.setErrorState('Unknown download type: ' + type, null, false);
} else { break;
this.setState({
DownloadActive: false,
DownloadProgress: 0.0,
DownloadName: '',
});
} }
}; };
onDownloadFileProgress = (event, arg) => {
this.setState({
DownloadProgress: arg.data.Progress,
});
};
onExtractReleaseComplete = (event, arg) => { onExtractReleaseComplete = (event, arg) => {
this.sendRequest(Constants.IPC_Delete_File, { this.sendRequest(Constants.IPC_Delete_File, {
FilePath: arg.data.Source, FilePath: arg.data.Source,
@@ -323,24 +257,18 @@ class App extends IPCContainer {
FilePath: arg.data.Source, FilePath: arg.data.Source,
}); });
this.setState({ if (!arg.data.Success) {
DownloadActive: false, this.setErrorState(arg.data.Error, () => {
DownloadProgress: 0.0, // TODO Prompt to verify
DownloadName: '', if (arg.data.AllowSkipVerification) {
}, () => { this.installUpgrade( {
if (!arg.data.Success) { SkipVerification: true,
this.setErrorState(arg.data.Error, () => { Source: arg.data.Source,
// TODO Prompt to verify Success: true,
if (arg.data.AllowSkipVerification) { });
this.installUpgrade( { }
SkipVerification: true, }, false);
Source: arg.data.Source, }
Success: true,
});
}
}, false);
}
});
}; };
saveState = () => { saveState = () => {
@@ -398,9 +326,9 @@ class App extends IPCContainer {
render() { render() {
const selectedVersion = this.getSelectedVersion(); const selectedVersion = this.getSelectedVersion();
const downloadEnabled = this.state.AllowDownload && const downloadEnabled = this.props.AllowDownload &&
!this.props.MountsBusy && !this.props.MountsBusy &&
!this.state.DownloadActive && !this.props.DownloadActive &&
(selectedVersion !== 'unavailable') && (selectedVersion !== 'unavailable') &&
(selectedVersion !== this.state.InstalledVersion); (selectedVersion !== this.state.InstalledVersion);
@@ -423,12 +351,12 @@ class App extends IPCContainer {
const showUpgrade = this.props.UpgradeAvailable && const showUpgrade = this.props.UpgradeAvailable &&
!this.state.DisplayError && !this.state.DisplayError &&
!showConfig && !showConfig &&
!this.state.DownloadActive && !this.props.DownloadActive &&
!this.props.UpgradeDismissed; !this.props.UpgradeDismissed;
const showDependencies = !showUpgrade && const showDependencies = !showUpgrade &&
missingDependencies && missingDependencies &&
!this.state.DownloadActive; !this.props.DownloadActive;
let errorDisplay = null; let errorDisplay = null;
if (this.state.DisplayError) { if (this.state.DisplayError) {
@@ -455,19 +383,18 @@ class App extends IPCContainer {
if (showDependencies) { if (showDependencies) {
dependencyDisplay = ( dependencyDisplay = (
<Modal> <Modal>
<DependencyList allowDownload={!this.state.DownloadingDependency} <DependencyList allowDownload={this.props.DownloadType !== DownloadTypes.Dependency}
dependencies={this.state.MissingDependencies} dependencies={this.state.MissingDependencies}
onDownload={this.handleDependencyDownload}/> onDownload={this.handleDownloadDependency}/>
</Modal> </Modal>
); );
} }
let downloadDisplay = null; let downloadDisplay = null;
if (this.state.DownloadActive) { if (this.props.DownloadActive) {
downloadDisplay = ( downloadDisplay = (
<Modal> <Modal>
<DownloadProgress display={this.state.DownloadName} <DownloadProgress/>
progress={this.state.DownloadProgress}/>
</Modal> </Modal>
); );
} }
@@ -476,7 +403,7 @@ class App extends IPCContainer {
if (showUpgrade) { if (showUpgrade) {
upgradeDisplay = ( upgradeDisplay = (
<Modal> <Modal>
<UpgradeUI upgrade={this.handleUIDownload}/> <UpgradeUI upgrade={this.handleDownloadUpgrade}/>
</Modal> </Modal>
); );
} }
@@ -487,11 +414,10 @@ class App extends IPCContainer {
mainContent.push(( mainContent.push((
<div key={'rvd_' + key++} <div key={'rvd_' + key++}
style={{height: '32%'}}> style={{height: '32%'}}>
<ReleaseVersionDisplay disabled={this.state.DownloadActive || this.state.ExtractActive || this.props.MountsBusy} <ReleaseVersionDisplay disabled={this.props.DownloadActive || this.state.ExtractActive || this.props.MountsBusy}
downloadClicked={this.handleReleaseDownload} downloadClicked={this.handleDownloadRelease}
downloadDisabled={!downloadEnabled} downloadDisabled={!downloadEnabled}
releaseExtracting={this.state.ExtractActive} releaseExtracting={this.state.ExtractActive}
saveState={this.saveState}
text={this.state.InstalledVersion + ' [' + this.props.AppPlatform + ']'}/> text={this.state.InstalledVersion + ' [' + this.props.AppPlatform + ']'}/>
</div> </div>
)); ));
@@ -503,7 +429,6 @@ class App extends IPCContainer {
allowSiaPrime={allowSiaPrime} allowSiaPrime={allowSiaPrime}
noConsoleSupported={noConsoleSupported} noConsoleSupported={noConsoleSupported}
errorHandler={this.setErrorState} errorHandler={this.setErrorState}
saveState={this.saveState}
version={this.state.InstalledVersion}/> version={this.state.InstalledVersion}/>
</div> </div>
)); ));
@@ -553,9 +478,12 @@ class App extends IPCContainer {
const mapStateToProps = state => { const mapStateToProps = state => {
return { return {
AllowDownload: state.download.AllowDownload,
AppPlatform: state.common.AppPlatform, AppPlatform: state.common.AppPlatform,
AppReady: state.common.AppReady, AppReady: state.common.AppReady,
DisplayConfiguration: state.mounts.DisplayConfiguration, DisplayConfiguration: state.mounts.DisplayConfiguration,
DownloadActive: state.download.DownloadActive,
DownloadType: state.download.DownloadType,
LocationsLookup: state.relver.LocationsLookup, LocationsLookup: state.relver.LocationsLookup,
MountsBusy: state.mounts.MountsBusy, MountsBusy: state.mounts.MountsBusy,
Platform: state.common.Platform, Platform: state.common.Platform,
@@ -573,8 +501,10 @@ const mapStateToProps = state => {
const mapDispatchToProps = dispatch => { const mapDispatchToProps = dispatch => {
return { return {
detectUIUpgrade: () => dispatch(detectUIUpgrade()), detectUIUpgrade: () => dispatch(detectUIUpgrade()),
downloadItem: (name, type, url, callback) => dispatch(downloadItem(name, type, url, callback)),
loadReleases: ()=> dispatch(loadReleases()), loadReleases: ()=> dispatch(loadReleases()),
setActiveRelease: (release, version) => dispatch(setActiveRelease(release, version)), setActiveRelease: (release, version) => dispatch(setActiveRelease(release, version)),
setAllowDownload: allow => dispatch(setAllowDownload(allow)),
setDismissUIUpgrade: dismiss => dispatch(setDismissUIUpgrade(dismiss)), setDismissUIUpgrade: dismiss => dispatch(setDismissUIUpgrade(dismiss)),
setProviderState: (provider, state) => dispatch(setProviderState(provider, state)), setProviderState: (provider, state) => dispatch(setProviderState(provider, state)),
setReleaseUpgradeAvailable: available => dispatch(setReleaseUpgradeAvailable(available)), setReleaseUpgradeAvailable: available => dispatch(setReleaseUpgradeAvailable(available)),

View File

@@ -1,16 +1,23 @@
import Box from '../UI/Box/Box'; import Box from '../UI/Box/Box';
import {connect} from 'react-redux';
import React from 'react'; import React from 'react';
import './DownloadProgress.css'; import './DownloadProgress.css';
export default props => { const mapStateToProps = state => {
return {
DownloadName: state.download.DownloadName,
DownloadProgress: state.download.DownloadProgress,
};
};
export default connect(mapStateToProps)(props => {
return ( return (
<Box dxStyle={{width: '380px', height: 'auto', padding: '5px'}}> <Box dxStyle={{width: '380px', height: 'auto', padding: '5px'}}>
<div style={{width: '100%', height: 'auto'}}> <div style={{width: '100%', height: 'auto'}}>
<h1 style={{width: '100%', textAlign: 'center'}}>{'Downloading ' + props.display}</h1> <h1 style={{width: '100%', textAlign: 'center'}}>{'Downloading ' + props.DownloadName}</h1>
</div> </div>
<progress max={100.0} id={'download_progress'} <progress max={100.0} id={'download_progress'}
style={{width: '100%'}} style={{width: '100%'}}
value={props.progress}/> value={props.DownloadProgress}/>
</Box>); </Box>);
}; });

View File

@@ -10,8 +10,8 @@ import {
setAllowMount, setAllowMount,
setAutoMountProcessed, setAutoMountProcessed,
setBusy, setBusy,
setMountState,
setMounted, setMounted,
setMountState,
setProviderState setProviderState
} from '../../redux/actions/mount_actions'; } from '../../redux/actions/mount_actions';
@@ -221,7 +221,6 @@ class MountItems extends IPCContainer {
RetrySeconds: 10, RetrySeconds: 10,
}; };
const mountState = { const mountState = {
...this.props.MountState[provider],
AllowMount: false, AllowMount: false,
Mounted: false, Mounted: false,
}; };

View File

@@ -0,0 +1,52 @@
import * as Constants from '../../constants';
import {createAction} from 'redux-starter-kit';
export const setAllowDownload = createAction('download/setAllowDownload');
export const SET_DOWNLOAD_BEGIN = 'download/setDownloadBegin';
export const setDownloadBegin = (name, type, url) => {
return {
type: SET_DOWNLOAD_BEGIN,
payload: {
name,
type,
url
}
};
};
export const setDownloadEnd = createAction('download/setDownloadEnd');
export const setDownloadProgress = createAction('download/setDownloadProgress');
export const downloadItem = (name, type, url, completedCallback) => {
return (dispatch, getState) => {
const state = getState();
if (!state.download.DownloadActive && state.download.AllowDownload) {
if (!process.versions.hasOwnProperty('electron')) {
const ipcRenderer = ((window && window.require) ? window.require('electron').ipcRenderer : null);
if (ipcRenderer) {
dispatch(setDownloadBegin(name, type, url));
const downloadFileProgress = (_, arg) => {
dispatch(setDownloadProgress(arg.data.Progress));
};
const downloadFileComplete = (_, arg) => {
this.ipcRenderer.removeListener(Constants.IPC_Download_File_Progress, downloadFileProgress);
this.ipcRenderer.removeListener(Constants.IPC_Download_File_Complete, downloadFileComplete);
completedCallback(name, type, url, arg.data);
dispatch(setDownloadEnd(arg.data));
};
ipcRenderer.on(Constants.IPC_Download_File_Progress, downloadFileProgress);
ipcRenderer.on(Constants.IPC_Download_File_Complete, downloadFileComplete);
ipcRenderer.send(Constants.IPC_Download_File, {
Filename: name,
URL: url,
});
}
}
}
};
};

View File

@@ -0,0 +1,49 @@
import {createReducer} from 'redux-starter-kit';
import {setAllowDownload, SET_DOWNLOAD_BEGIN, setDownloadEnd, setDownloadProgress} from '../actions/download_actions';
const defaultDownloadState = {
DownloadActive: false,
DownloadProgress: 0.0,
DownloadingDependency: false,
DownloadName: '',
DownloadType: null,
DownloadingRelease: false,
DownloadResult: null,
DownloadingUpgrade: false
};
export const downloadReducer = createReducer({
...defaultDownloadState,
AllowDownload: false,
}, {
[setAllowDownload]: (state, action) => {
return {
...state,
AllowDownload: action.payload,
};
},
[SET_DOWNLOAD_BEGIN]: (state, action) => {
return {
...state,
DownloadActive: true,
DownloadName: action.payload.name,
DownloadProgress: 0.0,
DownloadResult: null,
DownloadType: action.payload.type,
DownloadURL: action.payload.url,
}
},
[setDownloadEnd]: (state, action) => {
return {
...state,
...defaultDownloadState,
DownloadResult: action.payload,
};
},
[setDownloadProgress]: (state, action) => {
return {
...state,
DownloadProgress: action.payload,
}
}
});

View File

@@ -2,10 +2,12 @@ import {configureStore, getDefaultMiddleware} from 'redux-starter-kit';
import {createCommonReducer} from '../reducers/common_reducer'; import {createCommonReducer} from '../reducers/common_reducer';
import {mountReducer} from '../reducers/mount_reducer'; import {mountReducer} from '../reducers/mount_reducer';
import {releaseVersionReducer} from '../reducers/release_version_reducer'; import {releaseVersionReducer} from '../reducers/release_version_reducer';
import {downloadReducer} from '../reducers/download_reducer';
export default function createAppStore(platform, appPlatform, version) { export default function createAppStore(platform, appPlatform, version) {
const reducer = { const reducer = {
common: createCommonReducer(platform, appPlatform, version), common: createCommonReducer(platform, appPlatform, version),
download: downloadReducer,
mounts: mountReducer, mounts: mountReducer,
relver: releaseVersionReducer, relver: releaseVersionReducer,
}; };