index.jsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 SongList from "./SongList.jsx";
  6. import { connect } from "react-redux";
  7. import { closeOverlay3, closeOverlay2 } from "actions/stationOverlay";
  8. import io from "io";
  9. @connect(state => ({
  10. callback: state.stationOverlay.get("callback"),
  11. }))
  12. export default class SearchYouTube extends Component {
  13. constructor(props) {
  14. super(props);
  15. CustomInput.initialize(this);
  16. this.state = {
  17. results: null,
  18. gotResults: false,
  19. };
  20. io.getSocket((socket) => {
  21. });
  22. }
  23. search = () => {
  24. this.messages.clearErrorSuccess();
  25. if (CustomInput.hasInvalidInput(this.input, ["query"])) {
  26. this.messages.clearAddError(this.props.t("general:someFieldsAreIncorrectError"));
  27. } else {
  28. io.getSocket(socket => {
  29. socket.emit('apis.searchYoutube', this.input.query.getValue(), res => {
  30. if (res.status === "success") {
  31. let results = res.data.items.map((result) => {
  32. return {
  33. songId: result.id.videoId,
  34. url: `https://www.youtube.com/watch?v=${ result.id.videoId }`,
  35. title: result.snippet.title,
  36. duration: result.duration,
  37. thumbnail: result.snippet.thumbnails.default.url,
  38. };
  39. });
  40. this.setState({
  41. gotResults: true,
  42. results,
  43. });
  44. } else {
  45. this.messages.addError(res.message);
  46. }
  47. });
  48. });
  49. }
  50. };
  51. close = () => {
  52. if (this.props.order === 3) {
  53. this.props.dispatch(closeOverlay3());
  54. } else {
  55. this.props.dispatch(closeOverlay2());
  56. }
  57. };
  58. render() {
  59. return (
  60. <div className="overlay">
  61. <button onClick={ this.close } className="back"><i className="material-icons">arrow_back</i></button>
  62. <div className="content">
  63. <h1>Search</h1>
  64. <CustomInput type="youTubeSearchQuery" showLabel={true} name="query" label="YouTube search query" placeholder="YouTube search query" onRef={ ref => (this.input.query = ref) } />
  65. <button onClick={ this.search }>Search</button>
  66. {
  67. (this.state.gotResults)
  68. ? (
  69. <div className="search-youtube-results">
  70. <h2>Results</h2>
  71. <SongList songs={ this.state.results } callback={ this.props.callback }/>
  72. </div>
  73. )
  74. : null
  75. }
  76. <CustomErrors onRef={ ref => (this.messages = ref) } />
  77. </div>
  78. </div>
  79. );
  80. }
  81. }