11import _ from 'lodash'
22import React from 'react'
3+ import { withRouter } from 'react-router'
34import { connect } from 'react-redux'
45import update from 'react-addons-update'
56import MessageList from '../../../components/MessageList/MessageList'
@@ -30,12 +31,27 @@ class MessagesView extends React.Component {
3031
3132 constructor ( props ) {
3233 super ( props )
33- this . state = { threads : [ ] , activeThreadId : null , showEmptyState : true , showAll : [ ] }
34+ this . state = {
35+ threads : [ ] ,
36+ activeThreadId : null ,
37+ showEmptyState : true ,
38+ showAll : [ ] ,
39+ newPost : { }
40+ }
3441 this . onThreadSelect = this . onThreadSelect . bind ( this )
3542 this . onShowAllComments = this . onShowAllComments . bind ( this )
3643 this . onAddNewMessage = this . onAddNewMessage . bind ( this )
3744 this . onNewMessageChange = this . onNewMessageChange . bind ( this )
3845 this . onNewThread = this . onNewThread . bind ( this )
46+ this . onLeave = this . onLeave . bind ( this )
47+ this . isChanged = this . isChanged . bind ( this )
48+ this . onNewPostChange = this . onNewPostChange . bind ( this )
49+ this . changeThread = this . changeThread . bind ( this )
50+ }
51+
52+ componentDidMount ( ) {
53+ this . props . router . setRouteLeaveHook ( this . props . route , this . onLeave )
54+ window . addEventListener ( 'beforeunload' , this . onLeave )
3955 }
4056
4157 componentWillMount ( ) {
@@ -46,6 +62,24 @@ class MessagesView extends React.Component {
4662 this . init ( nextProps )
4763 }
4864
65+ componentWillUnmount ( ) {
66+ window . removeEventListener ( 'beforeunload' , this . onLeave )
67+ }
68+
69+ // Notify user if they navigate away while the form is modified.
70+ onLeave ( e ) {
71+ if ( this . isChanged ( ) ) {
72+ return e . returnValue = 'You have uposted content. Are you sure you want to leave?'
73+ }
74+ }
75+
76+ isChanged ( ) {
77+ const { newPost } = this . state
78+ const hasMessage = ! _ . isUndefined ( _ . find ( this . state . threads , ( thread ) => thread . newMessage && thread . newMessage . length ) )
79+ const hasThread = ( newPost . title && ! ! newPost . title . trim ( ) . length ) || ( newPost . content && ! ! newPost . content . trim ( ) . length )
80+ return hasThread || hasMessage
81+ }
82+
4983 mapFeed ( feed , isActive , showAll = false ) {
5084 const { allMembers } = this . props
5185 const item = _ . pick ( feed , [ 'id' , 'date' , 'read' , 'tag' , 'title' , 'totalPosts' , 'userId' , 'reference' , 'referenceId' , 'postIds' , 'isAddingComment' , 'isLoadingComments' , 'error' ] )
@@ -137,15 +171,28 @@ class MessagesView extends React.Component {
137171 }
138172
139173 onThreadSelect ( thread ) {
174+ const unsavedContentMsg = this . onLeave ( { } )
175+ if ( unsavedContentMsg ) {
176+ const changeConfirmed = confirm ( unsavedContentMsg )
177+ if ( changeConfirmed ) {
178+ this . changeThread ( thread )
179+ }
180+ } else {
181+ this . changeThread ( thread )
182+ }
183+ }
184+
185+ changeThread ( thread ) {
140186 this . setState ( {
141187 isCreateNewMessage : false ,
188+ newPost : { } ,
142189 activeThreadId : thread . id ,
143190 threads : this . state . threads . map ( ( item ) => {
144191 if ( item . isActive ) {
145192 if ( item . id === thread . id ) {
146193 return item
147194 }
148- return { ...item , isActive : false , messages : item . messages . map ( ( msg ) => ( { ...msg , unread : false } ) ) }
195+ return { ...item , isActive : false , newMessage : '' , messages : item . messages . map ( ( msg ) => ( { ...msg , unread : false } ) ) }
149196 }
150197 if ( item . id === thread . id ) {
151198 return { ...item , isActive : true , unreadCount : 0 }
@@ -155,6 +202,12 @@ class MessagesView extends React.Component {
155202 } )
156203 }
157204
205+ onNewPostChange ( title , content ) {
206+ this . setState ( {
207+ newPost : { title, content}
208+ } )
209+ }
210+
158211 onNewMessageChange ( content ) {
159212 this . setState ( {
160213 threads : this . state . threads . map ( ( item ) => {
@@ -200,6 +253,7 @@ class MessagesView extends React.Component {
200253 < NewPost
201254 currentUser = { currentUser }
202255 onPost = { this . onNewThread }
256+ onNewPostChange = { this . onNewPostChange }
203257 isCreating = { isCreatingFeed }
204258 hasError = { error }
205259 heading = "New Discussion Post"
@@ -251,7 +305,7 @@ class MessagesView extends React.Component {
251305}
252306
253307const enhance = spinnerWhileLoading ( props => ! props . isLoading )
254- const EnhancedMessagesView = enhance ( MessagesView )
308+ const EnhancedMessagesView = withRouter ( enhance ( MessagesView ) )
255309
256310class MessagesContainer extends React . Component {
257311 constructor ( props ) {
0 commit comments