// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. // See License.txt for license information. import $ from 'jquery'; 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 * as AsyncClient from 'utils/async_client.jsx'; import PreferenceStore from 'stores/preference_store.jsx'; import UserStore from 'stores/user_store.jsx'; import * as Utils from 'utils/utils.jsx'; import * as I18n from 'i18n/i18n.jsx'; import Constants from 'utils/constants.jsx'; const Preferences = Constants.Preferences; import {FormattedMessage} from 'react-intl'; function getDisplayStateFromStores() { return { militaryTime: PreferenceStore.get(Preferences.CATEGORY_DISPLAY_SETTINGS, 'use_military_time', 'false'), nameFormat: PreferenceStore.get(Preferences.CATEGORY_DISPLAY_SETTINGS, 'name_format', 'username'), selectedFont: PreferenceStore.get(Preferences.CATEGORY_DISPLAY_SETTINGS, 'selected_font', Constants.DEFAULT_FONT), channelDisplayMode: PreferenceStore.get(Preferences.CATEGORY_DISPLAY_SETTINGS, Preferences.CHANNEL_DISPLAY_MODE, Preferences.CHANNEL_DISPLAY_MODE_DEFAULT) }; } import React from 'react'; 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 userId = UserStore.getCurrentId(); const timePreference = { user_id: userId, category: Preferences.CATEGORY_DISPLAY_SETTINGS, name: 'use_military_time', value: this.state.militaryTime }; const namePreference = { user_id: userId, category: Preferences.CATEGORY_DISPLAY_SETTINGS, name: 'name_format', value: this.state.nameFormat }; const fontPreference = { user_id: userId, category: Preferences.CATEGORY_DISPLAY_SETTINGS, name: 'selected_font', value: this.state.selectedFont }; const channelDisplayModePreference = { user_id: userId, category: Preferences.CATEGORY_DISPLAY_SETTINGS, name: Preferences.CHANNEL_DISPLAY_MODE, value: this.state.channelDisplayMode }; AsyncClient.savePreferences([timePreference, namePreference, fontPreference, channelDisplayModePreference], () => { this.updateSection(''); }, (err) => { this.setState({serverError: err.message}); } ); } handleClockRadio(militaryTime) { this.setState({militaryTime}); } handleNameRadio(nameFormat) { this.setState({nameFormat}); } handleChannelDisplayModeRadio(channelDisplayMode) { this.setState({channelDisplayMode}); } handleFont(selectedFont) { Utils.applyFont(selectedFont); this.setState({selectedFont}); } updateSection(section) { $('.settings-modal .modal-body').scrollTop(0).perfectScrollbar('update'); 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 channelDisplayModeSection; 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 = [