Playlists.jsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. res.data.push({
  21. displayName: "Playlist",
  22. _id: "RandomId",
  23. });
  24. console.log("Remove above dummy data.");
  25. if (res.status === 'success') this.setState({
  26. playlists: res.data,
  27. });
  28. });
  29. });
  30. }
  31. close = () => {
  32. this.props.dispatch(closeOverlay1());
  33. };
  34. createPlaylist = () => {
  35. this.messages.clearErrorSuccess();
  36. if (CustomInput.hasInvalidInput(this.input, ["description"])) {
  37. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  38. } else {
  39. io.getSocket(socket => {
  40. socket.emit('playlists.create', { displayName: this.input.description.getValue(), songs: [] }, res => {
  41. if (res.status === "success") {
  42. this.props.dispatch(openOverlay2("editPlaylist", { playlistId: res.playlistId }));
  43. } else {
  44. this.messages.addError(res.message);
  45. }
  46. });
  47. });
  48. }
  49. };
  50. render() {
  51. return (
  52. <div className="overlay">
  53. <button onClick={ this.close }>Back</button>
  54. <h1>Playlists</h1>
  55. <CustomErrors onRef={ ref => (this.messages = ref) } />
  56. <CustomInput key="description" type="playlistDescription" name="description" label="Description" placeholder="Description" original={ this.props.description } onRef={ ref => (this.input.description = ref) } />
  57. <button onClick={ this.createPlaylist }>Create playlist</button>
  58. <h2>Playlists</h2>
  59. <ul>
  60. {
  61. this.state.playlists.map((playlist) => {
  62. return <li key={ playlist._id }>{ playlist.displayName } - <span onClick={ () => { this.props.dispatch(openOverlay2("editPlaylist", { playlistId: playlist._id })) } }>Edit</span></li>;
  63. })
  64. }
  65. </ul>
  66. </div>
  67. );
  68. }
  69. }