forked from notnull/mwe-comments
WIP recursion
This commit is contained in:
parent
56f915197d
commit
5576bf2251
2
.gitignore
vendored
2
.gitignore
vendored
@ -21,3 +21,5 @@
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
|
||||
old
|
||||
|
31
src/App.js
31
src/App.js
@ -1,5 +1,5 @@
|
||||
import React from 'react'
|
||||
import { NavbarView, CommentsView } from './Components'
|
||||
import { Navbar, Comments } from './Components'
|
||||
import axios from 'axios'
|
||||
|
||||
const API = 'http://localhost:5555'
|
||||
@ -13,15 +13,6 @@ const comments = [
|
||||
replies: [
|
||||
{ id: 3, text: 'c3', replies: [] },
|
||||
{ id: 4, text: 'c4', replies: [] },
|
||||
{ id: 5, text: 'c5', replies: [] },
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 6,
|
||||
text: 'c6',
|
||||
replies: [
|
||||
{ id: 7, text: 'c7', replies: [] },
|
||||
{ id: 8, text: 'c8', replies: [] },
|
||||
],
|
||||
},
|
||||
]
|
||||
@ -37,14 +28,14 @@ class App extends React.Component {
|
||||
}
|
||||
|
||||
async fetchComments() {
|
||||
// try {
|
||||
// const { data } = await axios.get(API + '/api/comments')
|
||||
// return data
|
||||
// } catch (error) {
|
||||
// console.log(error)
|
||||
// this.setState({ error })
|
||||
// }
|
||||
return comments
|
||||
try {
|
||||
const { data } = await axios.get(API + '/api/comments')
|
||||
return data
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
this.setState({ error })
|
||||
}
|
||||
//return comments
|
||||
}
|
||||
|
||||
async fetchData() {
|
||||
@ -67,8 +58,8 @@ class App extends React.Component {
|
||||
renderApp() {
|
||||
return (
|
||||
<div>
|
||||
<NavbarView {...this.state} />
|
||||
<CommentsView {...this.state} />
|
||||
<Navbar {...this.state} />
|
||||
<Comments {...this.state} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
@ -1,12 +0,0 @@
|
||||
import React from 'react'
|
||||
import CommentList from './CommentList'
|
||||
import { Container } from 'react-bootstrap'
|
||||
|
||||
const AllCommentsView = props => {
|
||||
return (
|
||||
<Container className="mt-5">
|
||||
<CommentList {...props} />
|
||||
</Container>
|
||||
)
|
||||
}
|
||||
export default AllCommentsView
|
@ -1,15 +0,0 @@
|
||||
import React from 'react'
|
||||
import CommentRow from './CommentRow'
|
||||
|
||||
const CommentList = props => {
|
||||
const { comments } = props
|
||||
return (
|
||||
<ul className="list-unstyled">
|
||||
{comments.map(comment => (
|
||||
<CommentRow key={comment.id} {...comment} />
|
||||
))}
|
||||
</ul>
|
||||
)
|
||||
}
|
||||
|
||||
export default CommentList
|
@ -1,33 +0,0 @@
|
||||
import React from 'react'
|
||||
import SingleComment from './SingleComment'
|
||||
import Replies from './Replies'
|
||||
|
||||
//todo: rename to 'thread'
|
||||
class CommentRow extends React.Component {
|
||||
constructor() {
|
||||
super()
|
||||
this.state = {}
|
||||
this.toggleCollapse = this.toggleCollapse.bind(this)
|
||||
}
|
||||
|
||||
toggleCollapse() {
|
||||
this.setState({ collapsed: !this.state.collapsed })
|
||||
}
|
||||
|
||||
render() {
|
||||
const { text, replies } = this.props
|
||||
|
||||
return (
|
||||
<div>
|
||||
<SingleComment
|
||||
toggleCollapse={this.toggleCollapse}
|
||||
collapsed={this.state.collapsed}
|
||||
text={text}
|
||||
/>
|
||||
<Replies collapsed={this.state.collapsed} replies={replies} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export default CommentRow
|
@ -1,20 +0,0 @@
|
||||
import React from 'react'
|
||||
import CommentList from './CommentList'
|
||||
|
||||
class Replies extends React.Component {
|
||||
constructor() {
|
||||
super()
|
||||
this.state = {}
|
||||
}
|
||||
|
||||
render() {
|
||||
const { replies } = this.props
|
||||
return (
|
||||
<div className="ml-3">
|
||||
<CommentList comments={replies} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export default Replies
|
@ -1,10 +0,0 @@
|
||||
import React from 'react'
|
||||
import ExpandedComment from './ExpandedComment'
|
||||
import CollapsedComment from './CollapsedComment'
|
||||
|
||||
export default props => {
|
||||
const { text, collapsed, toggleCollapse } = props
|
||||
if (collapsed)
|
||||
return <CollapsedComment toggleCollapse={toggleCollapse} text={text} />
|
||||
return <ExpandedComment toggleCollapse={toggleCollapse} text={text} />
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
import React from 'react'
|
||||
|
||||
export default props => {
|
||||
const CollapsedComment = props => {
|
||||
return (
|
||||
<li>
|
||||
<div className="media-body ml-3 py-2 my-auto">
|
||||
@ -22,3 +22,5 @@ export default props => {
|
||||
</li>
|
||||
)
|
||||
}
|
||||
|
||||
export default CollapsedComment
|
@ -1,6 +1,5 @@
|
||||
import React from 'react'
|
||||
|
||||
export default props => {
|
||||
const ExpandedComment = props => {
|
||||
const { text } = props
|
||||
|
||||
return (
|
||||
@ -30,3 +29,5 @@ export default props => {
|
||||
</li>
|
||||
)
|
||||
}
|
||||
|
||||
export default ExpandedComment
|
12
src/Components/Comments/SingleComment/index.js
Normal file
12
src/Components/Comments/SingleComment/index.js
Normal file
@ -0,0 +1,12 @@
|
||||
import React from 'react'
|
||||
import ExpandedComment from './ExpandedComment'
|
||||
import CollapsedComment from './CollapsedComment'
|
||||
|
||||
const SingleComment = props => {
|
||||
console.log(props)
|
||||
const { collapsed } = props
|
||||
if (collapsed) return <CollapsedComment {...props} />
|
||||
return <ExpandedComment {...props} />
|
||||
}
|
||||
|
||||
export default SingleComment
|
47
src/Components/Comments/index.js
Normal file
47
src/Components/Comments/index.js
Normal file
@ -0,0 +1,47 @@
|
||||
import React from 'react'
|
||||
import SingleComment from './SingleComment'
|
||||
import uuid from 'uuid'
|
||||
|
||||
class ThreadList extends React.Component {
|
||||
constructor() {
|
||||
super()
|
||||
this.state = { collapsed: false }
|
||||
this.toggleCollapse = this.toggleCollapse.bind(this)
|
||||
}
|
||||
|
||||
toggleCollapse() {
|
||||
this.setState({ collapsed: !this.state.collapsed })
|
||||
}
|
||||
|
||||
render() {
|
||||
const { comments } = this.props
|
||||
console.log(comments)
|
||||
|
||||
return (
|
||||
<ul className="list-unstyled">
|
||||
{comments.map(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.parentId}
|
||||
comments={comment.replies}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</ul>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export default ThreadList
|
@ -1,2 +1,2 @@
|
||||
export { default as NavbarView } from './Navbar/NavbarView'
|
||||
export { default as CommentsView } from './AllComments/AllCommentsView'
|
||||
export { default as Navbar } from './Navbar'
|
||||
export { default as Comments } from './Comments'
|
||||
|
Loading…
Reference in New Issue
Block a user