-
Notifications
You must be signed in to change notification settings - Fork 88
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
HT6 #36
base: master
Are you sure you want to change the base?
HT6 #36
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,15 +3,22 @@ import PropTypes from 'prop-types' | |
import Comment from './Comment' | ||
import CommentForm from './CommentForm' | ||
import toggleOpen from '../decorators/toggleOpen' | ||
import {connect} from 'react-redux' | ||
import {loadAllComments} from '../AC' | ||
import Loader from './Loader' | ||
|
||
function CommentList({article, isOpen, toggleOpen}) { | ||
const text = isOpen ? 'hide comments' : 'show comments' | ||
return ( | ||
<div> | ||
<button onClick={toggleOpen}>{text}</button> | ||
{getBody({article, isOpen})} | ||
</div> | ||
) | ||
class CommentList extends Component { | ||
render() { | ||
let { article, isOpen, toggleOpen, loadAllComments, loaded, loading, comments } = this.props | ||
|
||
const text = isOpen ? 'hide comments' : 'show comments' | ||
return ( | ||
<div> | ||
<button onClick={toggleOpen}>{text}</button> | ||
{isOpen && <Comments {...{article, isOpen, loadAllComments, loaded, loading, commentsState: comments}} />} | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
CommentList.propTypes = { | ||
|
@@ -21,23 +28,43 @@ CommentList.propTypes = { | |
toggleOpen: PropTypes.func | ||
} | ||
|
||
function getBody({article: {comments = [], id}, isOpen}) { | ||
if (!isOpen) return null | ||
if (!comments.length) return ( | ||
<div> | ||
<p>No comments yet</p> | ||
<CommentForm articleId = {id} /> | ||
</div> | ||
) | ||
|
||
return ( | ||
<div> | ||
<ul> | ||
{comments.map(id => <li key={id}><Comment id = {id}/></li>)} | ||
</ul> | ||
<CommentForm articleId = {id} /> | ||
</div> | ||
) | ||
class Comments extends Component { | ||
componentDidMount() { | ||
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. а изменения, только mount? |
||
const {loaded, loading, loadAllComments} = this.props | ||
if (!loaded || !loading) loadAllComments() | ||
} | ||
|
||
render() { | ||
let {article: {comments = [], id}, loading, commentsState} = this.props | ||
|
||
if (loading) return <Loader /> | ||
|
||
if (!comments.length) return ( | ||
<div> | ||
<p>No comments yet</p> | ||
<CommentForm articleId = {id} /> | ||
</div> | ||
) | ||
|
||
return ( | ||
<div> | ||
<ul> | ||
{comments.map(id => | ||
<li key={id}> | ||
<Comment comment = {commentsState.get(id)}/> | ||
</li> | ||
)} | ||
</ul> | ||
<CommentForm articleId = {id} /> | ||
</div> | ||
) | ||
} | ||
} | ||
|
||
export default toggleOpen(CommentList) | ||
export default toggleOpen(connect((state) => { | ||
return { | ||
comments: state.comments.comments, | ||
loading: state.comments.loading, | ||
loaded: state.comments.loaded | ||
} | ||
}, {loadAllComments})(CommentList)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,4 +49,4 @@ export default (articleState = defaultState, action) => { | |
} | ||
|
||
return articleState | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,41 @@ | ||
import {normalizedComments as defaulComments} from '../fixtures' | ||
import {ADD_COMMENT} from '../constants' | ||
import {ADD_COMMENT, LOAD_ALL_COMMENTS, START, SUCCESS} from '../constants' | ||
import {arrToMap} from '../helpers' | ||
import {OrderedMap, Record} from 'immutable' | ||
|
||
const commentsMap = arrToMap(defaulComments) | ||
const CommentRecord = Record({ | ||
id: null, | ||
user: '', | ||
text: '' | ||
}) | ||
|
||
export default (commentsState = commentsMap, action) => { | ||
const {type, payload, randomId} = action | ||
const ReducerState = Record({ | ||
comments: new OrderedMap({}), | ||
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. лучше нейтральное название, вроде entities |
||
loading: false, | ||
loaded: false | ||
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. Такая система не пойдет, нужно было загружать не все комменты, а для отдельных статей |
||
}) | ||
|
||
const defaultState = new ReducerState() | ||
|
||
export default (commentsState = defaultState, action) => { | ||
const {type, payload, randomId, response} = action | ||
|
||
switch (type) { | ||
case ADD_COMMENT: | ||
return {...commentsState, [randomId]: payload.comment} | ||
return commentsState.updateIn([randomId]: payload.comment) | ||
|
||
case LOAD_ALL_COMMENTS + START: | ||
return commentsState.set('loading', true) | ||
|
||
case LOAD_ALL_COMMENTS + SUCCESS:{ | ||
let comments = arrToMap(Array.prototype.slice.call(response.records), CommentRecord) | ||
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. slice лишнее |
||
|
||
return commentsState | ||
.set('comments', comments) | ||
.set('loading', false) | ||
.set('loaded', true) | ||
} | ||
} | ||
|
||
return commentsState | ||
} | ||
} |
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.
не все комменты, только для одной статьи