Compare commits

...

4 Commits

Author SHA1 Message Date
data
03311238e8 don't rerender on hover for collapse/expand link 2019-07-14 15:31:09 +02:00
data
e212b52f9b better property names for collapsed threads 2019-07-14 14:46:22 +02:00
2ae7ad948f Added comment replies 2019-07-13 20:31:59 -04:00
402d5bff80 fixed comment style 2019-07-13 18:55:35 -04:00
9 changed files with 144 additions and 101 deletions

View File

@ -1,3 +1,15 @@
.replies { .replies {
margin-left: 1em; margin-left: 1em;
} }
a:hover {
text-decoration: none;
color: #007bff;
}
.comment-meta a {
display: none;
}
.comment-meta:hover a {
display: inline;
text-decoration: none;
color: #007bff;
}

View File

@ -10,3 +10,13 @@ router.get('/', async (req, res, next) => {
next(err) next(err)
} }
}) })
router.post('/', async (req, res, next) => {
try {
const comment = await Comment.create({ text: req.body.text })
await comment.setParent(req.body.parentId)
res.send(comment)
} catch (err) {
next(err)
}
})

View File

@ -1,31 +1,14 @@
const db = require('../db') const db = require('../db')
const { Comment } = require('./models') const { Comment } = require('./models')
const tc1 = { text: 'c1' } const tc1 = { text: 'FIRST' }
const tc2 = { text: 'c2' }
const tc3 = { text: 'c3' }
const tc4 = { text: 'c4' }
const tc5 = { text: 'c5' }
const tc6 = { text: 'c6' }
async function runSeed() { async function runSeed() {
await db.sync({ force: true }) await db.sync({ force: true })
console.log('db synced!') console.log('db synced!')
console.log('seeding...') console.log('seeding...')
try { try {
const c1 = await Comment.create(tc1) await Comment.create(tc1)
const c2 = await Comment.create(tc2)
const c3 = await Comment.create(tc3)
const c4 = await Comment.create(tc4)
const c5 = await Comment.create(tc5)
const c6 = await Comment.create(tc6)
await c1.addReply(2)
await c2.addReplies([c3, c4])
await c5.setParent(c4)
await c6.setParent(c1)
console.log('seeded successfully') console.log('seeded successfully')
} catch (err) { } catch (err) {

View File

@ -9,6 +9,7 @@ class App extends React.Component {
constructor() { constructor() {
super() super()
this.state = { loading: true } this.state = { loading: true }
this.submitComment = this.submitComment.bind(this)
} }
componentDidMount() { componentDidMount() {
@ -35,6 +36,11 @@ class App extends React.Component {
} }
} }
async submitComment(submittedData) {
await axios.post(API + '/api/comments', submittedData)
await this.fetchData()
}
renderLoading() { renderLoading() {
return <div>loading...</div> return <div>loading...</div>
} }
@ -47,7 +53,7 @@ class App extends React.Component {
return ( return (
<div> <div>
<Navbar {...this.state} /> <Navbar {...this.state} />
<Comments {...this.state} /> <Comments submitComment={this.submitComment} {...this.state} />
</div> </div>
) )
} }

View File

@ -7,17 +7,15 @@ const CollapsedComment = props => {
<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>
<small className="px-3">One day ago</small> <small className="px-3">One day ago</small>
<span <a
className="a" href="#toggle"
style={{ style={{
fontVariant: 'small-caps', fontVariant: 'small-caps',
color: 'blue',
cursor: 'pointer',
}} }}
onClick={() => props.toggleCollapse(props.id)} onClick={() => props.toggleCollapse(props.id)}
> >
expand expand
</span> </a>
</div> </div>
</li> </li>
) )

View File

@ -0,0 +1,35 @@
import React from 'react'
class CommentReply extends React.Component {
constructor(props) {
super(props)
this.state = { parentId: props.id, text: '' }
this.handleSubmit = this.handleSubmit.bind(this)
this.handleChange = this.handleChange.bind(this)
}
handleChange(e) {
this.setState({ [e.target.name]: e.target.value })
}
handleSubmit(e) {
e.preventDefault()
this.props.submitComment(this.state)
this.props.showReply(null)
}
render() {
return (
<form onSubmit={this.handleSubmit}>
<textarea
name="text"
onChange={this.handleChange}
value={this.state.text}
/>
<button className="btn btn-dark" onClick={this.handleSubmit}>
Submit
</button>
</form>
)
}
}
export default CommentReply

View File

@ -1,45 +1,54 @@
import React from 'react' import React from 'react'
import CommentReply from './CommentReply'
const ExpandedComment = props => { const ExpandedComment = props => {
return ( return (
<li <li key={props.id}>
className="" <div id="media-image" className="media bg bg-dark">
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} /> <img alt="profile" src="anon.jpeg" height={64} />
<div className="media-body ml-3 my-auto"> <div id="media-body" className="media-body ml-3 my-auto">
<a className="" href="#user"> <a href="#user">username</a>
username <div className="comment-meta text-white-50">
</a> <small>{props.createdAt}</small>
</div> {props.replies.length > 0 ? (
<div className="white"> <a
<small>{props.createdAt}</small> href="#toggle"
{props.replies.length > 0 ? ( className="ml-3"
<span style={{
className="a ml-3" fontVariant: 'small-caps',
style={{ }}
fontVariant: 'small-caps', onClick={() =>
color: 'blue', props.toggleCollapse(props.id, !props.collapsedThread)
cursor: 'pointer', }
display: props.displayCollapse || 'none', >
}} {props.collapsedThread ? 'expand' : 'collapse'}
onClick={() => props.toggleCollapse(props.id, !props.collapsed)} </a>
> ) : (
{props.collapsed ? 'expand' : 'collapse'} ''
</span> )}
) : ( </div>
''
)}
<a className="float-right ml-2" href="#replies">
reply
</a>
</div> </div>
</div> </div>
<div className="">{props.text}</div> <div className="">{props.text}</div>
{props.collapsed ? (
<div />
) : (
<a className="" href="#reply" onClick={() => props.showReply(props.id)}>
reply
</a>
)}
{props.id === props.activeReplyId ? (
<CommentReply
id={props.id}
submitComment={props.submitComment}
showReply={props.showReply}
{...props}
/>
) : (
<div />
)}
</li> </li>
) )
} }

View File

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

View File

@ -5,29 +5,20 @@ import uuid from 'uuid'
class ThreadList extends React.Component { class ThreadList extends React.Component {
constructor() { constructor() {
super() super()
this.state = { activeReplyId: null }
this.toggleCollapse = this.toggleCollapse.bind(this) this.toggleCollapse = this.toggleCollapse.bind(this)
this.showCollapseLink = this.showCollapseLink.bind(this) this.showReply = this.showReply.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) { toggleCollapse(id, collapsed) {
const otherComments = this.props.comments.filter(c => c.id !== id) const otherComments = this.props.comments.filter(c => c.id !== id)
var parent = this.props.comments.find(c => c.id === id) var parent = this.props.comments.find(c => c.id === id)
console.log('toggling', parent) parent['collapsedThread'] = collapsed
parent['collapsed'] = collapsed
if (!collapsed) { if (!collapsed) {
// when parent was a reply in a previously collapsed thread // when parent was a reply in a previously collapsed thread
// and is to be expanded it will be marked hidden // and is to be expanded it will be marked hidden
// and needs to be unhidden // and needs to be unhidden
parent['hidden'] = false parent['collapsed'] = false
} }
this.setState({ comments: otherComments.concat(parent) }) this.setState({ comments: otherComments.concat(parent) })
parent.replies.map(comment => this.toggleReplies(comment.id, collapsed)) parent.replies.map(comment => this.toggleReplies(comment.id, collapsed))
@ -35,11 +26,18 @@ class ThreadList extends React.Component {
toggleReplies(id, collapsed) { toggleReplies(id, collapsed) {
const otherComments = this.props.comments.filter(c => c.id !== id) const otherComments = this.props.comments.filter(c => c.id !== id)
var comment = this.props.comments.find(c => c.id === id) var comment = this.props.comments.find(c => c.id === id)
comment['hidden'] = collapsed comment['collapsed'] = collapsed
this.setState({ comments: otherComments.concat(comment) }) this.setState({ comments: otherComments.concat(comment) })
if (comment.replies) { comment.replies.map(c => this.toggleReplies(c.id, collapsed))
comment.replies.map(c => this.toggleReplies(c.id, collapsed)) }
}
handleChange(e) {
this.setState({ [e.target.name]: e.target.value })
}
showReply(id) {
console.log('show ', id, this.state.activeReplyId, this.state)
this.setState({ activeReplyId: id })
} }
render() { render() {
@ -52,39 +50,31 @@ class ThreadList extends React.Component {
if (comments.length === 0) { if (comments.length === 0) {
return '' 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 && 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} showReply={this.showReply}
{...comment} submitComment={this.props.submitComment}
/> activeReplyId={this.state.activeReplyId}
</div> {...comment}
) />
} else { {comment.replies && (
return (
<div className="ml-3" key={uuid()}>
<SingleComment
threadLevel={this.props.threadLevel}
showCollapseLink={this.showCollapseLink}
toggleCollapse={this.toggleCollapse}
{...comment}
/>
<ThreadList <ThreadList
threadLevel={comment.id} threadLevel={comment.id}
showCollapseLink={this.showCollapseLink}
comments={this.props.comments} comments={this.props.comments}
submitComment={this.props.submitComment}
/> />
</div> )}
) </div>
} )
})} })}
</ul> </ul>
) )