Playlists.jsx 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. stationId: state.station.get("id"),
  10. }))
  11. export default class Playlists extends Component {
  12. constructor(props) {
  13. super(props);
  14. CustomInput.initialize(this);
  15. this.state = {
  16. playlists: [],
  17. };
  18. io.getSocket((socket) => {
  19. socket.emit('playlists.indexForUser', res => {
  20. if (res.status === 'success') this.setState({
  21. playlists: res.data,
  22. });
  23. });
  24. });
  25. }
  26. close = () => {
  27. this.props.dispatch(closeOverlay1());
  28. };
  29. createPlaylist = () => {
  30. this.messages.clearErrorSuccess();
  31. if (CustomInput.hasInvalidInput(this.input, ["description"])) {
  32. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  33. } else {
  34. io.getSocket(socket => {
  35. socket.emit('playlists.create', { displayName: this.input.description.getValue(), songs: [] }, res => {
  36. if (res.status === "success") {
  37. this.props.dispatch(openOverlay2("editPlaylist", { playlistId: res.playlistId }));
  38. } else {
  39. this.messages.addError(res.message);
  40. }
  41. });
  42. });
  43. }
  44. };
  45. render() {
  46. return (
  47. <div className="overlay">
  48. <button onClick={ this.close }>Back</button>
  49. <h1>Playlists</h1>
  50. <CustomErrors onRef={ ref => (this.messages = ref) } />
  51. <CustomInput key="description" type="playlistDescription" name="description" label="Description" placeholder="Description" original={ this.props.description } onRef={ ref => (this.input.description = ref) } />
  52. <button onClick={ this.createPlaylist }>Create playlist</button>
  53. <h2>Playlists</h2>
  54. <ul>
  55. {
  56. this.state.playlists.map((playlist) => {
  57. return <li key={ playlist._id }>{ playlist.displayName } - <span onClick={ () => { this.props.dispatch(openOverlay2("editPlaylist", { playlistId: playlist._id })) } }>Edit</span></li>;
  58. })
  59. }
  60. </ul>
  61. </div>
  62. );
  63. }
  64. }