Refactoring

This commit is contained in:
Scott E. Graves
2018-12-08 23:36:43 -06:00
parent ff87b699ab
commit e95477f421
3 changed files with 107 additions and 125 deletions

View File

@@ -14,6 +14,14 @@ if (!process.versions.hasOwnProperty('electron')) {
class MountItems extends Component {
constructor(props) {
super(props);
for (const provider of Constants.PROVIDER_LIST) {
this.state[provider] = {
AllowMount: false,
DriveLetters: [],
Mounted: false,
PID: -1,
};
}
if (ipcRenderer) {
ipcRenderer.on(Constants.IPC_Detect_Mounts_Reply, this.onDetectMountsReply);
ipcRenderer.on(Constants.IPC_Mount_Drive_Reply, this.onMountDriveReply);
@@ -23,26 +31,7 @@ class MountItems extends Component {
}
}
state = {
Hyperspace: {
AllowMount: false,
DriveLetters: [],
Mounted: false,
PID: -1,
},
Sia: {
AllowMount: false,
DriveLetters: [],
Mounted: false,
PID: -1,
},
SiaPrime: {
AllowMount: false,
DriveLetters: [],
Mounted: false,
PID: -1,
},
};
state = {};
componentWillUnmount = () => {
if (ipcRenderer) {
@@ -105,34 +94,34 @@ class MountItems extends Component {
if (arg.data.Success) {
let state = {};
let mountsBusy = false;
for (const name in this.state) {
state[name] = {
...this.state[name],
for (const provider of Constants.PROVIDER_LIST) {
state[provider] = {
...this.state[provider],
AllowMount: true,
DriveLetters: (arg.data.DriveLetters[name]),
Mounted: (arg.data.Locations[name].length > 0),
PID: arg.data.PIDS[name],
DriveLetters: (arg.data.DriveLetters[provider]),
Mounted: (arg.data.Locations[provider].length > 0),
PID: arg.data.PIDS[provider],
};
mountsBusy = mountsBusy || state[name].Mounted;
mountsBusy = mountsBusy || state[provider].Mounted;
}
this.props.mountsBusy(mountsBusy);
this.setState(state, () => {
const updateMountLocation = (data, name) => {
const nameLower = name.toLowerCase();
let location = data.Locations[name];
const updateMountLocation = (data, provider) => {
const providerLower = provider.toLowerCase();
let location = data.Locations[provider];
if (location.length === 0) {
location = (this.props.platform === 'win32') ?
this.props[nameLower].MountLocation || data.DriveLetters[name][0] :
this.props[nameLower].MountLocation;
this.props[providerLower].MountLocation || data.DriveLetters[provider][0] :
this.props[providerLower].MountLocation;
}
if (location !== this.props[nameLower].MountLocation) {
this.props.changed(name, location);
if (location !== this.props[providerLower].MountLocation) {
this.props.changed(provider, location);
}
};
for (const name in this.state) {
updateMountLocation(arg.data, name);
for (const provider of Constants.PROVIDER_LIST) {
updateMountLocation(arg.data, provider);
}
this.performAutoMount();
@@ -166,41 +155,41 @@ class MountItems extends Component {
performAutoMount = ()=> {
if (this.props.processAutoMount) {
this.props.autoMountProcessed();
const processAutoMount = (name) => {
const nameLower = name.toLowerCase();
if (this.props[nameLower].AutoMount &&
!this.state[name].Mounted &&
(this.props[nameLower].MountLocation.length > 0)) {
this.handleMountUnMount(name, true, this.props[nameLower].MountLocation);
const processAutoMount = (provider) => {
const providerLower = provider.toLowerCase();
if (this.props[providerLower].AutoMount &&
!this.state[provider].Mounted &&
(this.props[providerLower].MountLocation.length > 0)) {
this.handleMountUnMount(provider, true, this.props[providerLower].MountLocation);
}
};
for (const name in this.state) {
processAutoMount(name);
for (const provider of Constants.PROVIDER_LIST) {
processAutoMount(provider);
}
}
};
render() {
let items = [];
for (const name in this.state) {
const nameLower = name.toLowerCase();
for (const provider of Constants.PROVIDER_LIST) {
const providerLower = provider.toLowerCase();
items.push((
<MountItem allowConfig={this.props.allowConfig}
allowMount={this.state[name].AllowMount}
autoMount={this.props[nameLower].AutoMount}
autoMountChanged={(e)=>this.props.autoMountChanged(name, e)}
autoRestart={this.props[nameLower].AutoRestart}
autoRestartChanged={(e)=>this.props.autoRestartChanged(name, e)}
changed={(e) => this.handleMountLocationChanged(name, e.target.value)}
allowMount={this.state[provider].AllowMount}
autoMount={this.props[providerLower].AutoMount}
autoMountChanged={(e)=>this.props.autoMountChanged(provider, e)}
autoRestart={this.props[providerLower].AutoRestart}
autoRestartChanged={(e)=>this.props.autoRestartChanged(provider, e)}
changed={(e) => this.handleMountLocationChanged(provider, e.target.value)}
clicked={this.handleMountUnMount}
configClicked={()=>this.props.configClicked(name)}
items={this.state[name].DriveLetters}
configClicked={()=>this.props.configClicked(provider)}
items={this.state[provider].DriveLetters}
key={'mi_' + items.length}
location={this.props[nameLower].MountLocation}
mounted={this.state[name].Mounted}
pid={this.state[name].PID}
location={this.props[providerLower].MountLocation}
mounted={this.state[provider].Mounted}
pid={this.state[provider].PID}
platform={this.props.platform}
title={name} />
title={provider} />
));
if (items.length !== this.state.length) {
items.push(<div key={'di_' + items.length}