Last active
November 20, 2018 09:15
-
-
Save mlshv/7c81ed4f46cdcf71ca429db363578f70 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
class Article extends Component { | |
constructor(props) { | |
super(props) | |
this.state = { | |
isOpen: props.defaultOpen, | |
idAd: props.idAd | |
} | |
} | |
componentWillReceiveProps(nextProps) { | |
if (nextProps.defaultOpen !== this.props.defaultOpen) { | |
this.setState({ | |
isOpen: nextProps.defaultOpen | |
}) | |
} | |
if (nextProps.idAd !== this.props.idAd) { | |
this.setState({ | |
idAd: nextProps.idAd | |
}) | |
} | |
} | |
render() { | |
const {article} = this.props; | |
const body = this.state.isOpen && <section className = 'card-text'> {this.props.description} </section> | |
return ( | |
<div className="card mx-auto" style = {{width: '70%'}}> | |
<div className = "card-header "> | |
<h4> | |
{this.props.name} | |
<button onClick = {this.handleClick} className = 'btn btn-primary btn-lg float-right'> | |
{this.state.isOpen ? "close" : "open"} | |
</button> | |
<button className = 'btn btn-primary btn-lg float-right' onClick={this.delete} > | |
Delete | |
</button> | |
</h4> | |
</div> | |
<div className = 'card-body'> | |
{body} | |
<h6 className = "card-subtitle text-muted"> phone [rus]: {this.props.phoneNumber} </h6> | |
</div> | |
</div> | |
) | |
} | |
delete = () => { | |
this.props.deleteAd(this.state.idAd); | |
localStorage.removeItem(this.state.idAd); | |
} | |
handleClick = () => { | |
this.setState({ | |
isOpen: !this.state.isOpen | |
}) | |
} | |
} | |
export default Article; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment