Skip to content
Open

Hw 5 #53

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
9 changes: 8 additions & 1 deletion src/AC/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
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 +74,11 @@ export function loadArticleById(id) {
}))
}, 1000)
}
}

export function loadCommentsByArticleId(id) {
return {
type: LOAD_COMMENTS,
callAPI: `/api/comment?article=${id}`
}
}
39 changes: 33 additions & 6 deletions src/components/comment-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import CSSTransition from 'react-addons-css-transition-group'
import Comment from '../comment'
import CommentForm from '../comment-form'
import toggleOpen from '../../decorators/toggleOpen'
import Loader from '../loader'
import {loadingCommentsSelector, commentsMapSelector} from '../../selectors'
import { connect } from 'react-redux'
import { loadCommentsByArticleId } from '../../AC'
import './style.css'

class CommentList extends Component {
Expand All @@ -18,8 +22,18 @@ class CommentList extends Component {
toggleOpen: PropTypes.func
}

componentWillReceiveProps({ isOpen, loadCommentsByArticleId, article, comments }) {
if (!this.props.isOpen && isOpen) {
loadCommentsByArticleId(article.id)
console.log("!!!" + comments)
}
}
/* componentDidMount() {
console.log(this.props)//loadCommentsByArticleId
}*/
render() {
const {isOpen, toggleOpen} = this.props
const {isOpen, toggleOpen, loading} = this.props
if (loading) return <Loader />
const text = isOpen ? 'hide comments' : 'show comments'
return (
<div>
Expand All @@ -36,14 +50,23 @@ class CommentList extends Component {
}

getBody() {
const {article: { comments, id }, isOpen} = this.props
const {article: { id }, comments, commentsMap, isOpen} = this.props
if (!isOpen) return null

const commentElements = commentsMap.toSeq(id =>
<li key = {id} className = "test__comment-list--item">
<Comment id = {id}/>
</li>
)
return (
<div className="test__comment-list--body">
{
comments.length
? this.getComments()
commentsMap.size
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.

Если очень хочешь так делать - сделай лучше в коннекте. А вообще у нас Comment умеет достать объект по id

? <ul>
{
commentElements
}
</ul>
: <h3 className="test__comment-list--empty">No comments yet</h3>
}
<CommentForm articleId = {id} />
Expand All @@ -55,7 +78,7 @@ class CommentList extends Component {
return (
<ul>
{
this.props.article.comments.map(id =>
this.props./*article.*/comments.map(id =>
<li key = {id} className = "test__comment-list--item">
<Comment id = {id}/>
</li>)
Expand All @@ -66,4 +89,8 @@ class CommentList extends Component {
}


export default toggleOpen(CommentList)
export default connect(state => ({
comments: state.comments,
commentsMap: commentsMapSelector(state),
loading: loadingCommentsSelector(state)
}), { loadCommentsByArticleId })(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_COMMENTS = 'LOAD_COMMENTS'

export const START = '_START'
export const SUCCESS = '_SUCCESS'
Expand Down
35 changes: 30 additions & 5 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 { ADD_COMMENT, LOAD_COMMENTS, START, SUCCESS } from '../constants'
import {normalizedComments} from '../fixtures'
import {arrToMap} from './utils'
import { Record } from 'immutable'

export default (state = arrToMap(normalizedComments), action) => {
const { type, payload, randomId } = action

const CommentRecord = Record({
id: null,
user: null,
text: null
})

const ReducerState = Record({
entities: arrToMap([], CommentRecord),
loading: false,
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.

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

loaded: false
})

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

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

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

case LOAD_COMMENTS + SUCCESS:
console.log("bzzzzz", response)
return comments
.set('entities', arrToMap(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)
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.

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


default:
return state
return comments
}
}
4 changes: 4 additions & 0 deletions src/selectors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ export const articlesMapSelector = state => state.articles.entities
export const articleListSelector = createSelector(articlesMapSelector, articlesMap => articlesMap.valueSeq().toArray())
export const loadingArticlesSelector = state => state.articles.loading

export const commentsMapSelector = state => state.comments.entities
export const commentListSelector = createSelector(commentsMapSelector, commentMap => commentMap.valueSeq().toArray())
export const loadingCommentsSelector = state => state.comments.loading

const commentsSelector = state => state.comments
const filtersSelector = state => state.filters
const idSelector = (_, props) => props.id
Expand Down