1
1
import React , { PropTypes } from 'react'
2
+ import { withRouter } from 'react-router'
2
3
import _ from 'lodash'
3
4
import {
4
5
THREAD_MESSAGES_PAGE_SIZE ,
@@ -36,7 +37,16 @@ class FeedView extends React.Component {
36
37
this . onNewCommentChange = this . onNewCommentChange . bind ( this )
37
38
this . onShowAllComments = this . onShowAllComments . bind ( this )
38
39
this . onAddNewComment = this . onAddNewComment . bind ( this )
39
- this . state = { feeds : [ ] , showAll : [ ] }
40
+ this . onLeave = this . onLeave . bind ( this )
41
+ this . isChanged = this . isChanged . bind ( this )
42
+ this . onNewPostChange = this . onNewPostChange . bind ( this )
43
+ this . state = { feeds : [ ] , showAll : [ ] , newPost : { } }
44
+ }
45
+
46
+ componentDidMount ( ) {
47
+ const routeLeaveHook = this . props . router . setRouteLeaveHook ( this . props . route , this . onLeave )
48
+ window . addEventListener ( 'beforeunload' , this . onLeave )
49
+ this . setState ( { routeLeaveHook } )
40
50
}
41
51
42
52
componentWillMount ( ) {
@@ -47,7 +57,28 @@ class FeedView extends React.Component {
47
57
this . init ( nextProps )
48
58
}
49
59
50
- mapFeed ( feed , showAll = false ) {
60
+ componentWillUnmount ( ) {
61
+ if ( this . state . routeLeaveHook ) {
62
+ this . state . routeLeaveHook ( )
63
+ }
64
+ window . removeEventListener ( 'beforeunload' , this . onLeave )
65
+ }
66
+
67
+ // Notify user if they navigate away while the form is modified.
68
+ onLeave ( e ) {
69
+ if ( this . isChanged ( ) ) {
70
+ return e . returnValue = 'You have uposted content. Are you sure you want to leave?'
71
+ }
72
+ }
73
+
74
+ isChanged ( ) {
75
+ const { newPost } = this . state
76
+ const hasComment = ! _ . isUndefined ( _ . find ( this . state . feeds , ( feed ) => feed . newComment && feed . newComment . length ) )
77
+ const hasThread = ( newPost . title && ! ! newPost . title . trim ( ) . length ) || ( newPost . content && ! ! newPost . content . trim ( ) . length )
78
+ return hasThread || hasComment
79
+ }
80
+
81
+ mapFeed ( feed , showAll = false , resetNewComment = false ) {
51
82
const { allMembers } = this . props
52
83
const item = _ . pick ( feed , [ 'id' , 'date' , 'read' , 'tag' , 'title' , 'totalPosts' , 'userId' , 'reference' , 'referenceId' , 'postIds' , 'isAddingComment' , 'isLoadingComments' , 'error' ] )
53
84
if ( isSystemUser ( item . userId ) ) {
@@ -72,20 +103,27 @@ class FeedView extends React.Component {
72
103
author : isSystemUser ( p . userId ) ? SYSTEM_USER : allMembers [ p . userId ]
73
104
}
74
105
}
106
+ const validPost = ( post ) => {
107
+ return post . type === 'post' && ( post . body && post . body . trim ( ) . length || ! isSystemUser ( post . userId ) )
108
+ }
75
109
if ( showAll ) {
76
110
// if we are showing all comments, just iterate through the entire array
77
111
_ . forEach ( _ . slice ( feed . posts , 1 ) , p => {
78
- p . type === 'post' ? item . comments . push ( _toComment ( p ) ) : item . totalComments --
112
+ validPost ( p ) ? item . comments . push ( _toComment ( p ) ) : item . totalComments --
79
113
} )
80
114
} else {
81
115
// otherwise iterate from right and add to the beginning of the array
82
116
_ . forEachRight ( _ . slice ( feed . posts , 1 ) , ( p ) => {
83
- p . type === 'post' ? item . comments . unshift ( _toComment ( p ) ) : item . totalComments --
117
+ validPost ( p ) ? item . comments . unshift ( _toComment ( p ) ) : item . totalComments --
84
118
if ( ! feed . showAll && item . comments . length === THREAD_MESSAGES_PAGE_SIZE )
85
119
return false
86
120
} )
87
121
}
88
122
item . newComment = ''
123
+ if ( ! resetNewComment ) {
124
+ const feedFromState = _ . find ( this . state . feeds , f => feed . id === f . id )
125
+ item . newComment = feedFromState ? feedFromState . newComment : ''
126
+ }
89
127
item . hasMoreComments = item . comments . length !== item . totalComments
90
128
return item
91
129
}
@@ -99,6 +137,12 @@ class FeedView extends React.Component {
99
137
} )
100
138
}
101
139
140
+ onNewPostChange ( title , content ) {
141
+ this . setState ( {
142
+ newPost : { title, content}
143
+ } )
144
+ }
145
+
102
146
onNewPost ( { title, content} ) {
103
147
const { project } = this . props
104
148
const newFeed = {
@@ -194,6 +238,7 @@ class FeedView extends React.Component {
194
238
isCreating = { isCreatingFeed }
195
239
hasError = { error }
196
240
heading = "NEW STATUS POST"
241
+ onNewPostChange = { this . onNewPostChange }
197
242
titlePlaceholder = "Share the latest project updates with the team"
198
243
/>
199
244
}
@@ -203,7 +248,7 @@ class FeedView extends React.Component {
203
248
}
204
249
}
205
250
const enhance = spinnerWhileLoading ( props => ! props . isLoading )
206
- const EnhancedFeedView = enhance ( FeedView )
251
+ const EnhancedFeedView = withRouter ( enhance ( FeedView ) )
207
252
208
253
209
254
class FeedContainer extends React . Component {
0 commit comments