fix toggling and thread view
- pass all comments to ThreadList - filter comments based on threadLevel === parentId - pass comment id to toggleCollapse()
This commit is contained in:
parent
e19bc431b9
commit
072550cd96
@ -22,7 +22,8 @@ async function runSeed() {
|
|||||||
|
|
||||||
await c1.addReply(2)
|
await c1.addReply(2)
|
||||||
|
|
||||||
await c2.addReplies([c3, c4, c5])
|
await c2.addReplies([c3, c4])
|
||||||
|
await c5.setParent(c4)
|
||||||
|
|
||||||
await c6.setParent(c1)
|
await c6.setParent(c1)
|
||||||
|
|
||||||
|
12
src/App.js
12
src/App.js
@ -5,18 +5,6 @@ import axios from 'axios'
|
|||||||
const API = 'http://localhost:5555'
|
const API = 'http://localhost:5555'
|
||||||
console.log('Using API at ' + API)
|
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 {
|
class App extends React.Component {
|
||||||
constructor() {
|
constructor() {
|
||||||
super()
|
super()
|
||||||
|
@ -2,7 +2,7 @@ import React from 'react'
|
|||||||
|
|
||||||
const CollapsedComment = props => {
|
const CollapsedComment = props => {
|
||||||
return (
|
return (
|
||||||
<li>
|
<li key={props.id}>
|
||||||
<div className="media-body ml-3 py-2 my-auto">
|
<div className="media-body ml-3 py-2 my-auto">
|
||||||
<img className="px-2" alt="profile" src="anon.jpeg" height={16} />
|
<img className="px-2" alt="profile" src="anon.jpeg" height={16} />
|
||||||
<a href="#user">username</a>
|
<a href="#user">username</a>
|
||||||
@ -14,7 +14,7 @@ const CollapsedComment = props => {
|
|||||||
color: 'blue',
|
color: 'blue',
|
||||||
cursor: 'pointer',
|
cursor: 'pointer',
|
||||||
}}
|
}}
|
||||||
onClick={props.toggleCollapse}
|
onClick={() => props.toggleCollapse(props.id)}
|
||||||
>
|
>
|
||||||
expand
|
expand
|
||||||
</span>
|
</span>
|
||||||
|
@ -1,26 +1,29 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
const ExpandedComment = props => {
|
const ExpandedComment = props => {
|
||||||
const { text } = props
|
const { text } = props
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<li>
|
<li key={props.id}>
|
||||||
<div className="media bg bg-dark">
|
<div className="media bg bg-dark">
|
||||||
<img alt="profile" src="anon.jpeg" height={64} />
|
<img alt="profile" src="anon.jpeg" height={64} />
|
||||||
<div className="media-body ml-3 my-auto">
|
<div className="media-body ml-3 my-auto">
|
||||||
<a href="#user">username</a>
|
<a href="#user">username</a>
|
||||||
<div>
|
<div>
|
||||||
<small>July 9 2019, 01:32:27 UTC</small>
|
<small>July 9 2019, 01:32:27 UTC</small>
|
||||||
<span
|
{props.replies.length > 0 ? (
|
||||||
className="a ml-3"
|
<span
|
||||||
style={{
|
className="a ml-3"
|
||||||
fontVariant: 'small-caps',
|
style={{
|
||||||
color: 'blue',
|
fontVariant: 'small-caps',
|
||||||
cursor: 'pointer',
|
color: 'blue',
|
||||||
}}
|
cursor: 'pointer',
|
||||||
onClick={props.toggleCollapse}
|
}}
|
||||||
>
|
onClick={() => props.toggleCollapse(props.id)}
|
||||||
collapse
|
>
|
||||||
</span>
|
collapse
|
||||||
|
</span>
|
||||||
|
) : (
|
||||||
|
''
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -3,9 +3,7 @@ import ExpandedComment from './ExpandedComment'
|
|||||||
import CollapsedComment from './CollapsedComment'
|
import CollapsedComment from './CollapsedComment'
|
||||||
|
|
||||||
const SingleComment = props => {
|
const SingleComment = props => {
|
||||||
console.log(props)
|
if (props.collapsed) return <CollapsedComment {...props} />
|
||||||
const { collapsed } = props
|
|
||||||
if (collapsed) return <CollapsedComment {...props} />
|
|
||||||
return <ExpandedComment {...props} />
|
return <ExpandedComment {...props} />
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,39 +5,57 @@ import uuid from 'uuid'
|
|||||||
class ThreadList extends React.Component {
|
class ThreadList extends React.Component {
|
||||||
constructor() {
|
constructor() {
|
||||||
super()
|
super()
|
||||||
this.state = { collapsed: false }
|
|
||||||
this.toggleCollapse = this.toggleCollapse.bind(this)
|
this.toggleCollapse = this.toggleCollapse.bind(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleCollapse() {
|
toggleCollapse(id) {
|
||||||
this.setState({ collapsed: !this.state.collapsed })
|
// select passed comment from the state, toggle it and put it all back
|
||||||
|
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)
|
||||||
|
this.setState({ comments: otherComments.concat(comment) })
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { comments } = this.props
|
const comments = this.props.comments.filter(
|
||||||
console.log(comments)
|
// only show comments for this parent / thread
|
||||||
|
c =>
|
||||||
|
(!c.parentId && !this.props.threadLevel) ||
|
||||||
|
c.parentId === this.props.threadLevel
|
||||||
|
)
|
||||||
|
console.log('showing comments for thread', this.props.threadLevel, comments)
|
||||||
return (
|
return (
|
||||||
<ul className="list-unstyled">
|
<ul className="list-unstyled">
|
||||||
{comments.map(comment => {
|
{comments.map(comment => {
|
||||||
return (
|
// TODO comments aren't sorted by time
|
||||||
<div className="ml-3">
|
if (!comment.replies.length > 0) {
|
||||||
<SingleComment
|
return (
|
||||||
threadLevel={this.props.threadLevel}
|
<div className="ml-3" key={uuid()}>
|
||||||
collapsed={this.state.collapsed}
|
<SingleComment
|
||||||
toggleCollapse={this.toggleCollapse}
|
threadLevel={this.props.threadLevel}
|
||||||
key={uuid()}
|
toggleCollapse={this.toggleCollapse}
|
||||||
{...comment}
|
{...comment}
|
||||||
/>
|
|
||||||
|
|
||||||
{comment.replies && (
|
|
||||||
<ThreadList
|
|
||||||
threadLevel={comment.parentId}
|
|
||||||
comments={comment.replies}
|
|
||||||
/>
|
/>
|
||||||
)}
|
</div>
|
||||||
</div>
|
)
|
||||||
)
|
} else {
|
||||||
|
return (
|
||||||
|
<div className="ml-3" key={uuid()}>
|
||||||
|
<SingleComment
|
||||||
|
threadLevel={this.props.threadLevel}
|
||||||
|
toggleCollapse={this.toggleCollapse}
|
||||||
|
{...comment}
|
||||||
|
/>
|
||||||
|
{!comment.collapsed && (
|
||||||
|
<ThreadList
|
||||||
|
threadLevel={comment.id}
|
||||||
|
comments={this.props.comments}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
})}
|
})}
|
||||||
</ul>
|
</ul>
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user