-
Notifications
You must be signed in to change notification settings - Fork 14
Hw 6 #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Hw 6 #85
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,9 @@ import PropTypes from 'prop-types' | |
| import CommentForm from './CommentForm' | ||
| import Comment from './Comment' | ||
| import toggleOpen from '../decorators/toggleOpen' | ||
| import {loadComments} from '../AC' | ||
| import Loader from './common/Loader' | ||
| import store from '../store' | ||
|
|
||
| class CommentList extends Component { | ||
| static propTypes = { | ||
|
|
@@ -12,6 +15,10 @@ class CommentList extends Component { | |
| toggleOpen: PropTypes.func | ||
| } | ||
|
|
||
| componentWillReceiveProps({ isOpen, article, loaded}) { | ||
| if (!this.props.isOpen && isOpen && !loaded) store.dispatch(loadComments(article.id)) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не обращайся к стору явно, если очень хочешь использовать dispatch - заворачивай в коннект и бери из пропсов |
||
| } | ||
|
|
||
| render() { | ||
| const {isOpen, toggleOpen} = this.props | ||
| const text = isOpen ? 'hide comments' : 'show comments' | ||
|
|
@@ -24,9 +31,10 @@ class CommentList extends Component { | |
| } | ||
|
|
||
| getBody() { | ||
| const {article: { comments, id }, isOpen} = this.props | ||
| const {article: { comments, 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>)} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,42 @@ | ||
| import { ADD_COMMENT } from '../constants' | ||
| import {normalizedComments} from '../fixtures' | ||
| import { Map, Record } from 'immutable' | ||
| import { ADD_COMMENT, LOAD_COMMENTS, START, SUCCESS } from '../constants' | ||
| import {arrToMap} from './utils' | ||
|
|
||
| export default (state = arrToMap(normalizedComments), action) => { | ||
| const { type, payload, randomId } = action | ||
| const CommentRecord = Record({ | ||
| id: null, | ||
| user: null, | ||
| text: null, | ||
| }) | ||
|
|
||
| const idRecord = Record({ | ||
| id: null | ||
| }) | ||
|
|
||
| const ReducerRecord = Record({ | ||
| entities: arrToMap([], CommentRecord), | ||
| loading: arrToMap([], idRecord), | ||
| loaded: arrToMap([], idRecord), | ||
| error: arrToMap([], idRecord) | ||
| }) | ||
|
|
||
| export default (state = new ReducerRecord(), action) => { | ||
| const { type, payload, randomId} = action | ||
|
|
||
| switch (type) { | ||
| case ADD_COMMENT: | ||
| return state.set(randomId, { | ||
| ...payload.comment, | ||
| id: randomId | ||
| }) | ||
| return state.mergeIn(['entities'], arrToMap([{ | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Почему не просто setIn(['entities', id], new Record()) |
||
| id: randomId, | ||
| user: payload.comment.user, | ||
| text: payload.comment.text | ||
| }], CommentRecord)) | ||
| case LOAD_COMMENTS + START: | ||
| return state.setIn(['loading', payload.articleId], true) | ||
| case LOAD_COMMENTS + SUCCESS: | ||
| const { response } = action | ||
| return state | ||
| .setIn(['loading', payload.articleId], false) | ||
| .setIn(['loaded', payload.articleId], true) | ||
| .mergeIn(['entities'], arrToMap(response, CommentRecord)) | ||
| } | ||
|
|
||
| return state | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
почему бы не сделать сразу для этого селектор?