SongItem.jsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. import { formatTime } from "utils";
  4. import { fallbackImage } from "constants.js";
  5. import { connect } from "react-redux";
  6. import io from "io";
  7. @connect(state => ({
  8. user: {
  9. loggedIn: state.session.get("loggedIn"),
  10. userId: state.session.get("userId"),
  11. role: state.session.get("role"),
  12. },
  13. station: {
  14. stationId: state.station.info.get("stationId"),
  15. owner: state.station.info.get("ownerId"),
  16. },
  17. }))
  18. export default class SongItem extends Component {
  19. constructor(props) {
  20. super(props);
  21. }
  22. deleteSong = (songId) => {
  23. io.getSocket((socket) => {
  24. socket.emit("stations.removeFromQueue", this.props.station.stationId, songId, (data) => {
  25. //if (data.status === "success") this.messages.clearAddSuccess("Successfully removed song.");
  26. //else this.messages.clearAddError("Failed to remove song.", data.message);
  27. });
  28. });
  29. };
  30. isOwner = () => {
  31. if (this.props.user.loggedIn) {
  32. if (this.props.user.role === "admin") return true;
  33. if (this.props.user.userId === this.props.station.owner) return true;
  34. }
  35. return false;
  36. };
  37. render() {
  38. const { song } = this.props;
  39. const showRequestedBy = (song.get("requestedByUsername") && song.get("requestedByUsername") !== "Unknown");
  40. const showDelete = (this.isOwner() && this.props.deleteable !== false);
  41. return (
  42. <li>
  43. <div className="left">
  44. <img src={ song.get("thumbnail") } onError={function(e) {e.target.src=fallbackImage}}/>
  45. </div>
  46. <div className="right">
  47. <span className="duration">{ formatTime(song.get("duration")) }</span>
  48. <p className="title">{ song.get("title") }</p>
  49. <span className="title-artists-spacing"/>
  50. <p className="artists">{ song.get("artists").join(", ") }</p>
  51. {
  52. (showRequestedBy) ?
  53. <span>
  54. <span>Requested by: </span>
  55. <a href={`/u/${ song.get("requestedByUsername") }`}>{ song.get("requestedByUsername") }</a>
  56. </span> : null
  57. }
  58. {
  59. (showDelete)
  60. ? <i style={{ color: "black" }} onClick={ () => { this.deleteSong(song.get("songId")) } }>Delete</i>
  61. : null
  62. }
  63. </div>
  64. </li>
  65. );
  66. }
  67. }