Delay URL preview saving until the save button is pressed

Signed-off-by: Travis Ralston <travpc@gmail.com>
This commit is contained in:
Travis Ralston 2017-11-04 16:32:13 -06:00
parent 358298e4ee
commit 4f1ad974fc
3 changed files with 37 additions and 13 deletions

View file

@ -27,22 +27,45 @@ module.exports = React.createClass({
label: React.PropTypes.string, // untranslated
onChange: React.PropTypes.func,
isExplicit: React.PropTypes.bool,
manualSave: React.PropTypes.bool,
// If group is supplied, then this will create a radio button instead.
group: React.PropTypes.string,
value: React.PropTypes.any, // the value for the radio button
},
getInitialState: function() {
return {
value: SettingsStore.getValueAt(
this.props.level,
this.props.name,
this.props.roomId,
this.props.isExplicit,
),
};
},
onChange: function(e) {
if (this.props.group && !e.target.checked) return;
const newState = this.props.group ? this.props.value : e.target.checked;
SettingsStore.setValue(this.props.name, this.props.roomId, this.props.level, newState);
if (!this.props.manualSave) this.save(newState);
else this.setState({ value: newState });
if (this.props.onChange) this.props.onChange(newState);
},
save: function(val = null) {
SettingsStore.setValue(this.props.name, this.props.roomId, this.props.level, val ? val : this.state.value);
},
render: function() {
const val = SettingsStore.getValueAt(this.props.level, this.props.name, this.props.roomId, this.props.isExplicit);
const value = this.props.manualSave ? this.state.value : SettingsStore.getValueAt(
this.props.level,
this.props.name,
this.props.roomId,
this.props.isExplicit,
);
const canChange = SettingsStore.canSetValue(this.props.name, this.props.roomId, this.props.level);
let label = this.props.label;
@ -54,7 +77,7 @@ module.exports = React.createClass({
let checkbox = (
<input id={id}
type="checkbox"
defaultChecked={val}
defaultChecked={value}
onChange={this.onChange}
disabled={!canChange}
/>
@ -65,7 +88,7 @@ module.exports = React.createClass({
type="radio"
name={this.props.group}
value={this.props.value}
checked={val === this.props.value}
checked={value === this.props.value}
onChange={this.onChange}
disabled={!canChange}
/>

View file

@ -29,8 +29,7 @@ module.exports = React.createClass({
},
saveSettings: function() {
// TODO: {Travis} move toggle logic here instead of being 'live'
return [];
return [this.refs.urlPreviewsRoom.save(), this.refs.urlPreviewsSelf.save()];
},
render: function() {
@ -48,7 +47,6 @@ module.exports = React.createClass({
);
}
// TODO: {Travis} This needs to be an explicit true/false for "room" only.
let previewsForRoom = null;
if (SettingsStore.canSetValue("urlPreviewsEnabled", roomId, "room")) {
previewsForRoom = (
@ -56,7 +54,9 @@ module.exports = React.createClass({
<SettingsFlag name="urlPreviewsEnabled"
level={SettingLevel.ROOM}
roomId={this.props.room.roomId}
isExplicit={true} />
isExplicit={true}
manualSave={true}
ref="urlPreviewsRoom" />
</label>
);
} else {
@ -71,6 +71,8 @@ module.exports = React.createClass({
<SettingsFlag name="urlPreviewsEnabled"
level={SettingLevel.ROOM_ACCOUNT}
roomId={this.props.room.roomId}
manualSave={true}
ref="urlPreviewsSelf"
/>
);

View file

@ -41,7 +41,6 @@ export const SettingLevel = {
DEFAULT: "default",
};
// Convert the settings to easier to manage objects for the handlers
const defaultSettings = {};
const featureNames = [];