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 c1.addReply(2)
await c2.addReplies([c3, c4]) await c2.addReplies([c3, c4, c5])
await c5.setParent(c4)
await c6.setParent(c1) await c6.setParent(c1)

View File

@ -5,6 +5,18 @@ 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()

View File

@ -2,7 +2,7 @@ import React from 'react'
const CollapsedComment = props => { const CollapsedComment = props => {
return ( return (
<li key={props.id}> <li>
<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(props.id)} onClick={props.toggleCollapse}
> >
expand expand
</span> </span>

View File

@ -1,45 +1,31 @@
import React from 'react' import React from 'react'
const ExpandedComment = props => { const ExpandedComment = props => {
const { text } = props
return ( 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"> <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 className="" href="#user"> <a href="#user">username</a>
username <div>
</a> <small>July 9 2019, 01:32:27 UTC</small>
</div>
<div className="white">
<small>{props.createdAt}</small>
{props.replies.length > 0 ? (
<span <span
className="a ml-3" className="a ml-3"
style={{ style={{
fontVariant: 'small-caps', fontVariant: 'small-caps',
color: 'blue', color: 'blue',
cursor: 'pointer', cursor: 'pointer',
display: props.displayCollapse || 'none',
}} }}
onClick={() => props.toggleCollapse(props.id, !props.collapsed)} onClick={props.toggleCollapse}
> >
{props.collapsed ? 'expand' : 'collapse'} collapse
</span> </span>
) : (
''
)}
<a className="float-right ml-2" href="#replies">
reply
</a>
</div> </div>
</div> </div>
<div className="">{props.text}</div> </div>
<div>{text}</div>
<a href="#replies">reply</a>
</li> </li>
) )
} }

View File

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

View File

@ -5,86 +5,39 @@ 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)
this.showCollapseLink = this.showCollapseLink.bind(this)
} }
showCollapseLink(id, collapsed) { toggleCollapse() {
this.setState({ displayCollapse: 'block' }) this.setState({ collapsed: !this.state.collapsed })
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() { render() {
const comments = this.props.comments.filter( const { comments } = this.props
// only show comments for this parent / thread console.log(comments)
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 ( return (
<ul className="list-unstyled"> <ul className="list-unstyled">
{comments.map(comment => { {comments.map(comment => {
// TODO comments aren't sorted by time
if (comment.replies && comment.replies.length === 0) {
return ( return (
<div className="ml-3" key={uuid()}> <div className="ml-3">
<SingleComment <SingleComment
threadLevel={this.props.threadLevel} threadLevel={this.props.threadLevel}
showCollapseLink={this.showCollapseLink} collapsed={this.state.collapsed}
toggleCollapse={this.toggleCollapse}
{...comment}
/>
</div>
)
} else {
return (
<div className="ml-3" key={uuid()}>
<SingleComment
threadLevel={this.props.threadLevel}
showCollapseLink={this.showCollapseLink}
toggleCollapse={this.toggleCollapse} toggleCollapse={this.toggleCollapse}
key={uuid()}
{...comment} {...comment}
/> />
{comment.replies && (
<ThreadList <ThreadList
threadLevel={comment.id} threadLevel={comment.parentId}
showCollapseLink={this.showCollapseLink} comments={comment.replies}
comments={this.props.comments}
/> />
)}
</div> </div>
) )
}
})} })}
</ul> </ul>
) )