This repository has been archived on 2019-10-19. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
tecnologiabanana/src/reccomenders/Search.js
T

142 lines
4.8 KiB
JavaScript
Raw Normal View History

2019-01-30 21:45:29 +01:00
import React from 'react';
import axios from 'axios';
2019-02-16 17:55:35 +01:00
import { Link } from 'react-router-dom';
2019-03-03 16:30:39 +01:00
import { Col, Media } from 'react-bootstrap';
2019-01-30 21:45:29 +01:00
class Search extends React.Component {
state = {
query: '',
2019-02-14 15:03:45 +01:00
isLoaded: false,
error: null,
2019-01-30 21:45:29 +01:00
res: null
}
2019-02-16 17:55:35 +01:00
// static getDerivedStateFromProps(nextProps, prevState) {
2019-02-16 17:55:35 +01:00
// }
2019-01-30 21:45:29 +01:00
componentDidMount() {
2019-02-16 17:55:35 +01:00
if (new RegExp("[0-9A-Za-z_-]{10}[048AEIMQUYcgkosw]").test(this.props.match.params.query.toString()))
2019-02-14 15:03:45 +01:00
axios.get('https://www.googleapis.com/youtube/v3/videos', {
params: {
'part': 'id',
2019-02-16 17:55:35 +01:00
'id': this.props.match.params.query,
'key': 'AIzaSyA21Odw9UdfiOJnxvA_eDfOW0OxuUfHKig',
2019-02-14 15:03:45 +01:00
'field': 'pageInfo/resultsPerPage'
}
}).then(
res => {
res.data.pageInfo.resultsPerPage ?
2019-02-16 17:55:35 +01:00
this.props.history.push('/video/' + this.props.match.params.query) :
2019-02-14 15:03:45 +01:00
this.youtubeSearch()
},
error => {
console.error(error)
}
)
else
this.youtubeSearch();
2019-01-30 21:45:29 +01:00
}
// shouldComponentUpdate(nextProps, nextState) {
// }
2019-02-12 16:55:46 +01:00
componentDidUpdate(prevProps, prevState) {
2019-02-19 01:16:25 +01:00
if (prevProps.match.params.query !== this.props.match.params.query) {
this.setState({ isLoaded: false })
2019-02-16 17:55:35 +01:00
if (new RegExp("[0-9A-Za-z_-]{10}[048AEIMQUYcgkosw]").test(this.props.match.params.query.toString()))
2019-02-14 15:03:45 +01:00
axios.get('https://www.googleapis.com/youtube/v3/videos', {
params: {
'part': 'id',
2019-02-16 17:55:35 +01:00
'id': this.props.match.params.query,
'key': 'AIzaSyA21Odw9UdfiOJnxvA_eDfOW0OxuUfHKig',
2019-02-14 15:03:45 +01:00
'field': 'pageInfo/resultsPerPage'
}
}).then(
res => {
res.data.pageInfo.resultsPerPage ?
2019-02-16 17:55:35 +01:00
this.props.history.push('/video/' + this.props.match.params.query) :
2019-02-14 15:03:45 +01:00
this.youtubeSearch()
},
error => {
console.error(error)
this.setState({
error
})
}
)
else
this.youtubeSearch();
2019-02-19 01:16:25 +01:00
}
2019-02-12 16:55:46 +01:00
}
2019-01-30 21:45:29 +01:00
// componentWillUnmount() {
// }
youtubeSearch() {
2019-02-12 16:55:46 +01:00
return axios.get('https://www.googleapis.com/youtube/v3/search', {
params: {
'part': 'snippet',
2019-02-16 17:55:35 +01:00
'q': this.props.match.params.query,
2019-02-12 16:55:46 +01:00
'videoEmbeddable': 'true',
'type': 'video',
'maxResults': 30,
'topicId': '/m/04rlf, /m/02jjt',
2019-02-16 17:55:35 +01:00
'key': 'AIzaSyA21Odw9UdfiOJnxvA_eDfOW0OxuUfHKig'
2019-02-12 16:55:46 +01:00
}
}).then(
2019-02-19 01:16:25 +01:00
res =>
2019-02-12 16:55:46 +01:00
this.setState({
2019-02-14 15:03:45 +01:00
res: res.data,
isLoaded: true
2019-02-12 16:55:46 +01:00
})
2019-02-18 03:58:51 +01:00
,
error => this.setState({
error
})
2019-02-12 16:55:46 +01:00
)
2019-01-30 21:45:29 +01:00
}
render() {
2019-02-14 15:03:45 +01:00
if (this.state.error) {
2019-02-18 03:58:51 +01:00
return <React.Fragment>{this.state.error.message} -- {this.state.error.response.data.error.errors[0].reason} </React.Fragment>;
2019-02-14 15:03:45 +01:00
} else if (!this.state.isLoaded) {
2019-02-19 01:16:25 +01:00
return <Col className="text-center">
<div className="lds-ellipsis"><div></div><div></div><div></div><div></div></div>
2019-02-19 01:16:25 +01:00
</Col>;
2019-02-14 15:03:45 +01:00
} else {
return (
2019-02-19 01:16:25 +01:00
<ul className="list-unstyled">{
this.state.res.items.map(item => (
<Link
key={item.id.videoId}
as="li" className="media mx-5 my-1 suggItem"
to={{
pathname: '/video/' + item.id.videoId,
2019-03-03 16:30:39 +01:00
search: "?ref=Search"
2019-02-19 01:16:25 +01:00
}}>
<img
width={240}
height={120}
className="align-self-center mr-3"
src={item.snippet.thumbnails.medium.url}
alt={'Thumbnail of ' + item.snippet.title}
/>
<Media.Body>
<h5>{item.snippet.title}</h5>
<p>{item.snippet.description}</p>
<p>Reccomender: Search</p>
</Media.Body>
</Link>
))
}</ul>
2019-02-14 15:03:45 +01:00
);
}
2019-01-30 21:45:29 +01:00
}
}
export default Search;