SearchYouTube.jsx 2.4 KB

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