Last active
October 11, 2017 04:46
-
-
Save Dajust/047793245181ff72b223eb39888307dd 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"; | |
import { PropTypes } from "prop-types"; | |
import { postData, getLikeBusinessEndpoint } from "../../utils/"; | |
import CheckAuth from "../checkAuth/CheckAuth"; | |
class LikeBusinessButton extends Component { | |
state = { | |
isLiked: this.props.isLiked | |
}; | |
static propTypes = { | |
isLiked: PropTypes.number.isRequired, | |
businessId: PropTypes.number.isRequired, | |
user: PropTypes.object, | |
location: PropTypes.object.isRequired, | |
AccessToken: PropTypes.string | |
}; | |
handleLikeBusiness = (AccessToken = this.props.AccessToken) => { | |
const { isLiked } = this.state; | |
this.setState(() => ({ isLiked: isLiked == 1 ? 0 : 1 })); | |
postData( | |
{}, | |
getLikeBusinessEndpoint( | |
this.props.businessId, | |
isLiked == 1 ? 0 : 1, | |
AccessToken, | |
this.props.user.UserId, | |
// undo the like/unlike if the action was not succesful. | |
data => | |
data.StatusCode != 1 && | |
this.setState(() => ({ isLiked: isLiked == 1 ? 0 : 1 })) | |
) | |
); | |
}; | |
render() { | |
const { isLiked } = this.state; | |
return ( | |
<CheckAuth user={this.props.user} intentPage={this.props.location.pathname}> | |
{onCheckAuth => ( | |
<a | |
onClick={e => { | |
e.preventDefault(); | |
onCheckAuth(this.handleLikeBusiness); | |
}} | |
style={{ color: isLiked == 1 ? "red" : "" }} | |
className="split nripple-wrapper" | |
> | |
<div className="nripple js-ripple"> | |
<span className="nripple__circle" /> | |
</div> | |
<div className="nlisting-sort-nav sticky-sort-nav"> | |
<i className="icon-heart" /> | |
</div> | |
</a> | |
)} | |
</CheckAuth> | |
); | |
} | |
} | |
export default LikeBusinessButton; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So here, you can see how we fixed it in line 20. In case the no AccessToken props was passed, usually, when the user is already logged-in before performing the LIKE action, we'll fall back to reading AccessToken from the props. 😄