Guest User

Untitled

a guest
Feb 17th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import SearchBar from './Components/SearchBar';
  3. import VideoList from './Components/VideoList';
  4. import VideoDetail from './Components/VideoDetail';
  5. import YTSearch from 'youtube-api-search';
  6. import './App.css';
  7. import { Icon, notification } from 'antd';
  8. import dotenv from 'dotenv';
  9. dotenv.config();
  10.  
  11. const API_KEY = process.env.REACT_APP_API_KEY;
  12.  
  13. class App extends Component {
  14. constructor( props ) {
  15. super(props);
  16. this.state = {
  17. videos: [],
  18. search: true,
  19. selectedVideo: {}
  20. };
  21.  
  22. this.welcome();
  23. }
  24.  
  25. welcome = () => {
  26.  
  27. notification.open({
  28. message: 'Hey nice to see you here',
  29. description: 'Let us start by searching for some videos',
  30. icon: <Icon type="smile" style={{ color: '#108ee9' }} />
  31. })
  32. };
  33.  
  34. videoSearch( term ) {
  35. if( this.state.search ) {
  36. YTSearch({ key: API_KEY, term }, (data) => {
  37. try {
  38. if( data && data.data && data.data.error.message ) {
  39. console.log(data);
  40. throw ('error')
  41. }
  42. this.setState({ videos: data, selectedVideo: data[0] });
  43. console.log( this.state.videos );
  44. } catch( err ){
  45. notification['error']({
  46. message: "Daily Limit Exceeded",
  47. description: "Youtube data API daily limit have exceeded. Quota will be recharged at 1:30pm IST. Wait till then.",
  48. })
  49. }
  50.  
  51. });
  52. }
  53.  
  54. }
  55.  
  56.  
  57. handleChange = (value) => {
  58. setTimeout( () => {
  59. if( value === ''){
  60. this.setState({ videos: [], selectedVideo: null });
  61. return;
  62. }
  63.  
  64. if( this.state.search ) {
  65. this.videoSearch( value );
  66. }
  67.  
  68. setTimeout( () => {
  69. this.setState({ search: true });
  70. }, 5000);
  71.  
  72. }, 2000);
  73.  
  74. };
  75.  
  76. render() {
  77. return (
  78. <div style={{ "display": "flex", "flexDirection": "column" }}>
  79. <div style={{ "display": "flex", "justifyContent": "space-between", "background": "#123456"}}>
  80. <h1 style={{ "color": "#fff", "alignSelf": "center", "flexBasis": "4", "paddingTop": "20px", "paddingLeft": "30px" }}>YTSearch <Icon type={"search"}/></h1>
  81. <SearchBar videos={ this.state.videos } video={ this.state.selectedVideo } onChange={ this.handleChange } handleSearch={ (video) => { this.setState({ selectedVideo: this.state.videos[video], search: false })}}/>
  82. </div>
  83. <div style={{ "display" : "flex" }}>
  84. <VideoDetail video={ this.state.selectedVideo }/>
  85. <VideoList
  86. videos={ this.state.videos }
  87. onVideoSelect={ ( userSelected ) => { this.setState({ selectedVideo: this.state.videos[ userSelected ] }) }}
  88. />
  89. </div>
  90. </div>
  91. );
  92. }
  93. }
  94.  
  95. export default App;
Add Comment
Please, Sign In to add comment