Playlists.jsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 } from "actions/stationOverlay";
  7. import io from "io";
  8. @connect(state => ({
  9. user: {
  10. userId: state.user.get("userId"),
  11. role: state.user.get("role"),
  12. },
  13. loggedIn: state.user.get("loggedIn"),
  14. stationId: state.station.get("id"),
  15. stationOwner: state.station.get("ownerId"),
  16. partyEnabled: state.station.get("partyMode"),
  17. queueLocked: state.station.get("locked"),
  18. privatePlaylist: state.station.get("privatePlaylist"),
  19. }))
  20. export default class Playlists extends Component {
  21. constructor(props) {
  22. super(props);
  23. CustomInput.initialize(this);
  24. this.state = {
  25. playlists: [],
  26. mode: this.getModeTemp(props.partyEnabled, props.queueLocked),
  27. };
  28. io.getSocket((socket) => {
  29. socket.emit('playlists.indexForUser', res => {
  30. if (res.status === 'success') this.setState({
  31. playlists: res.data,
  32. });
  33. });
  34. });
  35. }
  36. close = () => {
  37. this.props.dispatch(closeOverlay1());
  38. };
  39. createPlaylist = () => {
  40. this.messages.clearErrorSuccess();
  41. if (CustomInput.hasInvalidInput(this.input, ["description"])) {
  42. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  43. } else {
  44. io.getSocket(socket => {
  45. socket.emit('playlists.create', { displayName: this.input.description.getValue(), songs: [] }, res => {
  46. if (res.status === "success") {
  47. this.props.dispatch(openOverlay2("editPlaylist", { playlistId: res.playlistId }));
  48. } else {
  49. this.messages.addError(res.message);
  50. }
  51. });
  52. });
  53. }
  54. };
  55. getModeTemp = (partyEnabled, queueLocked) => {
  56. // If party enabled
  57. // If queue locked
  58. // Mode is DJ
  59. // If queue not locked
  60. // Mode party
  61. // If party not enabled
  62. // Mode is normal
  63. if (partyEnabled) {
  64. if (queueLocked) return "dj";
  65. else return "party";
  66. } else return "normal";
  67. };
  68. getPlaylistAction = (playlistId) => {
  69. if (this.state.mode === "normal") {
  70. if (this.isOwner()) {
  71. const play = <span onClick={ () => { this.playPlaylist(playlistId) } }>PLAY!</span>;
  72. //const stop = <span onClick={ () => { this.stopPlaylist(playlistId) } }>STOP!</span>;
  73. const stop = null; // There's currently no backend functionality to stop a playlist from playing
  74. if (this.props.privatePlaylist === playlistId) return stop;
  75. else return play;
  76. }
  77. }
  78. return null;
  79. };
  80. isOwner = () => {
  81. if (this.props.loggedIn) {
  82. if (this.props.user.userId === this.props.stationOwner) return true;
  83. }
  84. return false;
  85. };
  86. playPlaylist = (playlistId) => {
  87. this.messages.clearErrorSuccess();
  88. io.getSocket((socket) => {
  89. socket.emit("stations.selectPrivatePlaylist", this.props.stationId, playlistId, (res) => {
  90. if (res.status === "success") {
  91. this.message.addSuccess("Successfully selected private playlist.");
  92. } else {
  93. this.messages.addError(res.message);
  94. }
  95. });
  96. });
  97. };
  98. /*stopPlaylist = (playlistId) => {
  99. this.messages.clearErrorSuccess();
  100. io.getSocket((socket) => {
  101. socket.emit("stations.selectPrivatePlaylist", this.props.stationId, playlistId, (res) => {
  102. if (res.status === "success") {
  103. this.message.addSuccess("Successfully selected private playlist.");
  104. } else {
  105. this.messages.addError(res.message);
  106. }
  107. });
  108. });
  109. };*/
  110. render() {
  111. return (
  112. <div className="overlay">
  113. <button onClick={ this.close }>Back</button>
  114. <h1>Playlists</h1>
  115. <CustomErrors onRef={ ref => (this.messages = ref) } />
  116. <CustomInput key="description" type="playlistDescription" name="description" label="Description" placeholder="Description" original={ this.props.description } onRef={ ref => (this.input.description = ref) } />
  117. <button onClick={ this.createPlaylist }>Create playlist</button>
  118. <h2>Playlists</h2>
  119. <ul>
  120. {
  121. this.state.playlists.map((playlist) => {
  122. return (
  123. <li key={ playlist._id }>
  124. { playlist.displayName } - <span onClick={ () => { this.props.dispatch(openOverlay2("editPlaylist", { playlistId: playlist._id })) } }>Edit</span>
  125. { this.getPlaylistAction(playlist._id) }
  126. </li>
  127. )
  128. })
  129. }
  130. </ul>
  131. </div>
  132. );
  133. }
  134. }