Light refactor of daemon settings processing

- Rename the daemon setter/getter methods to lbry.getDaemonSettings()
   and lbry.setDaemonSettings() to differentiate them from the new
   methods for client settings
 - Add lbry.setDaemonSetting(), for changing individual daemon settings
   easy to
 - Refactor Settings page to submit only changed settings instead of
   re-submitting all settings every time
This commit is contained in:
Alex Liebowitz 2016-08-22 14:13:41 -04:00
parent b75004c5f6
commit 0a2c0586d4
2 changed files with 25 additions and 27 deletions

View file

@ -90,13 +90,19 @@ lbry.getNewAddress = function(callback) {
lbry.call('get_new_address', {}, callback); lbry.call('get_new_address', {}, callback);
} }
lbry.getSettings = function(callback) { lbry.getDaemonSettings = function(callback) {
lbry.call('get_settings', {}, callback); lbry.call('get_settings', {}, callback);
}; }
lbry.setSettings = function(settings, callback) { lbry.setDaemonSettings = function(settings, callback) {
lbry.call('set_settings', settings, callback); lbry.call('set_settings', settings, callback);
}; }
lbry.setDaemonSetting = function(setting, value, callback) {
var setSettingsArgs = {};
setSettingsArgs[setting] = value;
lbry.call('set_settings', setSettingsArgs, callback)
}
lbry.getBalance = function(callback) lbry.getBalance = function(callback)
{ {

View file

@ -14,44 +14,36 @@ var settingsRadioOptionStyles = {
}; };
var SettingsPage = React.createClass({ var SettingsPage = React.createClass({
storeSetting: function(setting, val) {
var settings = Object.assign({}, this.state.settings);
settings[setting] = val;
this.setState({
'settings': settings
});
lbry.setSettings(settings);
},
onRunOnStartChange: function (event) { onRunOnStartChange: function (event) {
this.storeSetting('run_on_startup', event.target.checked); lbry.setDaemonSetting('run_on_startup', event.target.checked);
}, },
onShareDataChange: function (event) { onShareDataChange: function (event) {
this.storeSetting('upload_log', event.target.checked); lbry.setDaemonSetting('upload_log', event.target.checked);
}, },
onDownloadDirChange: function(event) { onDownloadDirChange: function(event) {
this.storeSetting('download_directory', event.target.value); lbry.setDaemonSetting('download_directory', event.target.value);
}, },
onMaxUploadPrefChange: function(isLimited) { onMaxUploadPrefChange: function(isLimited) {
if (!isLimited) { if (!isLimited) {
this.storeSetting('max_upload', 0.0); lbry.setDaemonSetting('max_upload', 0.0);
} }
this.setState({ this.setState({
isMaxUpload: isLimited isMaxUpload: isLimited
}); });
}, },
onMaxUploadFieldChange: function(event) { onMaxUploadFieldChange: function(event) {
this.storeSetting('max_upload', Number(event.target.value)); lbry.setDaemonSetting('max_upload', Number(event.target.value));
}, },
onMaxDownloadPrefChange: function(isLimited) { onMaxDownloadPrefChange: function(isLimited) {
if (!isLimited) { if (!isLimited) {
this.storeSetting('max_download', 0.0); lbry.setDaemonSetting('max_download', 0.0);
} }
this.setState({ this.setState({
isMaxDownload: isLimited isMaxDownload: isLimited
}); });
}, },
onMaxDownloadFieldChange: function(event) { onMaxDownloadFieldChange: function(event) {
this.storeSetting('max_download', Number(event.target.value)); lbry.setDaemonSetting('max_download', Number(event.target.value));
}, },
getInitialState: function() { getInitialState: function() {
return { return {
@ -62,16 +54,16 @@ var SettingsPage = React.createClass({
document.title = "Settings"; document.title = "Settings";
}, },
componentWillMount: function() { componentWillMount: function() {
lbry.getSettings(function(settings) { lbry.getDaemonSettings(function(settings) {
this.setState({ this.setState({
settings: settings, initDaemonSettings: settings,
isMaxUpload: settings.max_upload != 0, isMaxUpload: settings.max_upload != 0,
isMaxDownload: settings.max_download != 0 isMaxDownload: settings.max_download != 0
}); });
}.bind(this)); }.bind(this));
}, },
render: function() { render: function() {
if (!this.state.settings) { // If the settings aren't loaded yet, don't render anything. if (!this.state.initDaemonSettings) {
return null; return null;
} }
@ -80,13 +72,13 @@ var SettingsPage = React.createClass({
<section className="card"> <section className="card">
<h3>Run on Startup</h3> <h3>Run on Startup</h3>
<label style={settingsCheckBoxOptionStyles}> <label style={settingsCheckBoxOptionStyles}>
<input type="checkbox" onChange={this.onRunOnStartChange} defaultChecked={this.state.settings.run_on_startup} /> Run LBRY automatically when I start my computer <input type="checkbox" onChange={this.onRunOnStartChange} defaultChecked={this.state.initDaemonSettings.run_on_startup} /> Run LBRY automatically when I start my computer
</label> </label>
</section> </section>
<section className="card"> <section className="card">
<h3>Download Directory</h3> <h3>Download Directory</h3>
<div className="help">Where would you like the files you download from LBRY to be saved?</div> <div className="help">Where would you like the files you download from LBRY to be saved?</div>
<input style={downloadDirectoryFieldStyles} type="text" name="download_directory" defaultValue={this.state.settings.download_directory} onChange={this.onDownloadDirChange}/> <input style={downloadDirectoryFieldStyles} type="text" name="download_directory" defaultValue={this.state.initDaemonSettings.download_directory} onChange={this.onDownloadDirChange}/>
</section> </section>
<section className="card"> <section className="card">
<h3>Bandwidth Limits</h3> <h3>Bandwidth Limits</h3>
@ -97,7 +89,7 @@ var SettingsPage = React.createClass({
</label> </label>
<label style={settingsRadioOptionStyles}> <label style={settingsRadioOptionStyles}>
<input type="radio" name="max_upload_pref" onChange={this.onMaxUploadPrefChange.bind(this, true)} defaultChecked={this.state.isMaxUpload}/> { this.state.isMaxUpload ? 'Up to' : 'Choose limit...' } <input type="radio" name="max_upload_pref" onChange={this.onMaxUploadPrefChange.bind(this, true)} defaultChecked={this.state.isMaxUpload}/> { this.state.isMaxUpload ? 'Up to' : 'Choose limit...' }
<span className={ this.state.isMaxUpload ? '' : 'hidden'}> <input type="number" min="0" step=".5" defaultValue={this.state.settings.max_upload} style={settingsNumberFieldStyles} onChange={this.onMaxUploadFieldChange}/> MB/s</span> <span className={ this.state.isMaxUpload ? '' : 'hidden'}> <input type="number" min="0" step=".5" defaultValue={this.state.initDaemonSettings.max_upload} style={settingsNumberFieldStyles} onChange={this.onMaxUploadFieldChange}/> MB/s</span>
</label> </label>
</div> </div>
<div className="form-row"> <div className="form-row">
@ -107,14 +99,14 @@ var SettingsPage = React.createClass({
</label> </label>
<label style={settingsRadioOptionStyles}> <label style={settingsRadioOptionStyles}>
<input type="radio" name="max_download_pref" onChange={this.onMaxDownloadPrefChange.bind(this, true)} defaultChecked={this.state.isMaxDownload}/> { this.state.isMaxDownload ? 'Up to' : 'Choose limit...' } <input type="radio" name="max_download_pref" onChange={this.onMaxDownloadPrefChange.bind(this, true)} defaultChecked={this.state.isMaxDownload}/> { this.state.isMaxDownload ? 'Up to' : 'Choose limit...' }
<span className={ this.state.isMaxDownload ? '' : 'hidden'}> <input type="number" min="0" step=".5" defaultValue={this.state.settings.max_download} style={settingsNumberFieldStyles} onChange={this.onMaxDownloadFieldChange}/> MB/s</span> <span className={ this.state.isMaxDownload ? '' : 'hidden'}> <input type="number" min="0" step=".5" defaultValue={this.state.initDaemonSettings.max_download} style={settingsNumberFieldStyles} onChange={this.onMaxDownloadFieldChange}/> MB/s</span>
</label> </label>
</div> </div>
</section> </section>
<section className="card"> <section className="card">
<h3>Share Diagnostic Data</h3> <h3>Share Diagnostic Data</h3>
<label style={settingsCheckBoxOptionStyles}> <label style={settingsCheckBoxOptionStyles}>
<input type="checkbox" onChange={this.onShareDataChange} defaultChecked={this.state.settings.upload_log} /> Help make LBRY better by contributing diagnostic data about my usage <input type="checkbox" onChange={this.onShareDataChange} defaultChecked={this.state.initDaemonSettings.upload_log} /> Help make LBRY better by contributing diagnostic data about my usage
</label> </label>
</section> </section>
</main> </main>