QueueList.jsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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, closeOverlay2 } from "actions/stationOverlay";
  7. import { selectPlaylist, deselectPlaylists } from "actions/playlistQueue";
  8. import io from "io";
  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. playlistSelectedId: state.playlistQueue.get("playlistSelected"),
  23. }))
  24. export default class Settings extends Component {
  25. constructor(props) {
  26. super(props);
  27. this.state = {
  28. queue: [],
  29. userIdMap: {},
  30. userIdMapLoading: [],
  31. playlists: [],
  32. };
  33. io.getSocket((socket) => {
  34. socket.emit('stations.getQueue', this.props.stationId, data => {
  35. if (data.status === 'success') {
  36. this.setState({
  37. queue: data.queue,
  38. });
  39. data.queue.forEach((song) => {
  40. this.checkUserId(song.requestedBy);
  41. });
  42. }
  43. });
  44. socket.on('event:queue.update', queue => {
  45. this.setState({
  46. queue,
  47. });
  48. queue.forEach((song) => {
  49. this.checkUserId(song.requestedBy);
  50. });
  51. });
  52. socket.emit('playlists.indexForUser', res => {
  53. if (res.status === 'success') this.setState({
  54. playlists: res.data,
  55. });
  56. });
  57. socket.on('event:playlist.create', () => {
  58. socket.emit('playlists.indexForUser', res => {
  59. if (res.status === 'success') this.setState({
  60. playlists: res.data,
  61. });
  62. });
  63. });
  64. socket.on('event:playlist.delete', () => {
  65. socket.emit('playlists.indexForUser', res => {
  66. if (res.status === 'success') this.setState({
  67. playlists: res.data,
  68. });
  69. });
  70. });
  71. });
  72. }
  73. isOwner = () => {
  74. if (this.props.loggedIn) {
  75. if (this.props.user.role === "admin") return true;
  76. if (this.props.user.userId === this.props.stationOwner) return true;
  77. }
  78. return false;
  79. };
  80. deleteSong = (songId) => {
  81. io.getSocket((socket) => {
  82. socket.emit("stations.removeFromQueue", this.props.stationId, songId, (data) => {
  83. if (data.status === "success") this.messages.clearAddSuccess("Successfully removed song.");
  84. else this.messages.clearAddError("Failed to remove song.", data.message);
  85. });
  86. });
  87. };
  88. checkUserId = (userId) => {
  89. if (!this.state.userIdMap[`Z${ userId }`] && !this.state.userIdMapLoading[`Z${ userId }`]) {
  90. this.setState({
  91. userIdMapLoading: this.state.userIdMapLoading.concat([`Z${ userId }`]),
  92. });
  93. io.getSocket((socket) => {
  94. socket.emit("users.getUsernameFromId", userId, (data) => {
  95. if (data.status === "success") {
  96. this.setState({
  97. userIdMap: {
  98. [`Z${ userId }`]: data.data,
  99. },
  100. });
  101. }
  102. });
  103. });
  104. }
  105. };
  106. addSongToQueueCallback = (songId) => {
  107. io.getSocket((socket) => {
  108. // Add song to queue
  109. socket.emit("stations.addToQueue", this.props.stationId, songId, res => {
  110. if (res.status === "success") {
  111. this.messages.clearAddSuccess("Successfully added song.");
  112. } else {
  113. this.messages.addError(res.message);
  114. }
  115. this.props.dispatch(closeOverlay2());
  116. });
  117. });
  118. };
  119. addSongToQueue = () => {
  120. this.props.dispatch(openOverlay2("searchYouTube", null, this.addSongToQueueCallback));
  121. };
  122. getPlaylistAction = (playlistId) => {
  123. if (playlistId === this.props.playlistSelectedId) {
  124. return <span>SELECTED</span>;
  125. } else return <span onClick={ () => { this.selectPlaylist(playlistId); } }>SELECT</span>;
  126. }
  127. selectPlaylist = (playlistId) => {
  128. this.props.dispatch(selectPlaylist(playlistId));
  129. }
  130. deselectAll = () => {
  131. this.props.dispatch(deselectPlaylists());
  132. }
  133. close = () => {
  134. this.props.dispatch(closeOverlay1());
  135. };
  136. render() {
  137. console.log(this.isOwner());
  138. return (
  139. <div className="overlay">
  140. <button onClick={ this.close }>Back</button>
  141. <h1>Queue</h1>
  142. <CustomErrors onRef={ ref => (this.messages = ref) } />
  143. {
  144. (this.state.queue)
  145. ? (
  146. <ul>
  147. {
  148. (this.props.songExists)
  149. ? (
  150. <li>
  151. <p>{ this.props.songTitle }</p>
  152. <p>{ this.props.songDuration }</p>
  153. <hr/>
  154. </li>
  155. ) : null
  156. }
  157. {
  158. this.state.queue.map((song) => {
  159. return (
  160. <li key={ song.songId }>
  161. <p>{ song.title }</p>
  162. <p>{ song.duration }</p>
  163. <a href={ `/u/${ this.state.userIdMap[`Z${ song.requestedBy }`] }` }>{ this.state.userIdMap[`Z${ song.requestedBy }`] }</a>
  164. <p onClick={ () => { this.deleteSong(song.songId) } }>Delete</p>
  165. <hr/>
  166. </li>
  167. );
  168. })
  169. }
  170. </ul>
  171. )
  172. : null
  173. }
  174. <button onClick={ this.addSongToQueue }>Add song to queue</button>
  175. <hr/>
  176. <ul>
  177. {
  178. this.state.playlists.map((playlist) => {
  179. return (
  180. <li key={ playlist._id }>
  181. { playlist.displayName } - { this.getPlaylistAction(playlist._id) }
  182. </li>
  183. )
  184. })
  185. }
  186. </ul>
  187. {
  188. (this.props.playlistSelectedId)
  189. ? <button onClick={ this.deselectAll }>Deselect all playlists</button>
  190. : null
  191. }
  192. </div>
  193. );
  194. }
  195. }