view changes to ExpendedComments.js, show Collapse/Expand on hover

This commit is contained in:
data 2019-07-13 18:52:48 +02:00
parent 23144bc180
commit 3186a1f8ca
2 changed files with 56 additions and 24 deletions

View File

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

View File

@ -6,6 +6,16 @@ class ThreadList extends React.Component {
constructor() { constructor() {
super() super()
this.toggleCollapse = this.toggleCollapse.bind(this) 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) { toggleCollapse(id) {
@ -24,18 +34,23 @@ class ThreadList extends React.Component {
(!c.parentId && !this.props.threadLevel) || (!c.parentId && !this.props.threadLevel) ||
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) 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 // TODO comments aren't sorted by time
if (!comment.replies.length > 0) { if (comment.replies && comment.replies.length === 0) {
return ( return (
<div className="ml-3" key={uuid()}> <div className="ml-3" key={uuid()}>
<SingleComment <SingleComment
threadLevel={this.props.threadLevel} threadLevel={this.props.threadLevel}
showCollapseLink={this.showCollapseLink}
toggleCollapse={this.toggleCollapse} toggleCollapse={this.toggleCollapse}
{...comment} {...comment}
/>
</div> </div>
) )
} else { } else {
@ -43,12 +58,14 @@ class ThreadList extends React.Component {
<div className="ml-3" key={uuid()}> <div className="ml-3" key={uuid()}>
<SingleComment <SingleComment
threadLevel={this.props.threadLevel} threadLevel={this.props.threadLevel}
showCollapseLink={this.showCollapseLink}
toggleCollapse={this.toggleCollapse} toggleCollapse={this.toggleCollapse}
{...comment} {...comment}
/> />
{!comment.collapsed && ( {!comment.collapsed && (
<ThreadList <ThreadList
threadLevel={comment.id} threadLevel={comment.id}
showCollapseLink={this.showCollapseLink}
comments={this.props.comments} comments={this.props.comments}
/> />
)} )}