Ratings.jsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. import { connect } from "react-redux";
  4. import io from "io";
  5. @connect(state => ({
  6. user: {
  7. loggedIn: state.session.get("loggedIn"),
  8. },
  9. likes: state.station.currentSong.getIn(["ratings", "likes"]),
  10. dislikes: state.station.currentSong.getIn(["ratings", "dislikes"]),
  11. liked: state.station.currentSong.getIn(["ratings", "liked"]),
  12. disliked: state.station.currentSong.getIn(["ratings", "disliked"]),
  13. ratingsEnabled: state.station.currentSong.getIn(["ratings", "enabled"]),
  14. songId: state.station.currentSong.get("songId"),
  15. }))
  16. export default class Ratings extends Component {
  17. constructor(props) {
  18. super(props);
  19. }
  20. like = () => {
  21. io.getSocket(socket => {
  22. socket.emit("songs.like", this.props.songId, data => {/*TODO Handle error/success*/});
  23. });
  24. };
  25. dislike = () => {
  26. io.getSocket(socket => {
  27. socket.emit("songs.dislike", this.props.songId, data => {/*TODO Handle error/success*/});
  28. });
  29. };
  30. unlike = () => {
  31. io.getSocket(socket => {
  32. socket.emit("songs.unlike", this.props.songId, data => {/*TODO Handle error/success*/});
  33. });
  34. };
  35. undislike = () => {
  36. io.getSocket(socket => {
  37. socket.emit("songs.undislike", this.props.songId, data => {/*TODO Handle error/success*/});
  38. });
  39. };
  40. render() {
  41. if (!this.props.ratingsEnabled) return null;
  42. const likes = <span>{ this.props.likes }</span>;
  43. const dislikes = <span>{ this.props.dislikes }</span>;
  44. let likeButton = <i className="material-icons disabled">thumb_up</i>;
  45. let dislikeButton = <i className="material-icons disabled">thumb_down</i>;
  46. if (this.props.user.loggedIn) {
  47. if (this.props.liked) likeButton = <i className="material-icons liked" onClick={ this.unlike }>thumb_up</i>;
  48. else likeButton = <i className="material-icons" onClick={ this.like }>thumb_up</i>;
  49. if (this.props.disliked) dislikeButton = <i className="material-icons disliked" onClick={ this.undislike }>thumb_down</i>;
  50. else dislikeButton = <i className="material-icons" onClick={ this.dislike }>thumb_down</i>;
  51. }
  52. return (
  53. <div className="ratings-container">
  54. <div>
  55. { likeButton }
  56. { likes }
  57. </div>
  58. <div>
  59. { dislikeButton }
  60. { dislikes }
  61. </div>
  62. </div>
  63. );
  64. }
  65. }