allow a setting controller to validate and revert a change (asynchronously)

pull/21833/head
Bruno Windels 2018-08-09 16:56:02 +02:00
parent 612077125e
commit 308a6b419e
3 changed files with 22 additions and 8 deletions

View File

@ -844,8 +844,8 @@ module.exports = React.createClass({
SettingsStore.getLabsFeatures().forEach((featureId) => { SettingsStore.getLabsFeatures().forEach((featureId) => {
// TODO: this ought to be a separate component so that we don't need // TODO: this ought to be a separate component so that we don't need
// to rebind the onChange each time we render // to rebind the onChange each time we render
const onChange = (e) => { const onChange = async (e) => {
SettingsStore.setFeatureEnabled(featureId, e.target.checked); await SettingsStore.setFeatureEnabled(featureId, e.target.checked);
this.forceUpdate(); this.forceUpdate();
}; };
@ -855,7 +855,7 @@ module.exports = React.createClass({
type="checkbox" type="checkbox"
id={featureId} id={featureId}
name={featureId} name={featureId}
defaultChecked={SettingsStore.isFeatureEnabled(featureId)} checked={SettingsStore.isFeatureEnabled(featureId)}
onChange={onChange} onChange={onChange}
/> />
<label htmlFor={featureId}>{ SettingsStore.getDisplayName(featureId) }</label> <label htmlFor={featureId}>{ SettingsStore.getDisplayName(featureId) }</label>

View File

@ -260,7 +260,7 @@ export default class SettingsStore {
* @param {*} value The new value of the setting, may be null. * @param {*} value The new value of the setting, may be null.
* @return {Promise} Resolves when the setting has been changed. * @return {Promise} Resolves when the setting has been changed.
*/ */
static setValue(settingName, roomId, level, value) { static async setValue(settingName, roomId, level, value) {
// Verify that the setting is actually a setting // Verify that the setting is actually a setting
if (!SETTINGS[settingName]) { if (!SETTINGS[settingName]) {
throw new Error("Setting '" + settingName + "' does not appear to be a setting."); throw new Error("Setting '" + settingName + "' does not appear to be a setting.");
@ -275,11 +275,21 @@ export default class SettingsStore {
throw new Error("User cannot set " + settingName + " at " + level + " in " + roomId); throw new Error("User cannot set " + settingName + " at " + level + " in " + roomId);
} }
return handler.setValue(settingName, roomId, value).then(() => { const controller = SETTINGS[settingName].controller;
const controller = SETTINGS[settingName].controller; if (controller) {
if (!controller) return; const changeAllowed = await controller.canChangeTo(level, roomId, value);
if (!changeAllowed) {
return false;
}
}
await handler.setValue(settingName, roomId, value);
if (controller) {
controller.onChange(level, roomId, value); controller.onChange(level, roomId, value);
}); }
return true;
} }
/** /**

View File

@ -39,6 +39,10 @@ export default class SettingController {
return null; // no override return null; // no override
} }
canChangeTo(level, roomId, newValue) {
return Promise.resolve(true);
}
/** /**
* Called when the setting value has been changed. * Called when the setting value has been changed.
* @param {string} level The level at which the setting has been modified. * @param {string} level The level at which the setting has been modified.