// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. // See License.txt for license information. import * as Client from '../../utils/client.jsx'; import SettingItemMin from '../setting_item_min.jsx'; import SettingItemMax from '../setting_item_max.jsx'; import Constants from '../../utils/constants.jsx'; import PreferenceStore from '../../stores/preference_store.jsx'; const PreReleaseFeatures = Constants.PRE_RELEASE_FEATURES; export default class AdvancedSettingsDisplay extends React.Component { constructor(props) { super(props); this.updateSection = this.updateSection.bind(this); this.updateSetting = this.updateSetting.bind(this); this.toggleFeature = this.toggleFeature.bind(this); this.saveEnabledFeatures = this.saveEnabledFeatures.bind(this); const preReleaseFeaturesKeys = Object.keys(PreReleaseFeatures); const advancedSettings = PreferenceStore.getPreferences(Constants.Preferences.CATEGORY_ADVANCED_SETTINGS); const settings = { send_on_ctrl_enter: PreferenceStore.getPreference( Constants.Preferences.CATEGORY_ADVANCED_SETTINGS, 'send_on_ctrl_enter', {value: 'false'} ).value }; let enabledFeatures = 0; advancedSettings.forEach((setting) => { preReleaseFeaturesKeys.forEach((key) => { const feature = PreReleaseFeatures[key]; if (setting.name === Constants.FeatureTogglePrefix + feature.label) { settings[setting.name] = setting.value; if (setting.value === 'true') { enabledFeatures++; } } }); }); this.state = {preReleaseFeatures: PreReleaseFeatures, settings, preReleaseFeaturesKeys, enabledFeatures}; } updateSetting(setting, value) { const settings = this.state.settings; settings[setting] = value; this.setState(settings); } toggleFeature(feature, checked) { const settings = this.state.settings; settings[Constants.FeatureTogglePrefix + feature] = String(checked); let enabledFeatures = 0; Object.keys(this.state.settings).forEach((setting) => { if (setting.lastIndexOf(Constants.FeatureTogglePrefix) === 0 && this.state.settings[setting] === 'true') { enabledFeatures++; } }); this.setState({settings, enabledFeatures}); } saveEnabledFeatures() { const features = []; Object.keys(this.state.settings).forEach((setting) => { if (setting.lastIndexOf(Constants.FeatureTogglePrefix) === 0) { features.push(setting); } }); this.handleSubmit(features); } handleSubmit(settings) { const preferences = []; (Array.isArray(settings) ? settings : [settings]).forEach((setting) => { preferences.push( PreferenceStore.setPreference( Constants.Preferences.CATEGORY_ADVANCED_SETTINGS, setting, String(this.state.settings[setting]) ) ); }); Client.savePreferences(preferences, () => { PreferenceStore.emitChange(); this.updateSection(''); }, (err) => { this.setState({serverError: err.message}); } ); } updateSection(section) { this.props.updateSection(section); } render() { const serverError = this.state.serverError || null; let ctrlSendSection; if (this.props.activeSection === 'advancedCtrlSend') { const ctrlSendActive = [ this.state.settings.send_on_ctrl_enter === 'true', this.state.settings.send_on_ctrl_enter === 'false' ]; const inputs = [