import React, { Component } from 'react'; import Wikipedia from './areainfo/Wikipedia'; import { Col, Tabs, Tab,} from 'react-bootstrap'; import axios from 'axios'; import Linkify from "react-linkify"; import Card from "react-bootstrap/lib/Card"; import moment from 'moment'; import momentDurationFormat from 'moment-duration-format'; class VideoInfo extends Component { constructor(props) { super(props); this.state = { error: null, isLoaded: false, VideoDetails: {}, videoId: '0J2QdDbelmY', comments: [] }; this.momentDuration = this.momentDuration.bind(this); } momentDuration(duration){ return moment.duration(duration).format('hh:mm:ss'); } // componentWillMount() { // if (this.props.match.params.id) { // if (this.props.match.params.id === this.state.videoId) { } // else { // this.setState({ videoId: this.props.match.params.id }); // } // } // } componentDidMount() { console.log('didmount',this.state.videoId) axios.all([ axios.get('https://www.googleapis.com/youtube/v3/videos', { //Richiesta per tutte le info sul video params: { 'part': 'snippet,contentDetails,statistics,topicDetails', 'id': this.state.videoId, 'key': 'AIzaSyA21Odw9UdfiOJnxvA_eDfOW0OxuUfHKig' } }), axios.get('https://www.googleapis.com/youtube/v3/commentThreads', { //Richiesta per ottenere i commenti del video params: { 'part': 'snippet', 'videoId': this.state.videoId, 'order': 'relevance', 'key': 'AIzaSyA21Odw9UdfiOJnxvA_eDfOW0OxuUfHKig' } })]) .then(axios.spread((videosResponse, commentThreadResponse) => { this.setState({ VideoDetails: videosResponse.data.items, comments: commentThreadResponse.data.items, isLoaded: true }); }), error => { console.error(error); this.setState({ isLoaded: true, error }); }); } componentDidUpdate(prevProps, prevState, snapshot) { console.info('didup', this.props.match.params.id) // this.setState((state, props) => { return { }}); if (this.props.match.params.id && !(prevProps.match.params.id === this.props.match.params.id)) { this.setState({ videoId: this.props.match.params.id, isLoaded: false }); axios.all([ axios.get('https://www.googleapis.com/youtube/v3/videos', { //Richiesta per tutte le info sul video params: { 'part': 'snippet,contentDetails,statistics,topicDetails', 'id': this.props.match.params.id, 'key': 'AIzaSyA21Odw9UdfiOJnxvA_eDfOW0OxuUfHKig' } }), axios.get('https://www.googleapis.com/youtube/v3/commentThreads', { //Richiesta per ottenere i commenti del video params: { 'part': 'snippet', 'videoId': this.props.match.params.id, 'order': 'relevance', 'key': 'AIzaSyA21Odw9UdfiOJnxvA_eDfOW0OxuUfHKig' } })]) .then(axios.spread((videosResponse, commentThreadResponse) => { this.setState({ VideoDetails: videosResponse.data.items, comments: commentThreadResponse.data.items, isLoaded: true }); }), error => { console.error(error); this.setState({ isLoaded: true, error }); }); } } // shouldComponentUpdate(nextProps, nextState){ // console.log('shouldup', nextProps, nextState) // if(nextProps.match.params.id) // return console.log('a',!(nextProps.match.params.id === this.state.videoId)) && !(nextProps.match.params.id === this.state.videoId) ; // else if(!(nextState.videoId === this.state.videoId)) // return console.log('b') && true; // else if(nextState.isLoaded) // return console.log('d') && true; // else // return console.log('c') && false; // } // componentWillReceiveProps(nextProps){ // if (nextProps.match.params.id === this.state.videoId) { console.log('1',nextProps)} // else if (nextProps.match.params.id) { // console.log('2',nextProps) // this.setState({ videoId: nextProps.match.params.id }); // axios.all([ // axios.get('https://www.googleapis.com/youtube/v3/videos', { //Richiesta per tutte le info sul video // params: { // 'part': 'snippet,contentDetails,statistics,topicDetails', // 'id': nextProps.match.params.id, // 'key': 'AIzaSyA21Odw9UdfiOJnxvA_eDfOW0OxuUfHKig' // } // }), axios.get('https://www.googleapis.com/youtube/v3/commentThreads', { //Richiesta per ottenere i commenti del video // params: { // 'part': 'snippet', // 'videoId': nextProps.match.params.id, // 'order': 'relevance', // 'key': 'AIzaSyA21Odw9UdfiOJnxvA_eDfOW0OxuUfHKig' // } // })]) // .then(axios.spread((videosResponse, commentThreadResponse) => { // this.setState({ // VideoDetails: videosResponse.data.items, // comments: commentThreadResponse.data.items, // isLoaded: true // }); // }), error => { // console.error(error); // this.setState({ // isLoaded: true, // error // }); // }); // } // else{ // console.log('3',nextProps) // } // } render() { const { error, isLoaded, VideoDetails, comments } = this.state; const momentDuration = this.momentDuration; // bind momentDuration in render to the actual method if (error) { return ( Error: {error.message} -- Cannot get {error.config.url} ); } else if (!isLoaded) { return Loading...; } else { return ( {/* codice per tabs reccomenders */}
{this.state.VideoDetails.map(function(item,index){ return(
{item.snippet.title}

ID Video : {item.id}
Publish Time : {item.snippet.publishedAt.replace(/(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+).000Z/,function(match,p1,p2,p3,p4,p5,p6){ return(p3+'/'+p2+'/'+p1+' - '+p4+':'+p5+':'+p6); })}
Channel Name : {item.snippet.channelTitle}
Description :
{item.snippet.description}
Tags :
{item.snippet.tags.map(function(element,index){ if(index===4) return (
); return( {element} ) })}

) } )}
{this.state.VideoDetails.map(function (item,index) { return(
{item.snippet.title}

Duration :{ momentDuration(item.contentDetails.duration) }
Definition : {item.contentDetails.definition}
Dimension : {item.contentDetails.dimension}
Views : {item.statistics.viewCount}
Likes : {item.statistics.likeCount}
Dislikes : {item.statistics.dislikeCount}

) })}
{this.state.comments.map(function (item,index) { return(
{item.snippet.topLevelComment.snippet.authorDisplayName} {item.snippet.topLevelComment.snippet.textDisplay}
) })}
); } } } VideoInfo.propTypes = { }; export default VideoInfo;