QueueList.jsx 6.3 KB

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