I have very simple forum with React and firebase and currently everyone is allowed to vote on a post, but the problem is you can click on the like and dislike button more than once.
How do i prevent this? I'm trying to do something with location.storage
but not sure how to track each post.
class App extends React.Component {
downvotePost(key, text, vote) {
// alert(id);
// CommentsRef.child(id).transaction(function(Comments) {
// if (Comments) {
// Comments.vote++;
// }
// console.log('cc',Comments);
// return Comments;
// });
vote--;
CommentsRef.child(key).update({ vote: vote });
}
upvotePost(key, text, vote) {
// alert(id);
// CommentsRef.child(id).transaction(function(Comments) {
// if (Comments) {
// Comments.vote++;
// }
// console.log('cc',Comments);
// return Comments;
// });
vote++;
CommentsRef.child(key).update({ vote: vote });
}
componentWillMount() {
const { dispatch, match } = this.props;
dispatch(getsinglechamp(this.props.id));
console.log(this.props);
}
componentDidMount() {
const hasVisitedBefore = localStorage.getItem("hasVisitedBefore");
// Check if the user has visited the site (component) before.
if (!hasVisitedBefore) {
// If not, set the component state (or dispatch an action if you need)
// Also set the localStorage so it's globally accessible.
this.setState({ hasVisitedBefore: false });
localStorage.setItem("hasVisitedBefore", true);
}
}
render() {
const { dispatch, loading } = this.props;
const { comments, ChampsLoading } = this.state;
const orderedchamps = comments;
let commentList;
if (ChampsLoading) {
commentList = <div className="TaskList-empty">Loading...</div>;
} else if (comments.length) {
commentList = (
<ul className="TaskList">
{comments.map(comment => (
<div className="row">
<div className="col-lg-6">
<br /> <br /> <br />
<div className="cs-counter-tips-list">
<div className="cs-counter-tip">
{this.state.hasVisitedBefore
? "you can vote on this post"
: "disable button if user voted"}
<button
id="f"
onClick={() =>
this.upvotePost(comment.key, comment.text, comment.vote)
}
>
<Icon icon={chevronUp} />
</button>
<div id="col" className="col-lg-6">
{comment.vote}
</div>
<button
id="f"
onClick={() =>
this.downvotePost(
comment.key,
comment.text,
comment.vote
)
}
>
<Icon icon={chevronDown} />
</button>
<div>
<p className="cs-counter-tip-text">{comment.text} </p>
</div>
</div>
</div>
</div>
</div>
))}
</ul>
);
}
return (
<div className="container">
<h1>Counter Tips</h1>
<div className="brace"> </div>
<p> {commentList} </p>
</div>
);
}
}