show nested collapsed comments

This commit is contained in:
data 2019-07-13 20:24:49 +02:00
parent 77a714dac9
commit 0cf3c2e58b
3 changed files with 26 additions and 15 deletions

View File

@ -27,9 +27,9 @@ const ExpandedComment = props => {
cursor: 'pointer', cursor: 'pointer',
display: props.displayCollapse || 'none', display: props.displayCollapse || 'none',
}} }}
onClick={() => props.toggleCollapse(props.id)} onClick={() => props.toggleCollapse(props.id, !props.collapsed)}
> >
collapse {props.collapsed ? 'expand' : 'collapse'}
</span> </span>
) : ( ) : (
'' ''

View File

@ -3,7 +3,7 @@ import ExpandedComment from './ExpandedComment'
import CollapsedComment from './CollapsedComment' import CollapsedComment from './CollapsedComment'
const SingleComment = props => { const SingleComment = props => {
if (props.collapsed) return <CollapsedComment {...props} /> if (props.hidden) return <CollapsedComment {...props} />
return <ExpandedComment {...props} /> return <ExpandedComment {...props} />
} }

View File

@ -18,13 +18,26 @@ class ThreadList extends React.Component {
this.setState({ comments: otherComments.concat(comment) }) this.setState({ comments: otherComments.concat(comment) })
} }
toggleCollapse(id) { toggleCollapse(id, state) {
// select passed comment from the state, toggle it and put it all back
const otherComments = this.props.comments.filter(c => c.id !== id) const otherComments = this.props.comments.filter(c => c.id !== id)
const comment = this.props.comments.find(c => c.id === id) var parent = this.props.comments.find(c => c.id === id)
comment['collapsed'] = !comment.collapsed console.log('toggling', parent)
console.log('toggled: ', comment) parent['collapsed'] = state
if (!state) {
// unhide parent when unhiding thread
parent['hidden'] = false
}
this.setState({ comments: otherComments.concat(parent) })
parent.replies.map(comment => this.toggleReplies(comment.id, state))
}
toggleReplies(id, state) {
const otherComments = this.props.comments.filter(c => c.id !== id)
var comment = this.props.comments.find(c => c.id === id)
comment['hidden'] = state
this.setState({ comments: otherComments.concat(comment) }) this.setState({ comments: otherComments.concat(comment) })
if (comment.replies) {
comment.replies.map(c => this.toggleReplies(c.id, state))
}
} }
render() { render() {
@ -62,13 +75,11 @@ class ThreadList extends React.Component {
toggleCollapse={this.toggleCollapse} toggleCollapse={this.toggleCollapse}
{...comment} {...comment}
/> />
{!comment.collapsed && (
<ThreadList <ThreadList
threadLevel={comment.id} threadLevel={comment.id}
showCollapse={this.showCollapse} showCollapse={this.showCollapse}
comments={this.props.comments} comments={this.props.comments}
/> />
)}
</div> </div>
) )
} }