Skip to content

Instantly share code, notes, and snippets.

@Dajust
Last active October 11, 2017 04:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dajust/047793245181ff72b223eb39888307dd to your computer and use it in GitHub Desktop.
Save Dajust/047793245181ff72b223eb39888307dd to your computer and use it in GitHub Desktop.
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;
@Dajust
Copy link
Author

Dajust commented Oct 11, 2017

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. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment