SearchYouTube.jsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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, closeOverlay2 } 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. if (this.props.order === 3) {
  51. this.props.dispatch(closeOverlay3());
  52. } else {
  53. this.props.dispatch(closeOverlay2());
  54. }
  55. };
  56. render() {
  57. return (
  58. <div className="overlay">
  59. <button onClick={ this.close } className="back"><i className="material-icons">arrow_back</i></button>
  60. <div className="content">
  61. <h1>Search</h1>
  62. <CustomInput type="youTubeSearchQuery" showLabel={true} name="query" label="YouTube search query" placeholder="YouTube search query" onRef={ ref => (this.input.query = ref) } />
  63. <button onClick={ this.search }>Search</button>
  64. {
  65. (this.state.gotResults)
  66. ? (
  67. <div className="search-youtube-results">
  68. <h2>Results</h2>
  69. <ul>
  70. {
  71. this.state.results.map((result) => {
  72. return (
  73. <li key={ this.input.query.getValue() + result.songId }>
  74. <img src={ result.thumbnail }/>
  75. <a href={ result.url } target="_blank">{ result.title }</a>
  76. <div>
  77. <span className="duration">12:12</span>
  78. <span onClick={ () => { this.props.callback(result.songId); } } className="add" tabIndex="0"><i className="material-icons">add</i></span>
  79. </div>
  80. </li>
  81. );
  82. })
  83. }
  84. </ul>
  85. </div>
  86. )
  87. : null
  88. }
  89. <ul>
  90. {}
  91. </ul>
  92. <CustomErrors onRef={ ref => (this.messages = ref) } />
  93. </div>
  94. </div>
  95. );
  96. }
  97. }