@@ -19,8 +19,8 @@ limitations under the License.
1919var q = require ( "q" ) ;
2020var React = require ( 'react' ) ;
2121var classNames = require ( 'classnames' ) ;
22+ var RoomNotifs = require ( 'matrix-react-sdk/lib/RoomNotifs' ) ;
2223var MatrixClientPeg = require ( 'matrix-react-sdk/lib/MatrixClientPeg' ) ;
23- var dis = require ( 'matrix-react-sdk/lib/dispatcher' ) ;
2424
2525module . exports = React . createClass ( {
2626 displayName : 'NotificationStateContextMenu' ,
@@ -31,95 +31,86 @@ module.exports = React.createClass({
3131 onFinished : React . PropTypes . func ,
3232 } ,
3333
34- getInitialState : function ( ) {
35- var areNotifsMuted = false ;
36- var cli = MatrixClientPeg . get ( ) ;
37- if ( ! cli . isGuest ( ) ) {
38- var roomPushRule = cli . getRoomPushRule ( "global" , this . props . room . roomId ) ;
39- if ( roomPushRule ) {
40- if ( 0 <= roomPushRule . actions . indexOf ( "dont_notify" ) ) {
41- areNotifsMuted = true ;
42- }
43- }
34+ getInitialState ( ) {
35+ return {
36+ roomNotifState : RoomNotifs . getRoomNotifsState ( this . props . room . roomId ) ,
4437 }
38+ } ,
4539
46- return {
47- areNotifsMuted : areNotifsMuted ,
48- } ;
40+ componentWillMount : function ( ) {
41+ this . _unmounted = false ;
4942 } ,
5043
51- _save : function ( areNotifsMuted ) {
52- var self = this ;
44+ componentWillUnmount : function ( ) {
45+ this . _unmounted = true ;
46+ } ,
47+
48+ _save : function ( newState ) {
49+ const oldState = this . state . roomNotifState ;
5350 const roomId = this . props . room . roomId ;
5451 var cli = MatrixClientPeg . get ( ) ;
5552
56- if ( ! cli . isGuest ( ) ) {
57- // Wrapping this in a q promise, as setRoomMutePushRule can return
58- // a promise or a value
59- q ( cli . setRoomMutePushRule ( "global" , roomId , areNotifsMuted ) )
60- . then ( function ( ) {
61- self . setState ( { areNotifsMuted : areNotifsMuted } ) ;
62-
63- // delay slightly so that the user can see their state change
64- // before closing the menu
65- return q . delay ( 500 ) . then ( function ( ) {
66- // tell everyone that wants to know of the change in
67- // notification state
68- dis . dispatch ( {
69- action : 'notification_change' ,
70- roomId : self . props . room . roomId ,
71- areNotifsMuted : areNotifsMuted ,
72- } ) ;
73-
74- // Close the context menu
75- if ( self . props . onFinished ) {
76- self . props . onFinished ( ) ;
77- } ;
78- } ) ;
79- } ) . fail ( function ( error ) {
80- // TODO: some form of error notification to the user
81- // to inform them that their state change failed.
53+ if ( cli . isGuest ( ) ) return ;
54+
55+ this . setState ( {
56+ roomNotifState : newState ,
57+ } ) ;
58+ RoomNotifs . setRoomNotifsState ( this . props . room . roomId , newState ) . done ( ( ) => {
59+ // delay slightly so that the user can see their state change
60+ // before closing the menu
61+ return q . delay ( 500 ) . then ( ( ) => {
62+ if ( this . _unmounted ) return ;
63+ // Close the context menu
64+ if ( this . props . onFinished ) {
65+ this . props . onFinished ( ) ;
66+ } ;
8267 } ) ;
83- }
68+ } , ( error ) => {
69+ // TODO: some form of error notification to the user
70+ // to inform them that their state change failed.
71+ // For now we at least set the state back
72+ if ( this . _unmounted ) return ;
73+ this . setState ( {
74+ roomNotifState : oldState ,
75+ } ) ;
76+ } ) ;
8477 } ,
8578
8679 _onClickAlertMe : function ( ) {
87- // Placeholder
80+ this . _save ( RoomNotifs . ALL_MESSAGES_LOUD ) ;
8881 } ,
8982
9083 _onClickAllNotifs : function ( ) {
91- this . _save ( false ) ;
84+ this . _save ( RoomNotifs . ALL_MESSAGES ) ;
9285 } ,
9386
9487 _onClickMentions : function ( ) {
95- this . _save ( true ) ;
88+ this . _save ( RoomNotifs . MENTIONS_ONLY ) ;
9689 } ,
9790
9891 _onClickMute : function ( ) {
99- // Placeholder
92+ this . _save ( RoomNotifs . MUTE ) ;
10093 } ,
10194
10295 render : function ( ) {
103- var cli = MatrixClientPeg . get ( ) ;
104-
10596 var alertMeClasses = classNames ( {
10697 'mx_NotificationStateContextMenu_field' : true ,
107- 'mx_NotificationStateContextMenu_fieldDisabled ' : true ,
98+ 'mx_NotificationStateContextMenu_fieldSet ' : this . state . roomNotifState == RoomNotifs . ALL_MESSAGES_LOUD ,
10899 } ) ;
109100
110101 var allNotifsClasses = classNames ( {
111102 'mx_NotificationStateContextMenu_field' : true ,
112- 'mx_NotificationStateContextMenu_fieldSet' : ! this . state . areNotifsMuted ,
103+ 'mx_NotificationStateContextMenu_fieldSet' : this . state . roomNotifState == RoomNotifs . ALL_MESSAGES ,
113104 } ) ;
114105
115106 var mentionsClasses = classNames ( {
116107 'mx_NotificationStateContextMenu_field' : true ,
117- 'mx_NotificationStateContextMenu_fieldSet' : this . state . areNotifsMuted ,
108+ 'mx_NotificationStateContextMenu_fieldSet' : this . state . roomNotifState == RoomNotifs . MENTIONS_ONLY ,
118109 } ) ;
119110
120111 var muteNotifsClasses = classNames ( {
121112 'mx_NotificationStateContextMenu_field' : true ,
122- 'mx_NotificationStateContextMenu_fieldDisabled ' : true ,
113+ 'mx_NotificationStateContextMenu_fieldSet ' : this . state . roomNotifState == RoomNotifs . MUTE ,
123114 } ) ;
124115
125116 return (
0 commit comments