Archived
144 lines
3.9 KiB
JavaScript
144 lines
3.9 KiB
JavaScript
import React from 'react';
|
|||
|
|
import { Col, ListGroup, Card, CardDeck } from 'react-bootstrap'; // eslint-disable-line no-unused-vars
|
||
|
|
import axios from 'axios'; // eslint-disable-line no-unused-vars
|
||
|
|
import { Link } from 'react-router-dom'; // eslint-disable-line no-unused-vars
|
||
|
|
/*eslint no-console: ["error", { allow: ["warn", "error", "info"] }] */
|
||
|
|
|
||
|
|
class VideoList extends React.Component {
|
||
|
|
constructor(props) {
|
||
|
|
super(props);
|
||
|
|
this.state = {
|
||
|
|
error: null,
|
||
|
|
isLoaded: false,
|
||
|
|
items: [],
|
||
|
|
headers: null
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
componentDidMount() {
|
||
|
|
axios.get('http://site1825.tw.cs.unibo.it/video.json').then(
|
||
|
|
response => {
|
||
|
|
this.shuffleArray(response.data);
|
||
|
|
this.setState({
|
||
|
|
headers: response.config
|
||
|
|
});
|
||
|
|
return this.getThumbnails(response.data);
|
||
|
|
},
|
||
|
|
// Note: it's important to handle errors here
|
||
|
|
// instead of a catch() block so that we don't swallow
|
||
|
|
// exceptions from actual bugs in components.
|
||
|
|
error => {
|
||
|
|
console.error(error);
|
||
|
|
this.setState({
|
||
|
|
isLoaded: true,
|
||
|
|
error
|
||
|
|
});
|
||
|
|
}
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
render() {
|
||
|
|
const { error, isLoaded, items, headers } = this.state;
|
||
|
|
if (error) {
|
||
|
|
return (
|
||
|
|
<React.Fragment>
|
||
|
|
Error: {error.message} -- Cannot get {error.config.url}
|
||
|
|
</React.Fragment>
|
||
|
|
);
|
||
|
|
} else if (!isLoaded) {
|
||
|
|
return <React.Fragment>Loading...</React.Fragment>;
|
||
|
|
} else {
|
||
|
|
return (
|
||
|
|
<React.Fragment>
|
||
|
|
{items.map(item => (
|
||
|
|
<Col key={item.videoID} lg="4" md="6">
|
||
|
|
<Card className="my-1">
|
||
|
|
<Link to={{ pathname: '/video/' + item.videoID }}>
|
||
|
|
<ListGroup>
|
||
|
|
<ListGroup.Item className="bg-secondary text-white">
|
||
|
|
{item.artist} - {item.title}
|
||
|
|
</ListGroup.Item>
|
||
|
|
<ListGroup.Item>
|
||
|
|
<span className="bg-info text-white">
|
||
|
|
{' '}
|
||
|
|
{item.videoID}
|
||
|
|
</span>{' '}
|
||
|
|
{item.category}
|
||
|
|
</ListGroup.Item>
|
||
|
|
</ListGroup>
|
||
|
|
</Link>
|
||
|
|
</Card>
|
||
|
|
</Col>
|
||
|
|
))}
|
||
|
|
</React.Fragment>
|
||
|
|
);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
shuffleArray(videoarray) {
|
||
|
|
for (let i = videoarray.length - 1; i > 0; i--) {
|
||
|
|
const j = Math.floor(Math.random() * (i + 1));
|
||
|
|
[videoarray[i], videoarray[j]] = [videoarray[j], videoarray[i]]; // eslint-disable-line no-param-reassign
|
||
|
|
}
|
||
|
|
// Durstenfeld shuffle.
|
||
|
|
// https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
|
||
|
|
}
|
||
|
|
getThumbnails(videoItems) {
|
||
|
|
let idToLookFor = videoItems.map(item => item.videoID);
|
||
|
|
let finalChunk = (videoItems.length % 50) + 100;
|
||
|
|
axios
|
||
|
|
.all([
|
||
|
|
// chain 3 parallel get
|
||
|
|
axios.get('https://www.googleapis.com/youtube/v3/videos', {
|
||
|
|
params: {
|
||
|
|
part: 'snippet',
|
||
|
|
id: idToLookFor.slice(0, 50).toString(),
|
||
|
|
key: 'AIzaSyA21Odw9UdfiOJnxvA_eDfOW0OxuUfHKig',
|
||
|
|
fields: 'items(id,snippet/thumbnails/medium)'
|
||
|
|
}
|
||
|
|
}),
|
||
|
|
axios.get('https://www.googleapis.com/youtube/v3/videos', {
|
||
|
|
//chain multiple get into promises
|
||
|
|
params: {
|
||
|
|
part: 'snippet',
|
||
|
|
id: idToLookFor.slice(50, 100).toString(),
|
||
|
|
key: 'AIzaSyA21Odw9UdfiOJnxvA_eDfOW0OxuUfHKig',
|
||
|
|
fields: 'items(id,snippet/thumbnails/medium)'
|
||
|
|
}
|
||
|
|
}),
|
||
|
|
axios.get('https://www.googleapis.com/youtube/v3/videos', {
|
||
|
|
//chain multiple get into promises
|
||
|
|
params: {
|
||
|
|
part: 'snippet',
|
||
|
|
id: idToLookFor.slice(100, finalChunk).toString(),
|
||
|
|
key: 'AIzaSyA21Odw9UdfiOJnxvA_eDfOW0OxuUfHKig',
|
||
|
|
fields: 'items(id,snippet/thumbnails/medium)'
|
||
|
|
}
|
||
|
|
})
|
||
|
|
])
|
||
|
|
.then(
|
||
|
|
axios.spread((res1, res2, res3) => {
|
||
|
|
let allres = [].concat(
|
||
|
|
res1.data.items,
|
||
|
|
res2.data.items,
|
||
|
|
res3.data.items
|
||
|
|
);
|
||
|
|
allres.forEach(resCurrentValue => {
|
||
|
|
Object.defineProperty(
|
||
|
|
videoItems.find(videoItem => {
|
||
|
|
return videoItem.videoID === resCurrentValue.id;
|
||
|
|
}),
|
||
|
|
'thumbnail',
|
||
|
|
{ value: resCurrentValue.snippet.thumbnails.medium.url }
|
||
|
|
);
|
||
|
|
});
|
||
|
|
})
|
||
|
|
);
|
||
|
|
return this.setState({
|
||
|
|
isLoaded: true,
|
||
|
|
items: videoItems
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export default VideoList;
|