// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. // See License.txt for license information. import SettingItemMin from '../setting_item_min.jsx'; import SettingItemMax from '../setting_item_max.jsx'; import ManageLanguages from './manage_languages.jsx'; import ThemeSetting from './user_settings_theme.jsx'; import PreferenceStore from '../../stores/preference_store.jsx'; import * as Utils from '../../utils/utils.jsx'; import Constants from '../../utils/constants.jsx'; import {savePreferences} from '../../utils/client.jsx'; import {FormattedMessage} from 'mm-intl'; function getDisplayStateFromStores() { const militaryTime = PreferenceStore.getPreference(Constants.Preferences.CATEGORY_DISPLAY_SETTINGS, 'use_military_time', {value: 'false'}); const nameFormat = PreferenceStore.getPreference(Constants.Preferences.CATEGORY_DISPLAY_SETTINGS, 'name_format', {value: 'username'}); const selectedFont = PreferenceStore.getPreference(Constants.Preferences.CATEGORY_DISPLAY_SETTINGS, 'selected_font', {value: Constants.DEFAULT_FONT}); return { militaryTime: militaryTime.value, nameFormat: nameFormat.value, selectedFont: selectedFont.value }; } export default class UserSettingsDisplay extends React.Component { constructor(props) { super(props); this.handleSubmit = this.handleSubmit.bind(this); this.handleClockRadio = this.handleClockRadio.bind(this); this.handleNameRadio = this.handleNameRadio.bind(this); this.handleFont = this.handleFont.bind(this); this.updateSection = this.updateSection.bind(this); this.updateState = this.updateState.bind(this); this.deactivate = this.deactivate.bind(this); this.state = getDisplayStateFromStores(); } handleSubmit() { const timePreference = PreferenceStore.setPreference(Constants.Preferences.CATEGORY_DISPLAY_SETTINGS, 'use_military_time', this.state.militaryTime); const namePreference = PreferenceStore.setPreference(Constants.Preferences.CATEGORY_DISPLAY_SETTINGS, 'name_format', this.state.nameFormat); const fontPreference = PreferenceStore.setPreference(Constants.Preferences.CATEGORY_DISPLAY_SETTINGS, 'selected_font', this.state.selectedFont); savePreferences([timePreference, namePreference, fontPreference], () => { PreferenceStore.emitChange(); this.updateSection(''); }, (err) => { this.setState({serverError: err.message}); } ); } handleClockRadio(militaryTime) { this.setState({militaryTime}); } handleNameRadio(nameFormat) { this.setState({nameFormat}); } handleFont(selectedFont) { Utils.applyFont(selectedFont); this.setState({selectedFont}); } updateSection(section) { this.updateState(); this.props.updateSection(section); } updateState() { const newState = getDisplayStateFromStores(); if (!Utils.areObjectsEqual(newState, this.state)) { this.handleFont(newState.selectedFont); this.setState(newState); } } deactivate() { this.updateState(); } render() { const serverError = this.state.serverError || null; let clockSection; let nameFormatSection; let fontSection; let languagesSection; if (this.props.activeSection === 'clock') { const clockFormat = [false, false]; if (this.state.militaryTime === 'true') { clockFormat[1] = true; } else { clockFormat[0] = true; } const handleUpdateClockSection = (e) => { this.updateSection(''); e.preventDefault(); }; const inputs = [



]; clockSection = ( } inputs={inputs} submit={this.handleSubmit} server_error={serverError} updateSection={handleUpdateClockSection} /> ); } else { let describe; if (this.state.militaryTime === 'true') { describe = ( ); } else { describe = ( ); } const handleUpdateClockSection = () => { this.props.updateSection('clock'); }; clockSection = ( } describe={describe} updateSection={handleUpdateClockSection} /> ); } const showUsername = ( ); const showNickname = ( ); const showFullName = ( ); if (this.props.activeSection === 'name_format') { const nameFormat = [false, false, false]; if (this.state.nameFormat === 'nickname_full_name') { nameFormat[0] = true; } else if (this.state.nameFormat === 'full_name') { nameFormat[2] = true; } else { nameFormat[1] = true; } const inputs = [




]; nameFormatSection = ( } inputs={inputs} submit={this.handleSubmit} server_error={serverError} updateSection={(e) => { this.updateSection(''); e.preventDefault(); }} /> ); } else { let describe; if (this.state.nameFormat === 'username') { describe = ( ); } else if (this.state.nameFormat === 'full_name') { describe = ( ); } else { describe = ( ); } nameFormatSection = ( } describe={describe} updateSection={() => { this.props.updateSection('name_format'); }} /> ); } if (this.props.activeSection === 'font') { const options = []; Object.keys(Constants.FONTS).forEach((fontName, idx) => { const className = Constants.FONTS[fontName]; options.push( ); }); const inputs = [

]; fontSection = ( } inputs={inputs} submit={this.handleSubmit} server_error={serverError} updateSection={(e) => { this.updateSection(''); e.preventDefault(); }} /> ); } else { fontSection = ( } describe={this.state.selectedFont} updateSection={() => { this.props.updateSection('font'); }} /> ); } if (this.props.activeSection === 'languages') { languagesSection = ( { this.updateSection(''); e.preventDefault(); }} /> ); } else { var locale = 'English'; Utils.languages().forEach((l) => { if (l.value === this.props.user.locale) { locale = l.name; } }); languagesSection = ( } width='medium' describe={locale} updateSection={() => { this.updateSection('languages'); }} /> ); } return (

{fontSection}
{clockSection}
{nameFormatSection}
{languagesSection}
); } } UserSettingsDisplay.propTypes = { user: React.PropTypes.object, updateSection: React.PropTypes.func, updateTab: React.PropTypes.func, activeSection: React.PropTypes.string, closeModal: React.PropTypes.func.isRequired, collapseModal: React.PropTypes.func.isRequired, setRequireConfirm: React.PropTypes.func.isRequired, setEnforceFocus: React.PropTypes.func.isRequired };