diff --git a/.gitignore b/.gitignore
index 4d29575..56e27d6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -21,3 +21,5 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*
+
+old
diff --git a/server/db/seed.js b/server/db/seed.js
index 35380f8..73b8769 100755
--- a/server/db/seed.js
+++ b/server/db/seed.js
@@ -22,7 +22,8 @@ async function runSeed() {
await c1.addReply(2)
- await c2.addReplies([c3, c4, c5])
+ await c2.addReplies([c3, c4])
+ await c5.setParent(c4)
await c6.setParent(c1)
diff --git a/src/App.js b/src/App.js
index 7aed175..4629786 100644
--- a/src/App.js
+++ b/src/App.js
@@ -1,31 +1,10 @@
import React from 'react'
-import { NavbarView, CommentsView } from './Components'
+import { Navbar, Comments } from './Components'
import axios from 'axios'
const API = 'http://localhost:5555'
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: [] },
- { id: 5, text: 'c5', replies: [] },
- ],
- },
- {
- id: 6,
- text: 'c6',
- replies: [
- { id: 7, text: 'c7', replies: [] },
- { id: 8, text: 'c8', replies: [] },
- ],
- },
-]
-
class App extends React.Component {
constructor() {
super()
@@ -37,14 +16,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 +46,8 @@ class App extends React.Component {
renderApp() {
return (
-
-
+
+
)
}
diff --git a/src/Components/AllComments/AllCommentsView.js b/src/Components/AllComments/AllCommentsView.js
deleted file mode 100644
index 6b0947a..0000000
--- a/src/Components/AllComments/AllCommentsView.js
+++ /dev/null
@@ -1,12 +0,0 @@
-import React from 'react'
-import CommentList from './CommentList'
-import { Container } from 'react-bootstrap'
-
-const AllCommentsView = props => {
- return (
-
-
-
- )
-}
-export default AllCommentsView
diff --git a/src/Components/AllComments/CommentList.js b/src/Components/AllComments/CommentList.js
deleted file mode 100644
index 2026fc7..0000000
--- a/src/Components/AllComments/CommentList.js
+++ /dev/null
@@ -1,15 +0,0 @@
-import React from 'react'
-import CommentRow from './CommentRow'
-
-const CommentList = props => {
- const { comments } = props
- return (
-
- {comments.map(comment => (
-
- ))}
-
- )
-}
-
-export default CommentList
diff --git a/src/Components/AllComments/CommentRow.js b/src/Components/AllComments/CommentRow.js
deleted file mode 100644
index 003d810..0000000
--- a/src/Components/AllComments/CommentRow.js
+++ /dev/null
@@ -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 (
-
-
-
-
- )
- }
-}
-
-export default CommentRow
diff --git a/src/Components/AllComments/ExpandedComment.js b/src/Components/AllComments/ExpandedComment.js
deleted file mode 100644
index dfdd36f..0000000
--- a/src/Components/AllComments/ExpandedComment.js
+++ /dev/null
@@ -1,32 +0,0 @@
-import React from 'react'
-
-export default props => {
- const { text } = props
-
- return (
-
-
-

-
-
username
-
- July 9 2019, 01:32:27 UTC
-
- collapse
-
-
-
-
- {text}
- reply
-
- )
-}
diff --git a/src/Components/AllComments/Replies.js b/src/Components/AllComments/Replies.js
deleted file mode 100644
index 9622cd4..0000000
--- a/src/Components/AllComments/Replies.js
+++ /dev/null
@@ -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 (
-
-
-
- )
- }
-}
-
-export default Replies
diff --git a/src/Components/AllComments/SingleComment.js b/src/Components/AllComments/SingleComment.js
deleted file mode 100644
index 6b88047..0000000
--- a/src/Components/AllComments/SingleComment.js
+++ /dev/null
@@ -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
- return
-}
diff --git a/src/Components/AllComments/CollapsedComment.js b/src/Components/Comments/SingleComment/CollapsedComment.js
similarity index 76%
rename from src/Components/AllComments/CollapsedComment.js
rename to src/Components/Comments/SingleComment/CollapsedComment.js
index 8f02b22..cc712fb 100644
--- a/src/Components/AllComments/CollapsedComment.js
+++ b/src/Components/Comments/SingleComment/CollapsedComment.js
@@ -1,8 +1,8 @@
import React from 'react'
-export default props => {
+const CollapsedComment = props => {
return (
-
+
username
@@ -14,7 +14,7 @@ export default props => {
color: 'blue',
cursor: 'pointer',
}}
- onClick={props.toggleCollapse}
+ onClick={() => props.toggleCollapse(props.id)}
>
expand
@@ -22,3 +22,5 @@ export default props => {
)
}
+
+export default CollapsedComment
diff --git a/src/Components/Comments/SingleComment/ExpandedComment.js b/src/Components/Comments/SingleComment/ExpandedComment.js
new file mode 100644
index 0000000..5ad89cf
--- /dev/null
+++ b/src/Components/Comments/SingleComment/ExpandedComment.js
@@ -0,0 +1,47 @@
+import React from 'react'
+
+const ExpandedComment = props => {
+ return (
+
props.showCollapseLink(props.id, true)}
+ onMouseLeave={() => props.showCollapseLink(props.id, false)}
+ >
+
+

+
+
+
+
{props.createdAt}
+ {props.replies.length > 0 ? (
+
props.toggleCollapse(props.id, !props.collapsed)}
+ >
+ {props.collapsed ? 'expand' : 'collapse'}
+
+ ) : (
+ ''
+ )}
+
+ reply
+
+
+
+ {props.text}
+
+ )
+}
+
+export default ExpandedComment
diff --git a/src/Components/Comments/SingleComment/index.js b/src/Components/Comments/SingleComment/index.js
new file mode 100644
index 0000000..fd1ba90
--- /dev/null
+++ b/src/Components/Comments/SingleComment/index.js
@@ -0,0 +1,10 @@
+import React from 'react'
+import ExpandedComment from './ExpandedComment'
+import CollapsedComment from './CollapsedComment'
+
+const SingleComment = props => {
+ if (props.hidden) return
+ return
+}
+
+export default SingleComment
diff --git a/src/Components/Comments/index.js b/src/Components/Comments/index.js
new file mode 100644
index 0000000..11a3edf
--- /dev/null
+++ b/src/Components/Comments/index.js
@@ -0,0 +1,94 @@
+import React from 'react'
+import SingleComment from './SingleComment'
+import uuid from 'uuid'
+
+class ThreadList extends React.Component {
+ constructor() {
+ super()
+ 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, 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() {
+ const comments = this.props.comments.filter(
+ // only show comments for this parent / thread
+ 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 (
+
+ {comments.map(comment => {
+ // TODO comments aren't sorted by time
+ if (comment.replies && comment.replies.length === 0) {
+ return (
+
+
+
+ )
+ } else {
+ return (
+
+
+
+
+ )
+ }
+ })}
+
+ )
+ }
+}
+
+export default ThreadList
diff --git a/src/Components/Navbar/NavbarView.js b/src/Components/Navbar/index.js
similarity index 100%
rename from src/Components/Navbar/NavbarView.js
rename to src/Components/Navbar/index.js
diff --git a/src/Components/index.js b/src/Components/index.js
index 6f8970f..aa234d8 100644
--- a/src/Components/index.js
+++ b/src/Components/index.js
@@ -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'