EditPlaylist.jsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. playlists: state.station.playlists,
  10. }))
  11. export default class EditPlaylist extends Component {
  12. constructor(props) {
  13. super(props);
  14. CustomInput.initialize(this);
  15. }
  16. addSongToPlaylistCallback = (songId) => {
  17. io.getSocket((socket) => {
  18. // Add song to the playlist
  19. socket.emit("playlists.addSongToPlaylist", songId, this.props.playlistId, res => {
  20. if (res.status === "success") {
  21. this.messages.clearAddSuccess("Successfully added song.");
  22. } else {
  23. this.messages.addError(res.message);
  24. }
  25. this.props.dispatch(closeOverlay3());
  26. });
  27. });
  28. };
  29. addSongToPlaylist = () => {
  30. this.props.dispatch(openOverlay3("searchYouTube", this.addSongToPlaylistCallback));
  31. };
  32. changeDisplayName = () => {
  33. this.messages.clearErrorSuccess();
  34. if (CustomInput.hasInvalidInput(this.input, ["displayName"])) {
  35. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  36. } else {
  37. io.getSocket(socket => {
  38. socket.emit("playlists.updateDisplayName", this.props.playlistId, this.input.displayName.getValue(), res => {
  39. if (res.status === "success") {
  40. this.messages.clearAddSuccess("Successfully changed name.");
  41. } else {
  42. this.messages.addError(res.message);
  43. }
  44. });
  45. });
  46. }
  47. };
  48. deletePlaylist = () => {
  49. this.messages.clearErrorSuccess();
  50. io.getSocket(socket => {
  51. socket.emit("playlists.remove", this.props.playlistId, res => {
  52. if (res.status === "success") {
  53. this.messages.clearAddSuccess("Successfully deleted playlist.");
  54. this.props.dispatch(closeOverlay2());
  55. } else {
  56. this.messages.addError(res.message);
  57. }
  58. });
  59. });
  60. };
  61. deleteSong = (songId) => {
  62. this.messages.clearErrorSuccess();
  63. io.getSocket(socket => {
  64. socket.emit('playlists.removeSongFromPlaylist', songId, this.props.playlistId, res => {
  65. if (res.status === "success") {
  66. this.messages.clearAddSuccess("Successfully removed song.");
  67. } else {
  68. this.messages.addError(res.message);
  69. }
  70. });
  71. });
  72. };
  73. promoteSong = (songId) => {
  74. this.messages.clearErrorSuccess();
  75. io.getSocket(socket => {
  76. socket.emit('playlists.moveSongToTop', this.props.playlistId, songId, res => {
  77. if (res.status === "success") {
  78. this.messages.clearAddSuccess("Successfully moved song up.");
  79. } else {
  80. this.messages.addError(res.message);
  81. }
  82. });
  83. });
  84. };
  85. demoteSong = (songId) => {
  86. this.messages.clearErrorSuccess();
  87. io.getSocket(socket => {
  88. socket.emit('playlists.moveSongToBottom', this.props.playlistId, songId, res => {
  89. if (res.status === "success") {
  90. this.messages.clearAddSuccess("Successfully moved song up.");
  91. } else {
  92. this.messages.addError(res.message);
  93. }
  94. });
  95. });
  96. };
  97. close = () => {
  98. this.props.dispatch(closeOverlay2());
  99. };
  100. render() {
  101. const { playlistId } = this.props;
  102. const playlist = this.props.playlists.find((playlist) => {
  103. return playlist.get("playlistId") === playlistId;
  104. });
  105. if (!playlist) return null;
  106. return (
  107. <div className="overlay">
  108. <button onClick={ this.close }>Back</button>
  109. <h1>Edit Playlist</h1>
  110. <CustomInput type="playlistDescription" name="displayName" label="Display name" placeholder="Display name" onRef={ ref => (this.input.displayName = ref) } original={ playlist.get("displayName") }/>
  111. <button onClick={ this.changeDisplayName }>Change displayname</button>
  112. {
  113. <ul>
  114. {
  115. playlist.get("songs").map((song) => {
  116. return (
  117. <li key={ song.get("songId") }>
  118. <p>{ song.get("title") }</p>
  119. <span onClick={ () => { this.deleteSong(song.get("songId")) }}>DELETE</span><br/>
  120. <span onClick={ () => { this.promoteSong(song.get("songId")) }}>UP</span><br/>
  121. <span onClick={ () => { this.demoteSong(song.get("songId")) }}>DOWN</span>
  122. </li>
  123. );
  124. })
  125. }
  126. </ul>
  127. }
  128. <button onClick={ this.addSongToPlaylist }>Add song to playlist</button>
  129. <CustomErrors onRef={ ref => (this.messages = ref) } />
  130. <button onClick={ this.deletePlaylist }>Delete this playlist</button>
  131. </div>
  132. );
  133. }
  134. }