Skip to content

Commit 1866635

Browse files
authored
Merge pull request #4124 from maxceem/issue-4096
fix: preserve new comment during feed reloading
2 parents c415cfb + d2c6eac commit 1866635

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

src/components/ActionCard/AddComment.jsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default class AddComment extends React.Component {
2525
}
2626

2727
render() {
28-
const { className, avatarUrl, authorName, placeholder, isAdding, hasError, allMembers, projectMembers } = this.props
28+
const { className, avatarUrl, authorName, placeholder, isAdding, hasError, allMembers, projectMembers, defaultContent } = this.props
2929

3030
return (
3131
<RichTextArea ref="richTextArea"
@@ -41,6 +41,7 @@ export default class AddComment extends React.Component {
4141
allMembers={allMembers}
4242
projectMembers={projectMembers}
4343
canUploadAttachment
44+
content={defaultContent}
4445
/>
4546
)
4647
}
@@ -57,5 +58,9 @@ AddComment.propTypes = {
5758
hasError: PropTypes.bool,
5859
isAdding: PropTypes.bool,
5960
allMembers: PropTypes.object,
60-
projectMembers: PropTypes.object
61+
projectMembers: PropTypes.object,
62+
/**
63+
* Default value for comment content
64+
*/
65+
defaultContent: PropTypes.string,
6166
}

src/components/Feed/Feed.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class Feed extends React.Component {
104104
allowComments, comments, children, onNewCommentChange, onAddNewComment, isAddingComment, onSaveMessageChange,
105105
onEditMessage, onSaveMessage, isSavingTopic, onDeleteMessage, onDeleteTopic, isDeletingTopic, error, allMembers,
106106
onEnterFullscreenClick, onExitFullscreenClick, isFullScreen, commentId, projectMembers, commentAnchorPrefix, tag,
107-
inTopicDrawer, onDrawerClose
107+
inTopicDrawer, onDrawerClose, newComment
108108
} = this.props
109109
const { editTopicMode, headerHeight } = this.state
110110

@@ -222,6 +222,7 @@ class Feed extends React.Component {
222222
commentId={commentId}
223223
error={error}
224224
commentAnchorPrefix={commentAnchorPrefix}
225+
newComment={newComment}
225226
/>
226227
{children}
227228
{isDeletingTopic &&

src/components/Feed/FeedComments.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class FeedComments extends React.Component {
180180
const {
181181
currentUser, onLoadMoreComments, isLoadingComments, hasMoreComments, onAddNewComment,
182182
onNewCommentChange, error, avatarUrl, isAddingComment, allowComments, onSaveMessage, onDeleteMessage, allMembers,
183-
totalComments, isFullScreen, headerHeight, projectMembers, commentAnchorPrefix
183+
totalComments, isFullScreen, headerHeight, projectMembers, commentAnchorPrefix, newComment
184184
} = this.props
185185
let { comments } = this.props
186186
comments = _.sortBy(comments, 'createdBy')
@@ -383,6 +383,7 @@ class FeedComments extends React.Component {
383383
hasError={error}
384384
allMembers={allMembers}
385385
projectMembers={projectMembers}
386+
defaultContent={newComment}
386387
/>
387388
</div>
388389
}

src/projects/detail/containers/FeedContainer.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
489492
class 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

Comments
 (0)