From 6e8b7f410ac3e73ff5bff9987bb86767f2b0a51b Mon Sep 17 00:00:00 2001 From: Aditya Sankaran Date: Sun, 1 Dec 2019 15:55:53 -0500 Subject: [PATCH] saved liked posts --- twistter-frontend/src/pages/Home.js | 52 +++++++++++++++++++---------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/twistter-frontend/src/pages/Home.js b/twistter-frontend/src/pages/Home.js index 84763b9..0602a3a 100644 --- a/twistter-frontend/src/pages/Home.js +++ b/twistter-frontend/src/pages/Home.js @@ -39,7 +39,7 @@ class Home extends Component { render() { let authenticated = this.props.user.authenticated; - + let username = this.props.user.credentials.handle; let postMarkup = this.state.posts ? ( this.state.posts.map(post => @@ -61,7 +61,7 @@ class Home extends Component { Topics: {post.microBlogTopics}
{/* Likes {post.likeCount} */} - +
@@ -107,14 +107,6 @@ class Home extends Component { } -const mapStateToProps = (state) => ({ - user: state.user -}) - -Home.propTypes = { - user: PropTypes.object.isRequired -} - class Quote extends Component { constructor(props) { super(props); @@ -238,17 +230,28 @@ class Like extends Component { super(props) this.state = { num : this.props.count, - like: false + } this.handleClick = this.handleClick.bind(this); } + componentDidMount() { + this.setState({ + like: localStorage.getItem(this.props.microBlog + this.props.name) === "false" + + }) + + } + + + handleClick(){ - + this.setState({ like: !this.state.like }); + localStorage.setItem(this.props.microBlog + this.props.name, this.state.like.toString()) if(this.state.like == false) { @@ -284,19 +287,27 @@ class Like extends Component { axios.get(`/checkforLikePost/${this.props.microBlog}`) .then((res) => { this.setState({ - like: res.data + like2: res.data }) console.log(res.data); }) .catch((err) => { console.log(err) }) - } */ + if (this.state.like2 === this.state.like) + { + this.setState({ + like: false + }) + } + } */ render() { const label = this.state.like ? 'Unlike' : 'Like' return( + +
Likes {this.state.num} @@ -305,7 +316,12 @@ class Like extends Component { } } -Quote.propTypes = { + +const mapStateToProps = (state) => ({ + user: state.user +}) + +Home.propTypes = { user: PropTypes.object.isRequired } @@ -313,6 +329,8 @@ Like.propTypes = { user: PropTypes.object.isRequired } +Quote.propTypes = { + user: PropTypes.object.isRequired +} - -export default connect(mapStateToProps)(Home, Quote, Like); \ No newline at end of file +export default connect(mapStateToProps)(Home, Like, Quote); \ No newline at end of file