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/VideoPlayer.js
T

142 lines
3.4 KiB
JavaScript
Raw Normal View History

2018-11-08 18:08:07 +01:00
import React, { Component } from 'react'; // eslint-disable-line no-unused-vars
import YouTube from 'react-youtube'; // eslint-disable-line no-unused-vars
2018-12-02 21:02:12 +01:00
import axios from 'axios';
2018-12-01 02:44:31 +01:00
/* eslint no-console: ["error", { allow: ["info", "error", "warn"] }] */
2018-11-08 18:08:07 +01:00
class VideoPlayer extends Component {
constructor(props) {
super(props);
this.state = {
opt: {
// height: '390',
// width: '640',
playerVars: {
// https://developers.google.com/youtube/player_parameters
autoplay: 0,
modestbranding: 1
}
},
2018-12-01 02:44:31 +01:00
videoId: '0J2QdDbelmY',
promise1: null,
promise2: null,
outsideReject1: null,
outsideReject2: null
2018-11-08 18:08:07 +01:00
};
2018-12-01 02:44:31 +01:00
this._onPlay = this._onPlay.bind(this);
this._onStateChange = this._onStateChange.bind(this);
this._onReady = this._onReady.bind(this);
2018-11-08 18:08:07 +01:00
}
2018-12-01 02:44:31 +01:00
componentWillUpdate(nextProps, nextState) {
if (nextProps.match.params.id === this.props.match.params.id) {
} else if (nextProps.match.params.id) {
this.setState((state, props) => {
return { videoId: props.match.params.id };
});
}
}
2018-12-01 02:44:31 +01:00
componentWillReceiveProps(nextProps) {
// if (nextProps.location !== this.props.location) {}
}
2018-11-08 18:08:07 +01:00
2018-12-01 02:44:31 +01:00
componentWillMount() {
if (this.props.match.params.id)
this.setState((state, props) => {
return { videoId: props.match.params.id };
});
}
componentWillUnmount() {}
2018-11-08 18:08:07 +01:00
render() {
return (
<React.Fragment>
<YouTube
opts={this.state.opt}
2018-12-01 02:44:31 +01:00
videoId={this.state.videoId}
2018-11-08 18:08:07 +01:00
onReady={this._onReady}
onPlay={this._onPlay}
onError={this._onError}
onStateChange={this._onStateChange}
2018-11-08 18:08:07 +01:00
className="embed-responsive-item"
containerClassName="embed-responsive embed-responsive-16by9"
/>
</React.Fragment>
);
}
2018-12-03 01:22:48 +01:00
// access to player in all event handlers via event.target
2018-11-08 18:08:07 +01:00
_onError(event) {
2018-12-03 01:22:48 +01:00
console.error('Error: ', event);
2018-11-08 18:08:07 +01:00
}
2018-12-01 02:44:31 +01:00
_onPlay(event) {}
2018-12-03 01:22:48 +01:00
_onReady(event) {}
2018-12-01 02:44:31 +01:00
_onStateChange(event) {
2018-12-01 02:44:31 +01:00
let timerId1, timerId2;
if (parseInt(event.data) === -1) {
this.setState({
promise2: new Promise((resolve, reject) => {
timerId2 = window.setInterval(() => {
switch (parseInt(event.target.getPlayerState())) {
case 1:
resolve(1);
break;
case 5:
resolve(5);
break;
default:
break;
}
}, 500);
2018-12-02 21:02:12 +01:00
this.setState({ outsideReject2: reject });
2018-12-01 02:44:31 +01:00
})
});
this.state.promise2.then(msg => {
window.clearInterval(timerId2);
switch (parseInt(msg)) {
case 1:
this.setState({
promise1: new Promise((resolve, reject) => {
timerId1 = window.setInterval(() => {
if (event.target.getCurrentTime() > 15)
resolve(event.target.j.videoData.video_id);
}, 1000);
this.setState({ outsideReject1: reject });
})
});
this.state.promise1
.then(
2018-12-03 01:22:48 +01:00
videoIdWatched => {
2018-12-01 02:44:31 +01:00
// post request to local db
2018-12-02 21:02:12 +01:00
axios
2018-12-03 01:22:48 +01:00
.post('http://localhost:8000/videotrack', {
id: videoIdWatched.toString()
2018-12-02 21:02:12 +01:00
})
.then(
response => console.info(response.data),
error => console.info(error)
);
2018-12-01 02:44:31 +01:00
// local storage logic for recent reccomender
clearInterval(timerId1);
},
error => console.info(error)
)
.finally(() => this.setState({ promise1: null }));
break;
case 5:
if (this.state.promise1)
this.state.outsideReject1('Video changed');
break;
default:
break;
}
});
}
}
2018-11-08 18:08:07 +01:00
}
export default VideoPlayer;