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
27 changes: 25 additions & 2 deletions src/AC/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
INCREMENT, DELETE_ARTICLE, CHANGE_DATE_RANGE, CHANGE_SELECTION, ADD_COMMENT, LOAD_ALL_ARTICLES, LOAD_ARTICLE,
INCREMENT, DELETE_ARTICLE, CHANGE_DATE_RANGE, CHANGE_SELECTION, ADD_COMMENT,
LOAD_ALL_ARTICLES, LOAD_ARTICLE, LOAD_COMMENTS,
START, SUCCESS, FAIL
} from '../constants'

Expand Down Expand Up @@ -74,4 +75,26 @@ export function loadArticleById(id) {
}))
}, 1000)
}
}
}

export function loadCommentsById(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 }
}))
.catch(error => dispatch({
type: LOAD_COMMENTS + FAIL,
payload: { id, error }
}))
}, 1000)
}
}
26 changes: 21 additions & 5 deletions src/components/comment-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,30 @@ import PropTypes from 'prop-types'
import CSSTransition from 'react-addons-css-transition-group'
import Comment from '../comment'
import CommentForm from '../comment-form'
import Loader from '../loader'
import toggleOpen from '../../decorators/toggleOpen'
import './style.css'

import { connect } from 'react-redux'
import { loadCommentsById } from '../../AC'

class CommentList extends Component {
static defaultProps = {
comments: []
}

static propTypes = {
article: PropTypes.object.isRequired,
//from toggleOpen decorator
isOpen: PropTypes.bool,
toggleOpen: PropTypes.func
}


componentWillReceiveProps({ isOpen, loadCommentsById, article}) {
if (!this.props.isOpen && isOpen && !this.props.loaded)
loadCommentsById(article.id)
}

render() {
const {isOpen, toggleOpen} = this.props
const text = isOpen ? 'hide comments' : 'show comments'
Expand Down Expand Up @@ -52,18 +61,25 @@ class CommentList extends Component {
}

getComments() {
if (this.props.loading) return <Loader/>

return (
<ul>
{
this.props.article.comments.map(id =>
<li key = {id} className = "test__comment-list--item">
<Comment id = {id}/>
this.props.comments.map(comment =>
<li key = {comment.id} className = "test__comment-list--item">
<Comment comment = {comment}/>
</li>)
}
</ul>
)
}
}

const mapStateToProps = state => ({
comments: state.comments.get('entities'),
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

вот этого я совсем не понял. Все entities?

loading: state.comments.get('loading'),
loaded: state.comments.get('loaded')
})

export default toggleOpen(CommentList)
export default connect(mapStateToProps, { loadCommentsById })(toggleOpen(CommentList))
28 changes: 19 additions & 9 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 { createCommentSelector } from '../selectors'

function Comment({comment}) {
return (
Expand All @@ -18,12 +18,22 @@ Comment.propTypes = {
}).isRequired
}

const createMapStateToProps = () => {
const commentSelector = createCommentSelector()
// const createMapStateToProps = () => {
// const commentSelector = createCommentSelector()
//
// return ((state, ownProps) => {
// // console.log('COMMENT createMapStateToProps', ownProps);
// return ({
// // comment: commentSelector(state, ownProps)
// comment: ownProps.comment
// })
// })
//
// // return (state, ownProps) => ({
// // comment: commentSelector(state, ownProps)
// // })
// }
//
// export default connect(createMapStateToProps)(Comment)

return (state, ownProps) => ({
comment: commentSelector(state, ownProps)
})
}

export default connect(createMapStateToProps)(Comment)
export default connect(null)(Comment)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

зачем вообще этот коннект?

3 changes: 2 additions & 1 deletion 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 All @@ -11,4 +12,4 @@ export const ADD_COMMENT = 'ADD_COMMENT'

export const START = '_START'
export const SUCCESS = '_SUCCESS'
export const FAIL = '_FAIL'
export const FAIL = '_FAIL'
2 changes: 1 addition & 1 deletion src/middlewares/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ export default store => next => action => {
next(action)

console.log('---', 'state after: ', store.getState())
}
}
5 changes: 2 additions & 3 deletions src/reducer/articles.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DELETE_ARTICLE, ADD_COMMENT, LOAD_ALL_ARTICLES, LOAD_ARTICLE, START, SUCCESS, FAIL } from '../constants'
import { DELETE_ARTICLE, ADD_COMMENT, LOAD_ALL_ARTICLES, LOAD_ARTICLE, START, SUCCESS} from '../constants'
import { arrToMap } from './utils'
import { Record } from 'immutable'

Expand Down Expand Up @@ -46,8 +46,7 @@ export default (articles = new ReducerState(), action) => {
case LOAD_ARTICLE + SUCCESS:
return articles.setIn(['entities', payload.id], new ArticleRecord(payload.response))


default:
return articles
}
}
}
41 changes: 33 additions & 8 deletions src/reducer/comments.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
import { ADD_COMMENT } from '../constants'
import {normalizedComments} from '../fixtures'
import { ADD_COMMENT, LOAD_COMMENTS, START, SUCCESS, FAIL} from '../constants'
import {arrToMap} from './utils'
import { Record } from 'immutable'

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

const ReducerState = Record({
entities: arrToMap([], CommentRecord),
loading: false,
loaded: false,
error: null
})

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

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

case LOAD_COMMENTS + START:
return state.set('loading', true);

case LOAD_COMMENTS + FAIL:
return arrToMap(state)

case LOAD_COMMENTS + SUCCESS:
return state
.set('entities', arrToMap(payload.response, CommentRecord))
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

сейчас загружая комменты для одной статьи ты перезатираешь прошлые; используй .mergeIn

.set('loading', false)
.set('loaded', true)

default:
return state
}
}
}