1
0

WIP recursion

This commit is contained in:
notnull 2019-07-13 07:56:55 -04:00
parent 56f915197d
commit 5576bf2251
13 changed files with 80 additions and 115 deletions

2
.gitignore vendored
View File

@ -21,3 +21,5 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*
old

View File

@ -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>
)
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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} />
}

View File

@ -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

View File

@ -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

View 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

View 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

View File

@ -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'