Compare commits

..

No commits in common. "master" and "master" have entirely different histories.

6 changed files with 51 additions and 99 deletions

View File

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

View File

@ -5,6 +5,18 @@ 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 key={props.id}>
<li>
<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(props.id)}
onClick={props.toggleCollapse}
>
expand
</span>

View File

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

View File

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

View File

@ -5,86 +5,39 @@ 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)
}
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))
}
toggleCollapse() {
this.setState({ collapsed: !this.state.collapsed })
}
render() {
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)
const { comments } = this.props
console.log(comments)
return (
<ul className="list-unstyled">
{comments.map(comment => {
// 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>
)
} else {
return (
<div className="ml-3" key={uuid()}>
<SingleComment
threadLevel={this.props.threadLevel}
showCollapseLink={this.showCollapseLink}
toggleCollapse={this.toggleCollapse}
{...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.id}
showCollapseLink={this.showCollapseLink}
comments={this.props.comments}
threadLevel={comment.parentId}
comments={comment.replies}
/>
</div>
)
}
)}
</div>
)
})}
</ul>
)