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',
display: props.displayCollapse || 'none',
}}
onClick={() => props.toggleCollapse(props.id)}
onClick={() => props.toggleCollapse(props.id, !props.collapsed)}
>
collapse
{props.collapsed ? 'expand' : 'collapse'}
</span>
) : (
''

View File

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

View File

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