index.jsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. thumbnail: result.snippet.thumbnails.default.url,
  37. };
  38. });
  39. this.setState({
  40. gotResults: true,
  41. results,
  42. });
  43. } else {
  44. this.messages.addError(res.message);
  45. }
  46. });
  47. });
  48. }
  49. };
  50. close = () => {
  51. if (this.props.order === 3) {
  52. this.props.dispatch(closeOverlay3());
  53. } else {
  54. this.props.dispatch(closeOverlay2());
  55. }
  56. };
  57. render() {
  58. return (
  59. <div className="overlay">
  60. <button onClick={ this.close } className="back"><i className="material-icons">arrow_back</i></button>
  61. <div className="content">
  62. <h1>Search</h1>
  63. <CustomInput type="youTubeSearchQuery" showLabel={true} name="query" label="YouTube search query" placeholder="YouTube search query" onRef={ ref => (this.input.query = ref) } />
  64. <button onClick={ this.search }>Search</button>
  65. {
  66. (this.state.gotResults)
  67. ? (
  68. <div className="search-youtube-results">
  69. <h2>Results</h2>
  70. <SongList songs={ this.state.results } callback={ this.props.callback }/>
  71. </div>
  72. )
  73. : null
  74. }
  75. <CustomErrors onRef={ ref => (this.messages = ref) } />
  76. </div>
  77. </div>
  78. );
  79. }
  80. }