2015-10-08 12:27:09 -04:00
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
2015-06-14 23:53:32 -08:00
// See License.txt for license information.
2016-03-29 17:03:46 +05:00
import $ from 'jquery' ;
2016-03-14 08:50:46 -04:00
import { Modal } from 'react-bootstrap' ;
2015-11-19 21:12:56 -05:00
import SettingItemMin from './setting_item_min.jsx' ;
import SettingItemMax from './setting_item_max.jsx' ;
2015-06-14 23:53:32 -08:00
2016-07-19 10:16:44 -04:00
import Client from 'client/web_client.jsx' ;
2016-03-14 08:50:46 -04:00
import ChannelStore from 'stores/channel_store.jsx' ;
2015-06-14 23:53:32 -08:00
2016-03-14 08:50:46 -04:00
import { FormattedMessage } from 'react-intl' ;
import React from 'react' ;
2016-02-02 19:39:56 -03:00
2015-11-10 15:45:19 -05:00
export default class ChannelNotificationsModal extends React . Component {
2015-08-19 13:39:02 -07:00
constructor ( props ) {
super ( props ) ;
2015-08-31 11:20:31 -04:00
2015-08-19 13:39:02 -07:00
this . updateSection = this . updateSection . bind ( this ) ;
2015-08-31 11:20:31 -04:00
2015-09-23 11:38:08 -04:00
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 = {
2016-02-08 07:26:10 -05:00
activeSection : '' ,
notifyLevel : '' ,
unreadLevel : ''
2015-09-23 11:38:08 -04:00
} ;
2015-08-19 13:39:02 -07:00
}
2016-02-08 07:26:10 -05:00
updateSection ( section ) {
2016-08-18 19:09:33 +05:00
if ( $ ( '.section-max' ) . length ) {
$ ( '.settings-modal .modal-body' ) . scrollTop ( 0 ) . perfectScrollbar ( 'update' ) ;
}
2016-02-08 07:26:10 -05:00
this . setState ( { activeSection : section } ) ;
}
2015-12-01 11:59:35 -05:00
componentWillReceiveProps ( nextProps ) {
2015-12-01 14:53:42 -05:00
if ( ! this . props . show && nextProps . show ) {
2016-02-08 07:26:10 -05:00
this . setState ( {
notifyLevel : nextProps . channelMember . notify _props . desktop ,
unreadLevel : nextProps . channelMember . notify _props . mark _unread
} ) ;
2015-12-01 11:59:35 -05:00
}
2015-08-19 13:39:02 -07:00
}
2015-09-23 11:38:08 -04:00
handleSubmitNotifyLevel ( ) {
2016-02-08 07:26:10 -05:00
var channelId = this . props . channel . id ;
2015-08-19 13:39:02 -07:00
var notifyLevel = this . state . notifyLevel ;
2015-06-14 23:53:32 -08:00
2016-02-08 07:26:10 -05:00
if ( this . props . channelMember . notify _props . desktop === notifyLevel ) {
2015-09-23 16:58:50 -04:00
this . updateSection ( '' ) ;
return ;
}
2015-06-14 23:53:32 -08:00
var data = { } ;
2015-08-19 13:39:02 -07:00
data . channel _id = channelId ;
2016-02-08 07:26:10 -05:00
data . user _id = this . props . currentUser . id ;
2015-09-30 12:28:28 -04:00
data . desktop = notifyLevel ;
2015-06-14 23:53:32 -08:00
2016-02-08 07:26:10 -05:00
//TODO: This should be moved to event_helpers
2016-04-21 22:37:01 -07:00
Client . updateChannelNotifyProps ( data ,
2015-09-23 11:38:08 -04:00
( ) => {
2016-02-08 07:26:10 -05:00
// YUCK
2016-10-19 14:49:25 -04:00
var member = ChannelStore . getMyMember ( channelId ) ;
2015-09-30 12:28:28 -04:00
member . notify _props . desktop = notifyLevel ;
2016-10-19 14:49:25 -04:00
ChannelStore . storeMyChannelMember ( member ) ;
2015-08-19 13:39:02 -07:00
this . updateSection ( '' ) ;
2015-09-23 11:38:08 -04:00
} ,
( err ) => {
2015-08-19 13:39:02 -07:00
this . setState ( { serverError : err . message } ) ;
2015-09-23 11:38:08 -04:00
}
2015-06-14 23:53:32 -08:00
) ;
2015-08-19 13:39:02 -07:00
}
2015-09-23 11:38:08 -04:00
handleUpdateNotifyLevel ( notifyLevel ) {
this . setState ( { notifyLevel } ) ;
2015-08-19 13:39:02 -07:00
}
2015-09-23 11:38:08 -04:00
createNotifyLevelSection ( serverError ) {
2016-02-08 07:26:10 -05:00
// Get glabal user setting for notifications
const globalNotifyLevel = this . props . currentUser . notify _props . desktop ;
2015-09-22 12:00:34 -04:00
let globalNotifyLevelName ;
if ( globalNotifyLevel === 'all' ) {
2016-02-02 19:39:56 -03:00
globalNotifyLevelName = (
< FormattedMessage
id = 'channel_notifications.allActivity'
defaultMessage = 'For all activity'
/ >
) ;
2015-09-22 12:00:34 -04:00
} else if ( globalNotifyLevel === 'mention' ) {
2016-02-02 19:39:56 -03:00
globalNotifyLevelName = (
< FormattedMessage
id = 'channel_notifications.onlyMentions'
defaultMessage = 'Only for mentions'
/ >
) ;
2015-09-22 12:00:34 -04:00
} else {
2016-02-02 19:39:56 -03:00
globalNotifyLevelName = (
< FormattedMessage
id = 'channel_notifications.never'
defaultMessage = 'Never'
/ >
) ;
2015-09-22 12:00:34 -04:00
}
2016-02-02 19:39:56 -03:00
const sendDesktop = (
< FormattedMessage
id = 'channel_notifications.sendDesktop'
defaultMessage = 'Send desktop notifications'
/ >
) ;
2016-02-08 07:26:10 -05:00
const notificationLevel = this . state . notifyLevel ;
2015-06-22 14:23:59 -04:00
if ( this . state . activeSection === 'desktop' ) {
2016-02-08 07:26:10 -05:00
const notifyActive = [ false , false , false , false ] ;
if ( notificationLevel === 'default' ) {
2015-06-22 14:23:59 -04:00
notifyActive [ 0 ] = true ;
2016-02-08 07:26:10 -05:00
} else if ( notificationLevel === 'all' ) {
2015-09-22 12:00:34 -04:00
notifyActive [ 1 ] = true ;
2016-02-08 07:26:10 -05:00
} else if ( notificationLevel === 'mention' ) {
2015-06-22 14:23:59 -04:00
notifyActive [ 2 ] = true ;
2015-09-22 12:00:34 -04:00
} else {
notifyActive [ 3 ] = true ;
2015-06-22 14:23:59 -04:00
}
2015-08-31 11:20:31 -04:00
var inputs = [ ] ;
2015-06-22 14:23:59 -04:00
inputs . push (
2015-10-23 17:30:58 -07:00
< div key = 'channel-notification-level-radio' >
2015-08-19 13:39:02 -07:00
< div className = 'radio' >
2015-06-22 14:23:59 -04:00
< label >
2015-08-19 13:39:02 -07:00
< input
type = 'radio'
2016-06-27 08:21:24 -04:00
name = 'desktopNotificationLevel'
2015-08-19 13:39:02 -07:00
checked = { notifyActive [ 0 ] }
2015-09-23 11:38:08 -04:00
onChange = { this . handleUpdateNotifyLevel . bind ( this , 'default' ) }
2015-10-23 17:30:58 -07:00
/ >
2016-02-02 19:39:56 -03:00
< FormattedMessage
id = 'channel_notifications.globalDefault'
2016-02-23 09:57:39 -03:00
defaultMessage = 'Global default ({notifyLevel})'
2016-02-02 19:39:56 -03:00
values = { {
notifyLevel : ( globalNotifyLevelName )
} }
/ >
2015-06-22 14:23:59 -04:00
< / label >
< br / >
< / div >
2015-08-19 13:39:02 -07:00
< div className = 'radio' >
2015-06-22 14:23:59 -04:00
< label >
2015-08-19 13:39:02 -07:00
< input
type = 'radio'
2016-06-27 08:21:24 -04:00
name = 'desktopNotificationLevel'
2015-08-19 13:39:02 -07:00
checked = { notifyActive [ 1 ] }
2015-09-23 11:38:08 -04:00
onChange = { this . handleUpdateNotifyLevel . bind ( this , 'all' ) }
2015-10-23 17:30:58 -07:00
/ >
2016-02-22 08:31:10 -05:00
< FormattedMessage id = 'channel_notifications.allActivity' / >
2015-06-22 14:23:59 -04:00
< / label >
< br / >
< / div >
2015-08-19 13:39:02 -07:00
< div className = 'radio' >
2015-06-22 14:23:59 -04:00
< label >
2015-08-19 13:39:02 -07:00
< input
type = 'radio'
2016-06-27 08:21:24 -04:00
name = 'desktopNotificationLevel'
2015-08-19 13:39:02 -07:00
checked = { notifyActive [ 2 ] }
2015-09-23 11:38:08 -04:00
onChange = { this . handleUpdateNotifyLevel . bind ( this , 'mention' ) }
2015-10-23 17:30:58 -07:00
/ >
2016-02-22 08:31:10 -05:00
< FormattedMessage id = 'channel_notifications.onlyMentions' / >
2015-09-22 12:00:34 -04:00
< / label >
< br / >
< / div >
< div className = 'radio' >
< label >
< input
type = 'radio'
2016-06-27 08:21:24 -04:00
name = 'desktopNotificationLevel'
2015-09-22 12:00:34 -04:00
checked = { notifyActive [ 3 ] }
2015-09-23 11:38:08 -04:00
onChange = { this . handleUpdateNotifyLevel . bind ( this , 'none' ) }
2015-10-23 17:30:58 -07:00
/ >
2016-02-22 08:31:10 -05:00
< FormattedMessage id = 'channel_notifications.never' / >
2015-06-22 14:23:59 -04:00
< / label >
< / div >
< / div >
) ;
2016-02-08 07:26:10 -05:00
const handleUpdateSection = function updateSection ( e ) {
2015-08-31 11:20:31 -04:00
this . updateSection ( '' ) ;
2015-08-19 13:39:02 -07:00
e . preventDefault ( ) ;
2015-08-31 11:20:31 -04:00
} . bind ( this ) ;
2015-08-19 13:39:02 -07:00
2015-09-22 12:00:34 -04:00
const extraInfo = (
2015-09-24 10:58:06 -04:00
< span >
2016-02-02 19:39:56 -03:00
< FormattedMessage
id = 'channel_notifications.override'
defaultMessage = 'Selecting an option other than "Default" will override the global notification settings. Desktop notifications are available on Firefox, Safari, and Chrome.'
/ >
2015-09-24 10:58:06 -04:00
< / span >
) ;
2015-09-03 08:20:40 -07:00
2015-08-31 11:20:31 -04:00
return (
2015-06-22 14:23:59 -04:00
< SettingItemMax
2016-02-02 19:39:56 -03:00
title = { sendDesktop }
2015-06-22 14:23:59 -04:00
inputs = { inputs }
2015-09-23 11:38:08 -04:00
submit = { this . handleSubmitNotifyLevel }
2015-08-19 13:39:02 -07:00
server _error = { serverError }
updateSection = { handleUpdateSection }
2015-09-03 08:20:40 -07:00
extraInfo = { extraInfo }
2015-06-22 14:23:59 -04:00
/ >
) ;
2015-08-31 11:20:31 -04:00
}
2015-08-19 13:39:02 -07:00
2015-08-31 11:20:31 -04:00
var describe ;
2016-02-08 07:26:10 -05:00
if ( notificationLevel === 'default' ) {
2016-02-02 19:39:56 -03:00
describe = (
< FormattedMessage
id = 'channel_notifications.globalDefault'
values = { {
notifyLevel : ( globalNotifyLevelName )
} }
/ >
) ;
2016-02-08 07:26:10 -05:00
} else if ( notificationLevel === 'mention' ) {
2016-02-22 08:31:10 -05:00
describe = ( < FormattedMessage id = 'channel_notifications.onlyMentions' / > ) ;
2016-02-08 07:26:10 -05:00
} else if ( notificationLevel === 'all' ) {
2016-02-22 08:31:10 -05:00
describe = ( < FormattedMessage id = 'channel_notifications.allActivity' / > ) ;
2015-08-31 11:20:31 -04:00
} else {
2016-02-22 08:31:10 -05:00
describe = ( < FormattedMessage id = 'channel_notifications.never' / > ) ;
2015-06-22 14:23:59 -04:00
}
2015-08-31 11:20:31 -04:00
return (
< SettingItemMin
2016-02-02 19:39:56 -03:00
title = { sendDesktop }
2015-08-31 11:20:31 -04:00
describe = { describe }
2016-02-08 07:26:10 -05:00
updateSection = { ( ) => {
this . updateSection ( 'desktop' ) ;
} }
2015-08-31 11:20:31 -04:00
/ >
) ;
}
2015-09-23 11:38:08 -04:00
handleSubmitMarkUnreadLevel ( ) {
2016-02-08 07:26:10 -05:00
const channelId = this . props . channel . id ;
const markUnreadLevel = this . state . unreadLevel ;
2015-09-23 11:38:08 -04:00
2016-02-08 07:26:10 -05:00
if ( this . props . channelMember . notify _props . mark _unread === markUnreadLevel ) {
2015-09-23 16:58:50 -04:00
this . updateSection ( '' ) ;
return ;
}
2015-09-23 11:38:08 -04:00
const data = {
channel _id : channelId ,
2016-02-08 07:26:10 -05:00
user _id : this . props . currentUser . id ,
2015-09-30 11:08:36 -04:00
mark _unread : markUnreadLevel
2015-09-23 11:38:08 -04:00
} ;
2016-10-19 14:49:25 -04:00
//TODO: This should be fixed, moved to actions
2016-04-21 22:37:01 -07:00
Client . updateChannelNotifyProps ( data ,
2015-09-23 11:38:08 -04:00
( ) => {
2016-02-08 07:26:10 -05:00
// Yuck...
2016-10-19 14:49:25 -04:00
var member = ChannelStore . getMyMember ( channelId ) ;
2015-09-30 11:08:36 -04:00
member . notify _props . mark _unread = markUnreadLevel ;
2016-10-19 14:49:25 -04:00
ChannelStore . storeMyChannelMember ( member ) ;
2015-09-23 11:38:08 -04:00
this . updateSection ( '' ) ;
} ,
( err ) => {
this . setState ( { serverError : err . message } ) ;
2015-06-22 14:23:59 -04:00
}
2015-09-23 11:38:08 -04:00
) ;
}
2015-06-22 14:23:59 -04:00
2016-02-08 07:26:10 -05:00
handleUpdateMarkUnreadLevel ( unreadLevel ) {
this . setState ( { unreadLevel } ) ;
2015-09-23 11:38:08 -04:00
}
2015-08-31 11:20:31 -04:00
2015-09-23 11:38:08 -04:00
createMarkUnreadLevelSection ( serverError ) {
let content ;
2016-02-02 19:39:56 -03:00
const markUnread = (
< FormattedMessage
id = 'channel_notifications.markUnread'
defaultMessage = 'Mark Channel Unread'
/ >
) ;
2015-09-23 11:38:08 -04:00
if ( this . state . activeSection === 'markUnreadLevel' ) {
const inputs = [ (
2015-10-23 17:30:58 -07:00
< div key = 'channel-notification-unread-radio' >
2015-08-19 13:39:02 -07:00
< div className = 'radio' >
< label >
< input
type = 'radio'
2016-06-27 08:21:24 -04:00
name = 'markUnreadLevel'
2016-02-08 07:26:10 -05:00
checked = { this . state . unreadLevel === 'all' }
2015-09-23 11:38:08 -04:00
onChange = { this . handleUpdateMarkUnreadLevel . bind ( this , 'all' ) }
2015-10-23 17:30:58 -07:00
/ >
2016-05-12 07:50:53 -04:00
< FormattedMessage
id = 'channel_notifications.allUnread'
defaultMessage = 'For all unread messages'
/ >
2015-08-19 13:39:02 -07:00
< / label >
2016-02-22 08:31:10 -05:00
< br / >
2015-08-19 13:39:02 -07:00
< / div >
< div className = 'radio' >
< label >
< input
type = 'radio'
2016-06-27 08:21:24 -04:00
name = 'markUnreadLevel'
2016-02-08 07:26:10 -05:00
checked = { this . state . unreadLevel === 'mention' }
2015-09-23 11:38:08 -04:00
onChange = { this . handleUpdateMarkUnreadLevel . bind ( this , 'mention' ) }
2015-10-23 17:30:58 -07:00
/ >
2016-02-22 08:31:10 -05:00
< FormattedMessage id = 'channel_notifications.onlyMentions' / >
2015-08-19 13:39:02 -07:00
< / label >
2016-02-22 08:31:10 -05:00
< br / >
2015-06-22 14:23:59 -04:00
< / div >
< / div >
2015-09-23 11:38:08 -04:00
) ] ;
2015-06-22 14:23:59 -04:00
2015-09-23 11:38:08 -04:00
const handleUpdateSection = function handleUpdateSection ( e ) {
2015-08-31 11:20:31 -04:00
this . updateSection ( '' ) ;
2015-08-19 13:39:02 -07:00
e . preventDefault ( ) ;
2015-08-31 11:20:31 -04:00
} . bind ( this ) ;
2015-08-19 13:39:02 -07:00
2016-02-02 19:39:56 -03:00
const extraInfo = (
< span >
< FormattedMessage
id = 'channel_notifications.unreadInfo'
defaultMessage = 'The channel name is bolded in the sidebar when there are unread messages. Selecting "Only for mentions" will bold the channel only when you are mentioned.'
/ >
< / span >
) ;
2015-09-23 11:38:08 -04:00
content = (
2015-06-22 14:23:59 -04:00
< SettingItemMax
2016-02-02 19:39:56 -03:00
title = { markUnread }
2015-06-22 14:23:59 -04:00
inputs = { inputs }
2015-09-23 11:38:08 -04:00
submit = { this . handleSubmitMarkUnreadLevel }
2015-08-19 13:39:02 -07:00
server _error = { serverError }
updateSection = { handleUpdateSection }
2015-09-23 11:38:08 -04:00
extraInfo = { extraInfo }
2015-06-22 14:23:59 -04:00
/ >
) ;
2015-06-14 23:53:32 -08:00
} else {
2015-09-23 11:38:08 -04:00
let describe ;
2015-06-22 14:23:59 -04:00
2016-02-08 07:26:10 -05:00
if ( ! this . state . unreadLevel || this . state . unreadLevel === 'all' ) {
2016-02-02 19:39:56 -03:00
describe = (
< FormattedMessage
id = 'channel_notifications.allUnread'
defaultMessage = 'For all unread messages'
/ >
) ;
2015-09-23 11:38:08 -04:00
} else {
2016-02-22 08:31:10 -05:00
describe = ( < FormattedMessage id = 'channel_notifications.onlyMentions' / > ) ;
2015-09-23 11:38:08 -04:00
}
2015-08-19 13:39:02 -07:00
2015-09-23 11:38:08 -04:00
const handleUpdateSection = function handleUpdateSection ( e ) {
this . updateSection ( 'markUnreadLevel' ) ;
e . preventDefault ( ) ;
} . bind ( this ) ;
content = (
< SettingItemMin
2016-02-02 19:39:56 -03:00
title = { markUnread }
2015-09-23 11:38:08 -04:00
describe = { describe }
updateSection = { handleUpdateSection }
/ >
) ;
}
return content ;
2015-08-31 11:20:31 -04:00
}
2015-09-23 11:38:08 -04:00
2015-08-31 11:20:31 -04:00
render ( ) {
var serverError = null ;
if ( this . state . serverError ) {
serverError = < div className = 'form-group has-error' > < label className = 'control-label' > { this . state . serverError } < / label > < / div > ;
2015-06-14 23:53:32 -08:00
}
return (
2015-11-10 15:45:19 -05:00
< Modal
show = { this . props . show }
dialogClassName = 'settings-modal'
onHide = { this . props . onHide }
2015-08-19 13:39:02 -07:00
>
2015-11-10 15:45:19 -05:00
< Modal.Header closeButton = { true } >
2016-02-02 19:39:56 -03:00
< Modal.Title >
< FormattedMessage
id = 'channel_notifications.preferences'
defaultMessage = 'Notification Preferences for '
/ >
< span className = 'name' > { this . props . channel . display _name } < / span >
< / Modal.Title >
2015-11-10 15:45:19 -05:00
< / Modal.Header >
< Modal.Body >
< div className = 'settings-table' >
< div className = 'settings-content' >
< div
ref = 'wrapper'
className = 'user-settings'
2015-08-19 13:39:02 -07:00
>
2015-11-10 15:45:19 -05:00
< br / >
< div className = 'divider-dark first' / >
{ this . createNotifyLevelSection ( serverError ) }
< div className = 'divider-light' / >
{ this . createMarkUnreadLevelSection ( serverError ) }
< div className = 'divider-dark' / >
2015-06-14 23:53:32 -08:00
< / div >
< / div >
< / div >
2015-11-10 15:45:19 -05:00
{ serverError }
< / Modal.Body >
< / Modal >
2015-06-14 23:53:32 -08:00
) ;
}
2015-08-19 13:39:02 -07:00
}
2015-11-10 15:45:19 -05:00
ChannelNotificationsModal . propTypes = {
show : React . PropTypes . bool . isRequired ,
onHide : React . PropTypes . func . isRequired ,
2016-02-08 07:26:10 -05:00
channel : React . PropTypes . object . isRequired ,
channelMember : React . PropTypes . object . isRequired ,
currentUser : React . PropTypes . object . isRequired
2015-11-10 15:45:19 -05:00
} ;