saved liked posts

This commit is contained in:
Aditya Sankaran 2019-12-01 15:55:53 -05:00
parent 07a8f0b7bb
commit 6e8b7f410a

View File

@ -39,7 +39,7 @@ class Home extends Component {
render() { render() {
let authenticated = this.props.user.authenticated; let authenticated = this.props.user.authenticated;
let username = this.props.user.credentials.handle;
let postMarkup = this.state.posts ? ( let postMarkup = this.state.posts ? (
this.state.posts.map(post => this.state.posts.map(post =>
<Card> <Card>
@ -61,7 +61,7 @@ class Home extends Component {
<Typography variant="body2"><b>Topics:</b> {post.microBlogTopics}</Typography> <Typography variant="body2"><b>Topics:</b> {post.microBlogTopics}</Typography>
<br /> <br />
{/* <Typography variant="body2" color={"textSecondary"}>Likes {post.likeCount}</Typography> */} {/* <Typography variant="body2" color={"textSecondary"}>Likes {post.likeCount}</Typography> */}
<Like microBlog = {post.postId} count = {post.likeCount}></Like> <Like microBlog = {post.postId} count = {post.likeCount} name = {username}></Like>
<Quote microblog = {post.postId}></Quote> <Quote microblog = {post.postId}></Quote>
</CardContent> </CardContent>
</Card> </Card>
@ -107,14 +107,6 @@ class Home extends Component {
} }
const mapStateToProps = (state) => ({
user: state.user
})
Home.propTypes = {
user: PropTypes.object.isRequired
}
class Quote extends Component { class Quote extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
@ -238,17 +230,28 @@ class Like extends Component {
super(props) super(props)
this.state = { this.state = {
num : this.props.count, num : this.props.count,
like: false
} }
this.handleClick = this.handleClick.bind(this); this.handleClick = this.handleClick.bind(this);
} }
componentDidMount() {
this.setState({
like: localStorage.getItem(this.props.microBlog + this.props.name) === "false"
})
}
handleClick(){ handleClick(){
this.setState({ this.setState({
like: !this.state.like like: !this.state.like
}); });
localStorage.setItem(this.props.microBlog + this.props.name, this.state.like.toString())
if(this.state.like == false) if(this.state.like == false)
{ {
@ -284,19 +287,27 @@ class Like extends Component {
axios.get(`/checkforLikePost/${this.props.microBlog}`) axios.get(`/checkforLikePost/${this.props.microBlog}`)
.then((res) => { .then((res) => {
this.setState({ this.setState({
like: res.data like2: res.data
}) })
console.log(res.data); console.log(res.data);
}) })
.catch((err) => { .catch((err) => {
console.log(err) console.log(err)
}) })
} */ if (this.state.like2 === this.state.like)
{
this.setState({
like: false
})
}
} */
render() { render() {
const label = this.state.like ? 'Unlike' : 'Like' const label = this.state.like ? 'Unlike' : 'Like'
return( return(
<div> <div>
<Typography variant="body2" color={"textSecondary"}>Likes {this.state.num}</Typography> <Typography variant="body2" color={"textSecondary"}>Likes {this.state.num}</Typography>
<button onClick={this.handleClick}>{label}</button> <button onClick={this.handleClick}>{label}</button>
@ -305,7 +316,12 @@ class Like extends Component {
} }
} }
Quote.propTypes = {
const mapStateToProps = (state) => ({
user: state.user
})
Home.propTypes = {
user: PropTypes.object.isRequired user: PropTypes.object.isRequired
} }
@ -313,6 +329,8 @@ Like.propTypes = {
user: PropTypes.object.isRequired user: PropTypes.object.isRequired
} }
Quote.propTypes = {
user: PropTypes.object.isRequired
}
export default connect(mapStateToProps)(Home, Like, Quote);
export default connect(mapStateToProps)(Home, Quote, Like);