QueueList.jsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import React, { Component } from "react";
  2. import PropTypes from "prop-types";
  3. import CustomInput from "components/CustomInput.jsx";
  4. import CustomErrors from "components/CustomMessages.jsx";
  5. import { connect } from "react-redux";
  6. import { closeOverlay1, openOverlay2 } from "actions/stationOverlay";
  7. import io from "io";
  8. import {closeOverlay2} from "../../../actions/stationOverlay";
  9. @connect(state => ({
  10. user: {
  11. userId: state.user.get("userId"),
  12. role: state.user.get("role"),
  13. },
  14. loggedIn: state.user.get("loggedIn"),
  15. stationId: state.station.get("id"),
  16. stationOwner: state.station.get("ownerId"),
  17. songTitle: state.songPlayer.get("title"),
  18. songArtists: state.songPlayer.get("artists"),
  19. songDuration: state.songPlayer.get("duration"),
  20. simpleSong: state.songPlayer.get("simple"),
  21. songExists: state.songPlayer.get("exists"),
  22. }))
  23. export default class Settings extends Component {
  24. constructor(props) {
  25. super(props);
  26. this.state = {
  27. queue: [],
  28. userIdMap: {},
  29. userIdMapLoading: [],
  30. };
  31. io.getSocket((socket) => {
  32. socket.emit('stations.getQueue', this.props.stationId, data => {
  33. if (data.status === 'success') {
  34. this.setState({
  35. queue: data.queue,
  36. });
  37. data.queue.forEach((song) => {
  38. this.checkUserId(song.requestedBy);
  39. });
  40. }
  41. });
  42. socket.on('event:queue.update', queue => {
  43. this.setState({
  44. queue,
  45. });
  46. queue.forEach((song) => {
  47. this.checkUserId(song.requestedBy);
  48. });
  49. });
  50. });
  51. }
  52. isOwner = () => {
  53. if (this.props.loggedIn) {
  54. if (this.props.user.role === "admin") return true;
  55. if (this.props.user.userId === this.props.stationOwner) return true;
  56. }
  57. return false;
  58. };
  59. deleteSong = (songId) => {
  60. io.getSocket((socket) => {
  61. socket.emit("stations.removeFromQueue", this.props.stationId, songId, (data) => {
  62. if (data.status === "success") this.messages.clearAddSuccess("Successfully removed song.");
  63. else this.messages.clearAddError("Failed to remove song.", data.message);
  64. });
  65. });
  66. };
  67. checkUserId = (userId) => {
  68. if (!this.state.userIdMap[`Z${ userId }`] && !this.state.userIdMapLoading[`Z${ userId }`]) {
  69. this.setState({
  70. userIdMapLoading: this.state.userIdMapLoading.concat([`Z${ userId }`]),
  71. });
  72. io.getSocket((socket) => {
  73. socket.emit("users.getUsernameFromId", userId, (data) => {
  74. if (data.status === "success") {
  75. this.setState({
  76. userIdMap: {
  77. [`Z${ userId }`]: data.data,
  78. },
  79. });
  80. }
  81. });
  82. });
  83. }
  84. };
  85. addSongToQueueCallback = (songId) => {
  86. io.getSocket((socket) => {
  87. // Add song to queue
  88. socket.emit("stations.addToQueue", this.props.stationId, songId, res => {
  89. if (res.status === "success") {
  90. this.messages.clearAddSuccess("Successfully added song.");
  91. } else {
  92. this.messages.addError(res.message);
  93. }
  94. this.props.dispatch(closeOverlay2());
  95. });
  96. });
  97. };
  98. addSongToQueue = () => {
  99. this.props.dispatch(openOverlay2("searchYouTube", null, this.addSongToQueueCallback));
  100. };
  101. close = () => {
  102. this.props.dispatch(closeOverlay1());
  103. };
  104. render() {
  105. console.log(this.isOwner());
  106. return (
  107. <div className="overlay">
  108. <button onClick={ this.close }>Back</button>
  109. <h1>Queue</h1>
  110. <CustomErrors onRef={ ref => (this.messages = ref) } />
  111. {
  112. (this.state.queue)
  113. ? (
  114. <ul>
  115. {
  116. (this.props.songExists)
  117. ? (
  118. <li>
  119. <p>{ this.props.songTitle }</p>
  120. <p>{ this.props.songDuration }</p>
  121. <hr/>
  122. </li>
  123. ) : null
  124. }
  125. {
  126. this.state.queue.map((song) => {
  127. return (
  128. <li key={ song.songId }>
  129. <p>{ song.title }</p>
  130. <p>{ song.duration }</p>
  131. <a href={ `/u/${ this.state.userIdMap[`Z${ song.requestedBy }`] }` }>{ this.state.userIdMap[`Z${ song.requestedBy }`] }</a>
  132. <p onClick={ () => { this.deleteSong(song.songId) } }>Delete</p>
  133. <hr/>
  134. </li>
  135. );
  136. })
  137. }
  138. </ul>
  139. )
  140. : null
  141. }
  142. <button onClick={ this.addSongToQueue }>Add song to queue</button>
  143. <button onClick={ () => {io.getSocket((socket) => {socket.emit("stations.addToQueue", this.props.stationId, "A1PAO3jgmXY", (data) => {console.log(data);});});} }>Add temp</button>
  144. <button onClick={ () => {io.getSocket((socket) => {socket.emit('stations.updatePartyMode', this.props.stationId, true, res => {console.log(res);});})}}>Enable party</button>
  145. </div>
  146. );
  147. }
  148. }