Skip to content
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

HT-2 #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

HT-2 #11

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
22 changes: 12 additions & 10 deletions src/components/ArticleList.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import React, {Component} from 'react'
import Article from './Article'
import accordeon from '../decorators/accordeon'
import PropTypes from 'prop-types'

export default class ArticleList extends Component {
state = {
openArticleId: null
class ArticleList extends Component {
static propTypes = {
articles: PropTypes.array,
openArticleId: PropTypes.oneOfType([PropTypes.bool, PropTypes.string]).isRequired,
Copy link
Owner

Choose a reason for hiding this comment

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

почему bool ?

toggleOpenArticle: PropTypes.func.isRequired
}

render() {
const articleElements = this.props.articles.map(article => <li key={article.id}>
<Article
article = {article}
isOpen = {article.id === this.state.openArticleId}
toggleOpen = {this.toggleOpenArticle(article.id)}
isOpen = {article.id === this.props.openArticleId}
toggleOpen = {this.props.toggleOpenArticle(article.id)}
/>
</li>)

Expand All @@ -21,8 +25,6 @@ export default class ArticleList extends Component {
</ul>
)
}
}

toggleOpenArticle = openArticleId => ev => {
this.setState({ openArticleId })
}
}
export default accordeon(ArticleList)
6 changes: 5 additions & 1 deletion src/components/CommentList.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {Component} from 'react'
import Comment from './Comment'
import toggleOpen from '../decorators/toggleOpen'
import PropTypes from 'prop-types'

function CommentList({comments = [], isOpen, toggleOpen}) {
const text = isOpen ? 'hide comments' : 'show comments'
Expand All @@ -11,6 +12,9 @@ function CommentList({comments = [], isOpen, toggleOpen}) {
</div>
)
}
CommentList.propTypes = {
toggleOpen: PropTypes.func
}

function getBody({comments, isOpen}) {
if (!isOpen) return null
Expand All @@ -23,4 +27,4 @@ function getBody({comments, isOpen}) {
)
}

export default toggleOpen(CommentList)
export default toggleOpen(CommentList)
21 changes: 21 additions & 0 deletions src/decorators/accordeon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React, {Component as ReactComponent} from 'react'

export default (OriginalComponent) => class WrappedComponent extends ReactComponent {
Copy link
Owner

Choose a reason for hiding this comment

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

WrappedComponent я называл чтоб легче понять было. Лучше выбирай более значущее название

state = {
openArticleId: null
Copy link
Owner

Choose a reason for hiding this comment

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

Не привязывайся к названиям сущностей, вся суть декораторов в универсальности. Сделай openItemId

}

render() {
return <OriginalComponent {...{...this.props, ...this.state, toggleOpenArticle: this.toggleOpenArticle}}/>
Copy link
Owner

Choose a reason for hiding this comment

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

зачем тебе столько спредов?

}

toggleOpenArticle = openArticleId => ev => {
let { openArticleId: openedId } = this.state;

if (openedId === openArticleId) {
this.setState({openArticleId: null})
} else {
this.setState({ openArticleId })
}
}
}