EditPlaylist.jsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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, closeOverlay3 } 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. addSongToPlaylistCallback = (songId) => {
  104. io.getSocket((socket) => {
  105. // Add song to the playlist
  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. this.props.dispatch(closeOverlay3());
  113. });
  114. });
  115. };
  116. addSongToPlaylist = () => {
  117. this.props.dispatch(openOverlay3("searchYouTube", this.addSongToPlaylistCallback));
  118. };
  119. changeDisplayName = () => {
  120. this.messages.clearErrorSuccess();
  121. if (CustomInput.hasInvalidInput(this.input, ["displayName"])) {
  122. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  123. } else {
  124. io.getSocket(socket => {
  125. socket.emit("playlists.updateDisplayName", this.props.playlistId, this.input.displayName.getValue(), res => {
  126. if (res.status === "success") {
  127. this.messages.clearAddSuccess("Successfully changed name.");
  128. } else {
  129. this.messages.addError(res.message);
  130. }
  131. });
  132. });
  133. }
  134. };
  135. deletePlaylist = () => {
  136. this.messages.clearErrorSuccess();
  137. io.getSocket(socket => {
  138. socket.emit("playlists.remove", this.props.playlistId, res => {
  139. if (res.status === "success") {
  140. this.messages.clearAddSuccess("Successfully deleted playlist.");
  141. this.props.dispatch(closeOverlay2());
  142. } else {
  143. this.messages.addError(res.message);
  144. }
  145. });
  146. });
  147. };
  148. deleteSong = (songId) => {
  149. this.messages.clearErrorSuccess();
  150. io.getSocket(socket => {
  151. socket.emit('playlists.removeSongFromPlaylist', songId, this.props.playlistId, res => {
  152. if (res.status === "success") {
  153. this.messages.clearAddSuccess("Successfully removed song.");
  154. } else {
  155. this.messages.addError(res.message);
  156. }
  157. });
  158. });
  159. };
  160. promoteSong = (songId) => {
  161. this.messages.clearErrorSuccess();
  162. io.getSocket(socket => {
  163. socket.emit('playlists.moveSongToTop', this.props.playlistId, songId, res => {
  164. if (res.status === "success") {
  165. this.messages.clearAddSuccess("Successfully moved song up.");
  166. } else {
  167. this.messages.addError(res.message);
  168. }
  169. });
  170. });
  171. };
  172. demoteSong = (songId) => {
  173. this.messages.clearErrorSuccess();
  174. io.getSocket(socket => {
  175. socket.emit('playlists.moveSongToBottom', this.props.playlistId, songId, res => {
  176. if (res.status === "success") {
  177. this.messages.clearAddSuccess("Successfully moved song up.");
  178. } else {
  179. this.messages.addError(res.message);
  180. }
  181. });
  182. });
  183. };
  184. close = () => {
  185. this.props.dispatch(closeOverlay2());
  186. };
  187. render() {
  188. return (
  189. <div className="overlay">
  190. <button onClick={ this.close }>Back</button>
  191. <h1>Edit Playlist</h1>
  192. <CustomInput type="playlistDescription" name="displayName" label="Display name" placeholder="Display name" onRef={ ref => (this.input.displayName = ref) } />
  193. <button onClick={ this.changeDisplayName }>Change displayname</button>
  194. {
  195. (this.state.gotPlaylist)
  196. ? (
  197. <ul>
  198. {
  199. this.state.playlist.songs.map((song) => {
  200. return (
  201. <li key={ song.songId }>
  202. <p>{ song.title }</p>
  203. <span onClick={ () => { this.deleteSong(song.songId) }}>DELETE</span><br/>
  204. <span onClick={ () => { this.promoteSong(song.songId) }}>UP</span><br/>
  205. <span onClick={ () => { this.demoteSong(song.songId) }}>DOWN</span>
  206. </li>
  207. );
  208. })
  209. }
  210. </ul>
  211. )
  212. : null
  213. }
  214. <button onClick={ this.addSongToPlaylist }>Add song to playlist</button>
  215. <CustomErrors onRef={ ref => (this.messages = ref) } />
  216. <button onClick={ this.deletePlaylist }>Delete this playlist</button>
  217. </div>
  218. );
  219. }
  220. }