SongItem.jsx 1.9 KB

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