@@ -113,7 +113,7 @@ class FeedView extends React.Component {
113113 return hasThread || hasComment
114114 }
115115
116- mapFeed ( feed , showAll = false , resetNewComment = false , prevProps ) {
116+ mapFeed ( feed , showAll = false , resetNewComment = false , prevProps , currentProps ) {
117117 const { allMembers, project, currentMemberRole } = this . props
118118 const item = _ . pick ( feed , [ 'id' , 'date' , 'read' , 'tag' , 'title' , 'totalPosts' , 'userId' , 'reference' , 'referenceId' , 'postIds' , 'isSavingTopic' , 'isDeletingTopic' , 'isAddingComment' , 'isLoadingComments' , 'error' ] )
119119 // Github issue##623, allow comments on all posts (including system posts)
@@ -189,7 +189,8 @@ class FeedView extends React.Component {
189189 item . newComment = ''
190190 if ( ! resetNewComment ) {
191191 const feedFromState = _ . find ( this . state . feeds , f => feed . id === f . id )
192- item . newComment = feedFromState ? feedFromState . newComment : ''
192+ const preservedNewComment = _ . find ( currentProps && currentProps . preservedNewComments , { feedId : feed . id } )
193+ item . newComment = ( feedFromState || preservedNewComment ) ? ( feedFromState || preservedNewComment ) . newComment : ''
193194 }
194195 item . hasMoreComments = item . comments . length !== item . totalComments
195196 // adds permalink for the feed
@@ -214,7 +215,7 @@ class FeedView extends React.Component {
214215 }
215216 // reset new comment if we were adding comment and there is no error in doing so
216217 const resetNewComment = prevFeed && prevFeed . isAddingComment && ! feed . isAddingComment && ! feed . error
217- return this . mapFeed ( feed , this . state . showAll . indexOf ( feed . id ) > - 1 , resetNewComment , prevProps )
218+ return this . mapFeed ( feed , this . state . showAll . indexOf ( feed . id ) > - 1 , resetNewComment , prevProps , props )
218219 } ) . filter ( item => item )
219220 } )
220221 }
@@ -247,6 +248,8 @@ class FeedView extends React.Component {
247248 return item
248249 } )
249250 } )
251+ // also save new comment to a place where we can keep it even during feed reloading
252+ this . props . updatePreservedNewComment ( feedId , content )
250253 }
251254
252255 onShowAllComments ( feedId ) {
@@ -489,6 +492,32 @@ const EnhancedFeedView = enhance(FeedView)
489492class FeedContainer extends React . Component {
490493 constructor ( props ) {
491494 super ( props )
495+
496+ this . state = {
497+ // as we loose `newComment` in the state inside `EnhancedFeedView` we have to keep it here
498+ // so we can restore the currently edited new comment, in case the feed has been reloaded
499+ preservedNewComments : [ ]
500+ }
501+
502+ this . updatePreservedNewComment = this . updatePreservedNewComment . bind ( this )
503+ }
504+
505+ updatePreservedNewComment ( feedId , newComment ) {
506+ const feedIndex = _ . findIndex ( this . state . preservedNewComments , { feedId } )
507+
508+ if ( feedIndex !== - 1 ) {
509+ this . setState ( {
510+ preservedNewComments : [
511+ ...this . state . preservedNewComments . slice ( 0 , feedIndex ) ,
512+ { feedId, newComment } ,
513+ ...this . state . preservedNewComments . slice ( feedIndex + 1 ) ,
514+ ]
515+ } )
516+ } else {
517+ this . setState ( {
518+ preservedNewComments : [ ...this . state . preservedNewComments , { feedId, newComment } ]
519+ } )
520+ }
492521 }
493522
494523 componentWillMount ( ) {
@@ -502,11 +531,14 @@ class FeedContainer extends React.Component {
502531 }
503532
504533 render ( ) {
534+ console . log ( 'preservedNewComments' , this . state . preservedNewComments )
505535 // Load only specified topics if topics input is available. Otherwise, load all feeds
506536 const { feeds, topics} = this . props
507537 const props = {
508538 ...this . props ,
509- feeds : topics ? feeds . filter ( f => _ . includes ( topics , f . id ) ) : feeds
539+ feeds : topics ? feeds . filter ( f => _ . includes ( topics , f . id ) ) : feeds ,
540+ preservedNewComments : this . state . preservedNewComments ,
541+ updatePreservedNewComment : this . updatePreservedNewComment ,
510542 }
511543 return < EnhancedFeedView { ...props } />
512544 }
0 commit comments