// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved. // See License.txt for license information. var SettingItemMin = require('./setting_item_min.jsx'); var SettingItemMax = require('./setting_item_max.jsx'); var Utils = require('../utils/utils.jsx'); var Client = require('../utils/client.jsx'); var UserStore = require('../stores/user_store.jsx'); var ChannelStore = require('../stores/channel_store.jsx'); export default class ChannelNotifications extends React.Component { constructor(props) { super(props); this.onListenerChange = this.onListenerChange.bind(this); this.updateSection = this.updateSection.bind(this); this.handleSubmitNotifyLevel = this.handleSubmitNotifyLevel.bind(this); this.handleUpdateNotifyLevel = this.handleUpdateNotifyLevel.bind(this); this.createNotifyLevelSection = this.createNotifyLevelSection.bind(this); this.handleSubmitMarkUnreadLevel = this.handleSubmitMarkUnreadLevel.bind(this); this.handleUpdateMarkUnreadLevel = this.handleUpdateMarkUnreadLevel.bind(this); this.createMarkUnreadLevelSection = this.createMarkUnreadLevelSection.bind(this); this.state = { notifyLevel: '', markUnreadLevel: '', title: '', channelId: '', activeSection: '' }; } componentDidMount() { ChannelStore.addChangeListener(this.onListenerChange); $(React.findDOMNode(this.refs.modal)).on('show.bs.modal', function showModal(e) { var button = e.relatedTarget; var channelId = button.getAttribute('data-channelid'); const member = ChannelStore.getMember(channelId); var notifyLevel = member.notify_level; var markUnreadLevel = member.mark_unread_level; this.setState({ notifyLevel, markUnreadLevel, title: button.getAttribute('data-title'), channelId: channelId }); }.bind(this)); } componentWillUnmount() { ChannelStore.removeChangeListener(this.onListenerChange); } onListenerChange() { if (!this.state.channelId) { return; } const member = ChannelStore.getMember(this.state.channelId); var notifyLevel = member.notify_level; var markUnreadLevel = member.mark_unread_level; var newState = this.state; newState.notifyLevel = notifyLevel; newState.markUnreadLevel = markUnreadLevel; if (!Utils.areStatesEqual(this.state, newState)) { this.setState(newState); } } updateSection(section) { this.setState({activeSection: section}); } handleSubmitNotifyLevel() { var channelId = this.state.channelId; var notifyLevel = this.state.notifyLevel; var data = {}; data.channel_id = channelId; data.user_id = UserStore.getCurrentId(); data.notify_level = notifyLevel; if (!data.notify_level || data.notify_level.length === 0) { return; } Client.updateNotifyLevel(data, () => { var member = ChannelStore.getMember(channelId); member.notify_level = notifyLevel; ChannelStore.setChannelMember(member); this.updateSection(''); }, (err) => { this.setState({serverError: err.message}); } ); } handleUpdateNotifyLevel(notifyLevel) { this.setState({notifyLevel}); React.findDOMNode(this.refs.modal).focus(); } createNotifyLevelSection(serverError) { var handleUpdateSection; const user = UserStore.getCurrentUser(); const globalNotifyLevel = user.notify_props.desktop; let globalNotifyLevelName; if (globalNotifyLevel === 'all') { globalNotifyLevelName = 'For all activity'; } else if (globalNotifyLevel === 'mention') { globalNotifyLevelName = 'Only for mentions'; } else { globalNotifyLevelName = 'Never'; } if (this.state.activeSection === 'desktop') { var notifyActive = [false, false, false, false]; if (this.state.notifyLevel === 'default') { notifyActive[0] = true; } else if (this.state.notifyLevel === 'all') { notifyActive[1] = true; } else if (this.state.notifyLevel === 'mention') { notifyActive[2] = true; } else { notifyActive[3] = true; } var inputs = []; inputs.push(