mirror of https://github.com/vector-im/riot-web
allow a setting controller to validate and revert a change (asynchronously)
parent
612077125e
commit
308a6b419e
|
@ -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>
|
||||||
|
|
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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.
|
||||||
|
|
Loading…
Reference in New Issue