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
21 changes: 14 additions & 7 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
START, SUCCESS, FAIL, LOAD_ALL_COMMENTS
} from '../constants'

export function increment() {
Expand Down Expand Up @@ -47,11 +47,11 @@ export function loadAllArticles() {

/*
export function loadArticle(id) {
return {
type: LOAD_ARTICLE,
payload: { id },
callAPI: `/api/article/${id}`
}
return {
type: LOAD_ARTICLE,
payload: { id },
callAPI: `/api/article/${id}`
}
}
*/

Expand All @@ -70,6 +70,13 @@ export function loadArticle(id) {
payload: { id, response }
}))

}, 1000)
}, 300)
}
}

export function loadAllComments(id) {
return {
type: LOAD_ALL_COMMENTS,
callAPI: `/api/comment?article=${id}`,
}
}
6 changes: 3 additions & 3 deletions src/components/Comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PropTypes from 'prop-types'
import {connect} from 'react-redux'
import {createCommentSelector} from '../selectors'

function Comment({comment}) {
export default function Comment({comment}) {
return (
<div>
{comment.text} <b>by {comment.user}</b>
Expand All @@ -20,7 +20,7 @@ Comment.propTypes = {
}).isRequired
}

const createMapStateToProps = () => {
/* const createMapStateToProps = () => {
const commentSelector = createCommentSelector()
return (state, ownProps) => {
return {
Expand All @@ -29,4 +29,4 @@ const createMapStateToProps = () => {
}
}

export default connect(createMapStateToProps)(Comment)
export default connect(createMapStateToProps)(Comment) */
27 changes: 22 additions & 5 deletions src/components/CommentList.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import PropTypes from 'prop-types'
import CommentForm from './CommentForm'
import Comment from './Comment'
import toggleOpen from '../decorators/toggleOpen'
import { loadAllComments } from '../AC';
import { connect } from 'react-redux';
import Loader from './common/Loader';
import { commentListSelector } from '../selectors';
import { commentsLoadingSelector } from '../selectors/';

class CommentList extends Component {
static propTypes = {
Expand All @@ -23,24 +28,36 @@ class CommentList extends Component {
)
}

componentWillReceiveProps(nextProps) {
if(!this.props.comments.loaded && nextProps.isOpen && !this.props.isOpen) {
const { loadAllComments } = this.props;
loadAllComments(this.props.article.id);
}
}

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

const body = comments.length ? (
const body = comments.entities.size > 0 ? (
<ul>
{comments.map(id => <li key = {id}><Comment id = {id} /></li>)}
{comments.entities.valueSeq().map(comment => <li key = {comment.id}><Comment comment={comment} /></li>)}
Copy link
Owner

Choose a reason for hiding this comment

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

сделай для этого селектор, старайся не завязываться в компонентах на api immutable

</ul>
) : <h3>No comments yet</h3>

return (
<div>
{body}
<CommentForm articleId = {id} />
<CommentForm articleId = {articleId} />
</div>
)
}
}


export default toggleOpen(CommentList)
export default connect(state => ({
loading: commentsLoadingSelector(state),
comments: commentListSelector(state),
}), {loadAllComments})(toggleOpen(CommentList));
1 change: 1 addition & 0 deletions src/constants/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const CHANGE_SELECTION = 'CHANGE_SELECTION'
export const CHANGE_DATE_RANGE = 'CHANGE_DATE_RANGE'

export const ADD_COMMENT = 'ADD_COMMENT'
export const LOAD_ALL_COMMENTS = 'LOAD_ALL_COMMENTS'

export const START = '_START'
export const SUCCESS = '_SUCCESS'
Expand Down
42 changes: 37 additions & 5 deletions src/reducer/comments.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,49 @@
import { ADD_COMMENT } from '../constants'
import { ADD_COMMENT, LOAD_ALL_COMMENTS, START, FAIL, SUCCESS } from '../constants'
import {Record} from 'immutable'
import {normalizedComments} from '../fixtures'
import {arrToMap} from './utils'

export default (state = arrToMap(normalizedComments), action) => {
const { type, payload, randomId } = action
const CommentRecord = Record({
id: null,
text: null,
author: null,
loading: true,
})

const ReducerRecord = Record({
entities: arrToMap([], CommentRecord),
loading: 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, ведь ты для конкрентной статьи загружаешь

loaded: false,
error: null
})

// export default (comments = arrToMap(normalizedComments), action) => {
export default (comments = new ReducerRecord(), action) => {
const { type, payload, randomId, response, error } = action

switch (type) {
case ADD_COMMENT:
return state.set(randomId, {
return comments.set(randomId, {
...payload.comment,
id: randomId
})

case LOAD_ALL_COMMENTS + START:
return comments
.set('loading', true)
.set('loaded', false)

case LOAD_ALL_COMMENTS + FAIL:
return comments
.set('loading', false)
.set('error', error)

case LOAD_ALL_COMMENTS + SUCCESS:
return comments
.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('entities', arrToMap(response, CommentRecord));
Copy link
Author

Choose a reason for hiding this comment

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

Я думаю, что здесь я возвращаю неправильный объект, т.к. у меня все комментарии оказываются в entities.

Copy link
Owner

Choose a reason for hiding this comment

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

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

}

return state
return comments
}
7 changes: 6 additions & 1 deletion src/selectors/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import {createSelector} from 'reselect'

export const articlesMapSelector = state => state.articles.entities
export const articlesLoadingSelector = state => state.articles.loading
export const articlesLoadingSelector = state => {
return state.articles.loading;
}
export const filtersSelector = state => state.filters
export const commentListSelector = state => state.comments
export const idSelector = (_, props) => props.id
export const commentsLoadingSelector = state => {
return state.comments.loading;
}

export const articlesSelector = createSelector(articlesMapSelector, articles => articles.valueSeq().toArray())

Expand Down