Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/AC/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
INCREMENT, DELETE_ARTICLE, CHANGE_DATE_RANGE, CHANGE_SELECTION, ADD_COMMENT, LOAD_ALL_ARTICLES, LOAD_ARTICLE,
START, SUCCESS, FAIL
LOAD_COMMENTS, START, SUCCESS, FAIL
} from '../constants'

export function increment() {
Expand Down Expand Up @@ -70,6 +70,25 @@ export function loadArticle(id) {
payload: { id, response }
}))

}, 1000)
}, 300)
}
}

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

}, 300)
}
}
15 changes: 5 additions & 10 deletions src/components/Comment.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import PropTypes from 'prop-types'
import {connect} from 'react-redux'
import {createCommentSelector} from '../selectors'
import {commentSelector} from '../selectors'

function Comment({comment}) {
return (
Expand All @@ -20,13 +20,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)
})(Comment)
23 changes: 20 additions & 3 deletions src/components/CommentList.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import React, {Component} from 'react'
import { connect } from 'react-redux'
import PropTypes from 'prop-types'
import CommentForm from './CommentForm'
import Comment from './Comment'
import toggleOpen from '../decorators/toggleOpen'
import Loader from './common/Loader'
import {loadComments} from '../AC'
import { commentsLoadingSelector, commentListSelector, commentsCacheSelector, commentsLoadedIdSelector } from '../selectors'

class CommentList extends Component {
static propTypes = {
Expand All @@ -12,6 +16,11 @@ class CommentList extends Component {
toggleOpen: PropTypes.func
}

componentWillReceiveProps({ isOpen, article, loadComments }) {
//console.log('-', this.props);
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'
Expand All @@ -24,12 +33,13 @@ class CommentList extends Component {
}

getBody() {
const {article: { comments, id }, isOpen} = this.props
const { comments, article: { id }, isOpen, loading} = this.props
if (!isOpen) return null
if (loading) return <Loader />

const body = comments.length ? (
<ul>
{comments.map(id => <li key = {id}><Comment id = {id} /></li>)}
{comments.map(item => <li key = {item.id}><Comment id = {item.id} /></li>)}
</ul>
) : <h3>No comments yet</h3>

Expand All @@ -43,4 +53,11 @@ class CommentList extends Component {
}


export default toggleOpen(CommentList)
export default connect(state => {
return {
loading: commentsLoadingSelector(state),
comments: commentListSelector(state),
//cache: commentsCacheSelector(state),
loadedId: commentsLoadedIdSelector(state)
}
}, {loadComments})(toggleOpen(CommentList))
1 change: 1 addition & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export const INCREMENT = 'INCREMENT'
export const DELETE_ARTICLE = 'DELETE_ARTICLE'
export const LOAD_ALL_ARTICLES = 'LOAD_ALL_ARTICLES'
export const LOAD_ARTICLE = 'LOAD_ARTICLE'
export const LOAD_COMMENTS = 'LOAD_COMMENTS'

export const CHANGE_SELECTION = 'CHANGE_SELECTION'
export const CHANGE_DATE_RANGE = 'CHANGE_DATE_RANGE'
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ export default store => next => action => {
.then(res => res.json())
.then(response => next({...rest, response, type: type + SUCCESS}))
.catch(error => next({...rest, error, type: type + FAIL}))
}, 1000)
}, 300)
}
46 changes: 38 additions & 8 deletions src/reducer/comments.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,46 @@
import { ADD_COMMENT } from '../constants'
import {normalizedComments} from '../fixtures'
import { Map, Record } from 'immutable'
import { ADD_COMMENT, LOAD_COMMENTS, START, SUCCESS, FAIL } from '../constants'
import {arrToMap} from './utils'

export default (state = arrToMap(normalizedComments), action) => {
const { type, payload, randomId } = action
const CommentsRecord = Record({
id: null,
user: null,
text: null
})

const ReducerRecord = Record({
entities: arrToMap([], CommentsRecord),
loading: false,
loaded: false,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

здесь не достаточно повесить loading на весь comments, ведь ты для конкрентной статьи загружаешь

error: null,
//cache: false,
loadedId: null
})

export default (state = new ReducerRecord(), action) => {
const { type, payload, randomId, error } = action

switch (type) {
case ADD_COMMENT:
return state.set(randomId, {
...payload.comment,
id: randomId
})
return state.setIn(['entities', randomId],
new CommentsRecord({ ...payload.comment, id: randomId}))

case LOAD_COMMENTS + START:
return state
.set('loading', true)
.set('loadedId', payload.id)

case LOAD_COMMENTS + FAIL:
return state
.set('loading', false)
.set('error', error)

case LOAD_COMMENTS + SUCCESS:
return state
.set('loading', false)
.set('loaded', true)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Почему loaded === true, ведь ты загрузил только для одной статьи?

//.set('cache', true)
.set('entities', arrToMap(payload.response, CommentsRecord))
}

return state
Expand Down
11 changes: 8 additions & 3 deletions src/selectors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import {createSelector} from 'reselect'
export const articlesMapSelector = state => state.articles.entities
export const articlesLoadingSelector = state => state.articles.loading
export const filtersSelector = state => state.filters
export const commentListSelector = state => state.comments
export const commentListMapSelector = state => state.comments.entities
export const commentsLoadingSelector = state => state.comments.loading
export const commentsLoadedIdSelector = state => state.comments.loadedId
//export const commentsCacheSelector = state => state.comments.cache
export const idSelector = (_, props) => props.id

export const articlesSelector = createSelector(articlesMapSelector, articles => articles.valueSeq().toArray())
Expand All @@ -18,6 +21,8 @@ export const filtratedArticlesSelector = createSelector(articlesSelector, filter
})
})

export const createCommentSelector = () => createSelector(commentListSelector, idSelector, (comments, id) => {
return comments.get(id)
export const commentListSelector = createSelector(commentListMapSelector, comments => comments.valueSeq().toArray())

export const commentSelector = createSelector(commentListSelector, idSelector, (comments, id) => {
return comments.find(item => item.id == id)
})