diff --git a/src/AC/index.js b/src/AC/index.js
index e3a996a..8df36e5 100644
--- a/src/AC/index.js
+++ b/src/AC/index.js
@@ -1,6 +1,6 @@
import {
INCREMENT, DELETE_ARTICLE, CHANGE_DATE_RANGE, CHANGE_SELECTION, ADD_COMMENT, LOAD_ALL_ARTICLES, LOAD_ARTICLE,
- START, SUCCESS, FAIL
+ START, SUCCESS, FAIL, LOAD_COMMENTS
} from '../constants'
export function increment() {
@@ -72,4 +72,22 @@ export function loadArticle(id) {
}, 1000)
}
+}
+
+export function loadComments(id) {
+ return dispatch => {
+ dispatch({
+ type: LOAD_COMMENTS + START,
+ payload: { id }
+ })
+
+ setTimeout(() => {
+ fetch(`/api/comment?article=${id}`)
+ .then(res => res.json())
+ .then(response => dispatch({
+ type: LOAD_COMMENTS + SUCCESS,
+ payload: { id, response }
+ }))
+ }, 1000)
+ }
}
\ No newline at end of file
diff --git a/src/components/Comment.js b/src/components/Comment.js
index 4b123ce..3750c04 100644
--- a/src/components/Comment.js
+++ b/src/components/Comment.js
@@ -1,7 +1,8 @@
import React from 'react'
import PropTypes from 'prop-types'
import {connect} from 'react-redux'
-import {createCommentSelector} from '../selectors'
+import {commentSelector} from '../selectors'
+import { loadComments } from '../AC'
function Comment({comment}) {
return (
@@ -20,13 +21,8 @@ Comment.propTypes = {
}).isRequired
}
-const createMapStateToProps = () => {
- const commentSelector = createCommentSelector()
- return (state, ownProps) => {
- return {
- comment: commentSelector(state, ownProps)
- }
+export default connect((state, ownProps) => {
+ return {
+ comment: commentSelector(state, ownProps)
}
-}
-
-export default connect(createMapStateToProps)(Comment)
\ No newline at end of file
+})(Comment)
\ No newline at end of file
diff --git a/src/components/CommentList.js b/src/components/CommentList.js
index 3082942..e287396 100644
--- a/src/components/CommentList.js
+++ b/src/components/CommentList.js
@@ -1,8 +1,12 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
+import { connect } from 'react-redux'
import CommentForm from './CommentForm'
import Comment from './Comment'
import toggleOpen from '../decorators/toggleOpen'
+import { loadComments } from '../AC'
+import Loader from './common/Loader'
+import { commentsLoadingSelector, commentListSelector, commentsLoadedIDSelector } from '../selectors'
class CommentList extends Component {
static propTypes = {
@@ -12,6 +16,11 @@ class CommentList extends Component {
toggleOpen: PropTypes.func
}
+ componentWillReceiveProps({ isOpen, article, loadComments }) {
+ console.log(this.props.loadedID)
+ if (!this.props.isOpen && isOpen && (this.props.loadedID != article.id)) loadComments(article.id)
+ }
+
render() {
const {isOpen, toggleOpen} = this.props
const text = isOpen ? 'hide comments' : 'show comments'
@@ -24,12 +33,15 @@ class CommentList extends Component {
}
getBody() {
- const {article: { comments, id }, isOpen} = this.props
+ const { comments, article: { id }, isOpen, loading, loaded} = this.props
+
if (!isOpen) return null
+ if (loading) return