1
0

Compare commits

..

4 Commits

Author SHA1 Message Date
e2d72d00e0 Merge branch 'toggle' of tsr/mwe-comments into master 2019-07-13 15:29:22 -07:00
data
e8e3729f5f show nested collapsed comments (#3) 2019-07-13 22:40:08 +02:00
data
3186a1f8ca view changes to ExpendedComments.js, show Collapse/Expand on hover 2019-07-13 22:38:36 +02:00
data
23144bc180 fix toggling and thread view
- pass all comments to ThreadList
- filter comments based on threadLevel === parentId
- pass comment id to toggleCollapse()
2019-07-13 21:09:23 +02:00
6 changed files with 100 additions and 52 deletions

View File

@ -22,7 +22,8 @@ async function runSeed() {
await c1.addReply(2)
await c2.addReplies([c3, c4, c5])
await c2.addReplies([c3, c4])
await c5.setParent(c4)
await c6.setParent(c1)

View File

@ -5,18 +5,6 @@ import axios from 'axios'
const API = 'http://localhost:5555'
console.log('Using API at ' + API)
const comments = [
{ id: 1, text: 'c1', replies: [] },
{
id: 2,
text: 'c2',
replies: [
{ id: 3, text: 'c3', replies: [] },
{ id: 4, text: 'c4', replies: [] },
],
},
]
class App extends React.Component {
constructor() {
super()

View File

@ -2,7 +2,7 @@ import React from 'react'
const CollapsedComment = props => {
return (
<li>
<li key={props.id}>
<div className="media-body ml-3 py-2 my-auto">
<img className="px-2" alt="profile" src="anon.jpeg" height={16} />
<a href="#user">username</a>
@ -14,7 +14,7 @@ const CollapsedComment = props => {
color: 'blue',
cursor: 'pointer',
}}
onClick={props.toggleCollapse}
onClick={() => props.toggleCollapse(props.id)}
>
expand
</span>

View File

@ -1,31 +1,45 @@
import React from 'react'
const ExpandedComment = props => {
const { text } = props
const ExpandedComment = props => {
return (
<li>
<li
className=""
key={props.id}
onMouseEnter={() => props.showCollapseLink(props.id, true)}
onMouseLeave={() => props.showCollapseLink(props.id, false)}
>
<div className="media bg bg-dark">
<img alt="profile" src="anon.jpeg" height={64} />
<div className="media-body ml-3 my-auto">
<a href="#user">username</a>
<div>
<small>July 9 2019, 01:32:27 UTC</small>
<a className="" href="#user">
username
</a>
</div>
<div className="white">
<small>{props.createdAt}</small>
{props.replies.length > 0 ? (
<span
className="a ml-3"
style={{
fontVariant: 'small-caps',
color: 'blue',
cursor: 'pointer',
display: props.displayCollapse || 'none',
}}
onClick={props.toggleCollapse}
onClick={() => props.toggleCollapse(props.id, !props.collapsed)}
>
collapse
{props.collapsed ? 'expand' : 'collapse'}
</span>
</div>
) : (
''
)}
<a className="float-right ml-2" href="#replies">
reply
</a>
</div>
</div>
<div>{text}</div>
<a href="#replies">reply</a>
<div className="">{props.text}</div>
</li>
)
}

View File

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

View File

@ -5,39 +5,86 @@ import uuid from 'uuid'
class ThreadList extends React.Component {
constructor() {
super()
this.state = { collapsed: false }
this.toggleCollapse = this.toggleCollapse.bind(this)
this.showCollapseLink = this.showCollapseLink.bind(this)
}
toggleCollapse() {
this.setState({ collapsed: !this.state.collapsed })
showCollapseLink(id, collapsed) {
this.setState({ displayCollapse: 'block' })
const otherComments = this.props.comments.filter(c => c.id !== id)
const comment = this.props.comments.find(c => c.id === id)
comment['displayCollapse'] = collapsed
//console.log('displayed collapse for: ', comment)
this.setState({ comments: otherComments.concat(comment) })
}
toggleCollapse(id, collapsed) {
const otherComments = this.props.comments.filter(c => c.id !== id)
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() {
const { comments } = this.props
console.log(comments)
const comments = this.props.comments.filter(
// only show comments for this parent / thread
c =>
(!c.parentId && !this.props.threadLevel) ||
c.parentId === this.props.threadLevel
)
if (comments.length === 0) {
return ''
}
console.log('showing comments for thread', this.props.threadLevel, comments)
return (
<ul className="list-unstyled">
{comments.map(comment => {
return (
<div className="ml-3">
<SingleComment
threadLevel={this.props.threadLevel}
collapsed={this.state.collapsed}
toggleCollapse={this.toggleCollapse}
key={uuid()}
{...comment}
/>
{comment.replies && (
<ThreadList
threadLevel={comment.parentId}
comments={comment.replies}
// TODO comments aren't sorted by time
if (comment.replies && comment.replies.length === 0) {
return (
<div className="ml-3" key={uuid()}>
<SingleComment
threadLevel={this.props.threadLevel}
showCollapseLink={this.showCollapseLink}
toggleCollapse={this.toggleCollapse}
{...comment}
/>
)}
</div>
)
</div>
)
} else {
return (
<div className="ml-3" key={uuid()}>
<SingleComment
threadLevel={this.props.threadLevel}
showCollapseLink={this.showCollapseLink}
toggleCollapse={this.toggleCollapse}
{...comment}
/>
<ThreadList
threadLevel={comment.id}
showCollapseLink={this.showCollapseLink}
comments={this.props.comments}
/>
</div>
)
}
})}
</ul>
)