show nested collapsed comments (#3)

This commit is contained in:
data 2019-07-13 20:38:09 +02:00
parent 3186a1f8ca
commit e8e3729f5f
3 changed files with 29 additions and 20 deletions

View File

@ -27,18 +27,14 @@ 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>
) : (
''
)}
<a
className="float-right ml-2"
href="#replies"
onClick={props.replyToComment}
>
<a className="float-right ml-2" href="#replies">
reply
</a>
</div>

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,28 @@ 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, collapsed) {
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'] = collapsed
if (!collapsed) {
// when parent was a reply in a previously collapsed thread
// and is to be expanded it will be marked hidden
// and needs to be unhidden
parent['hidden'] = false
}
this.setState({ comments: otherComments.concat(parent) })
parent.replies.map(comment => this.toggleReplies(comment.id, collapsed))
}
toggleReplies(id, collapsed) {
const otherComments = this.props.comments.filter(c => c.id !== id)
var comment = this.props.comments.find(c => c.id === id)
comment['hidden'] = collapsed
this.setState({ comments: otherComments.concat(comment) })
if (comment.replies) {
comment.replies.map(c => this.toggleReplies(c.id, collapsed))
}
}
render() {
@ -62,13 +77,11 @@ class ThreadList extends React.Component {
toggleCollapse={this.toggleCollapse}
{...comment}
/>
{!comment.collapsed && (
<ThreadList
threadLevel={comment.id}
showCollapseLink={this.showCollapseLink}
comments={this.props.comments}
/>
)}
</div>
)
}