EditPlaylist.jsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 { closeOverlay2, openOverlay3 } from "actions/stationOverlay";
  7. import io from "io";
  8. @connect(state => ({
  9. stationId: state.station.get("id"),
  10. }))
  11. export default class EditPlaylist extends Component {
  12. constructor(props) {
  13. super(props);
  14. CustomInput.initialize(this);
  15. this.state = {
  16. gotPlaylist: false,
  17. playlist: {},
  18. };
  19. io.getSocket((socket) => {
  20. socket.emit('playlists.getPlaylist', this.props.playlistId, res => {
  21. if (res.status === 'success') {
  22. this.input.displayName.setValue(res.data.displayName, true);
  23. this.setState({
  24. gotPlaylist: true,
  25. playlist: res.data,
  26. });
  27. }
  28. });
  29. socket.on('event:playlist.addSong', data => {
  30. if (this.props.playlistId === data.playlistId) {
  31. let songs = this.state.playlist.songs;
  32. songs.push(data.song);
  33. this.setState({
  34. playlist: {
  35. ...this.state.playlist,
  36. songs,
  37. },
  38. });
  39. }
  40. });
  41. socket.on('event:playlist.updateDisplayName', data => {
  42. if (this.props.playlistId === data.playlistId) {
  43. this.setState({
  44. playlist: {
  45. ...this.state.playlist,
  46. displayName: data.displayName,
  47. },
  48. });
  49. }
  50. });
  51. socket.on('event:playlist.moveSongToBottom', data => {
  52. if (this.props.playlistId === data.playlistId) {
  53. let songs = this.state.playlist.songs;
  54. let songIndex;
  55. songs.forEach((song, index) => {
  56. if (song.songId === data.songId) songIndex = index;
  57. });
  58. let song = songs.splice(songIndex, 1)[0];
  59. songs.push(song);
  60. this.setState({
  61. playlist: {
  62. ...this.state.playlist,
  63. songs,
  64. },
  65. });
  66. }
  67. });
  68. socket.on('event:playlist.moveSongToTop', (data) => {
  69. if (this.props.playlistId === data.playlistId) {
  70. let songs = this.state.playlist.songs;
  71. let songIndex;
  72. songs.forEach((song, index) => {
  73. if (song.songId === data.songId) songIndex = index;
  74. });
  75. let song = songs.splice(songIndex, 1)[0];
  76. songs.unshift(song);
  77. this.setState({
  78. playlist: {
  79. ...this.state.playlist,
  80. songs,
  81. },
  82. });
  83. }
  84. });
  85. socket.on('event:playlist.removeSong', data => {
  86. if (this.props.playlistId === data.playlistId) {
  87. //TODO Somehow make this sync, so when 2 songs get removed at the same ms it removes both not just one
  88. let songs = this.state.playlist.songs;
  89. songs.forEach((song, index) => {
  90. if (song.songId === data.songId) songs.splice(index, 1);
  91. });
  92. this.setState({
  93. playlist: {
  94. ...this.state.playlist,
  95. songs,
  96. },
  97. });
  98. }
  99. });
  100. });
  101. console.log("edit Playlist", props);
  102. }
  103. addSongToQueueCallback = (songId) => {
  104. io.getSocket((socket) => {
  105. // Add song to queue
  106. socket.emit("playlists.addSongToPlaylist", songId, this.props.playlistId, res => {
  107. if (res.status === "success") {
  108. this.messages.clearAddSuccess("Successfully added song.");
  109. } else {
  110. this.messages.addError(res.message);
  111. }
  112. });
  113. });
  114. };
  115. addSongToQueue = () => {
  116. this.props.dispatch(openOverlay3("searchYouTube", this.addSongToQueueCallback));
  117. };
  118. changeDisplayName = () => {
  119. this.messages.clearErrorSuccess();
  120. if (CustomInput.hasInvalidInput(this.input, ["displayName"])) {
  121. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  122. } else {
  123. io.getSocket(socket => {
  124. socket.emit("playlists.updateDisplayName", this.props.playlistId, this.input.displayName.getValue(), res => {
  125. if (res.status === "success") {
  126. this.messages.clearAddSuccess("Successfully changed name.");
  127. } else {
  128. this.messages.addError(res.message);
  129. }
  130. });
  131. });
  132. }
  133. };
  134. deletePlaylist = () => {
  135. this.messages.clearErrorSuccess();
  136. io.getSocket(socket => {
  137. socket.emit("playlists.remove", this.props.playlistId, res => {
  138. if (res.status === "success") {
  139. this.messages.clearAddSuccess("Successfully deleted playlist.");
  140. this.props.dispatch(closeOverlay2());
  141. } else {
  142. this.messages.addError(res.message);
  143. }
  144. });
  145. });
  146. };
  147. deleteSong = (songId) => {
  148. this.messages.clearErrorSuccess();
  149. io.getSocket(socket => {
  150. socket.emit('playlists.removeSongFromPlaylist', songId, this.props.playlistId, res => {
  151. if (res.status === "success") {
  152. this.messages.clearAddSuccess("Successfully removed song.");
  153. } else {
  154. this.messages.addError(res.message);
  155. }
  156. });
  157. });
  158. };
  159. promoteSong = (songId) => {
  160. this.messages.clearErrorSuccess();
  161. io.getSocket(socket => {
  162. socket.emit('playlists.moveSongToTop', this.props.playlistId, songId, res => {
  163. if (res.status === "success") {
  164. this.messages.clearAddSuccess("Successfully moved song up.");
  165. } else {
  166. this.messages.addError(res.message);
  167. }
  168. });
  169. });
  170. };
  171. demoteSong = (songId) => {
  172. this.messages.clearErrorSuccess();
  173. io.getSocket(socket => {
  174. socket.emit('playlists.moveSongToBottom', this.props.playlistId, songId, res => {
  175. if (res.status === "success") {
  176. this.messages.clearAddSuccess("Successfully moved song up.");
  177. } else {
  178. this.messages.addError(res.message);
  179. }
  180. });
  181. });
  182. };
  183. close = () => {
  184. this.props.dispatch(closeOverlay2());
  185. };
  186. render() {
  187. return (
  188. <div className="overlay">
  189. <button onClick={ this.close }>Back</button>
  190. <h1>Edit Playlist</h1>
  191. <CustomInput type="playlistDescription" name="displayName" label="Display name" placeholder="Display name" onRef={ ref => (this.input.displayName = ref) } />
  192. <button onClick={ this.changeDisplayName }>Change displayname</button>
  193. {
  194. (this.state.gotPlaylist)
  195. ? (
  196. <ul>
  197. {
  198. this.state.playlist.songs.map((song) => {
  199. return (
  200. <li key={ song.songId }>
  201. <p>{ song.title }</p>
  202. <span onClick={ () => { this.deleteSong(song.songId) }}>DELETE</span><br/>
  203. <span onClick={ () => { this.promoteSong(song.songId) }}>UP</span><br/>
  204. <span onClick={ () => { this.demoteSong(song.songId) }}>DOWN</span>
  205. </li>
  206. );
  207. })
  208. }
  209. </ul>
  210. )
  211. : null
  212. }
  213. <button onClick={ this.addSongToQueue }>Add song to queue</button>
  214. <CustomErrors onRef={ ref => (this.messages = ref) } />
  215. <button onClick={ this.deletePlaylist }>Delete this playlist</button>
  216. </div>
  217. );
  218. }
  219. }