Skip to content

Ht3 opanasiuk #19

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
3 changes: 2 additions & 1 deletion src/components/article.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { Component } from 'react'
import CommponentsList from './comments'

class Article extends Component {
render() {
Expand All @@ -17,7 +18,7 @@ class Article extends Component {
getBody() {
const { isOpen, article } = this.props
if (!isOpen) return null
return <section>{article.text}</section>
return <CommponentsList article={article} />
}
}

Expand Down
36 changes: 36 additions & 0 deletions src/components/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React, { Component } from 'react'

class CommponentsList extends Component {
state = {
show: false
}

handleToggleShow = () => {
this.setState({ show: !this.state.show })
}
render() {
const { article } = this.props
const { show } = this.state
return (
<div>
<section>{article.text}</section>
<button onClick={this.handleToggleShow}>{show ? 'Hide comments' : 'Open comments'}</button>
{show && (
<div>
<h4>Comments:</h4>
<ul>
{article.comments.map((comment) => (
<li key={comment.id}>
<p>User name: {comment.user}</p>
<p>{comment.text}</p>
</li>
))}
</ul>
</div>
)}
</div>
)
}
}

export default CommponentsList
39 changes: 39 additions & 0 deletions src/decorators/componentShowHOC.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import React, { Component } from 'react'

// export default function (WrappedComponent) {
// return class ComponentShowHOC extends Component {
// state = {
// show: false
// }
// handleToggleShow = () => this.setState({ show: !this.state.show })

// render() {
// return (
// <WrappedComponent
// {...this.props}
// show={this.state.show}
// handleToggleShow={this.handleToggleShow}
// />
// )
// }
// }
// }

export default (OriginalComponent) =>
class ComponentShowHOC extends Component {
state = {
show: false
}

handleToggleShow = () => this.setState({ show: !this.state.show })

render() {
return (
<OriginalComponent
{...this.props}
show={this.state.show}
handleToggleShow={this.handleToggleShow}
/>
)
}
}