OpenMediaCenter/src/HomePage.js

107 lines
3.0 KiB
JavaScript
Raw Normal View History

2020-06-01 18:46:28 +00:00
import React, {useState} from "react";
2020-05-31 18:24:35 +00:00
import Preview from "./Preview";
import "./css/HomePage.css"
2020-05-31 18:24:35 +00:00
class HomePage extends React.Component {
// stores all available movies
data = null;
// stores current index of loaded elements
loadindex = 0;
2020-05-31 18:24:35 +00:00
constructor(props, context) {
super(props, context);
this.state = {
2020-06-01 18:46:28 +00:00
loadeditems: [],
sideinfo: {
videonr: null,
hdvideonr: null,
sdvideonr: null,
categorynr: null
}
2020-05-31 18:24:35 +00:00
};
}
componentDidMount() {
document.addEventListener('scroll', this.trackScrolling);
2020-05-31 18:24:35 +00:00
const updateRequest = new FormData();
updateRequest.append('action', 'getMovies');
// fetch all videos available
fetch('/php/videoload.php', {method: 'POST', body: updateRequest})
2020-06-01 18:46:28 +00:00
.then((response) => response.json()
.then((result) => {
this.data = result;
this.loadPreviewBlock(12);
}))
.catch(() => {
console.log("no connection to backend");
2020-05-31 18:24:35 +00:00
});
2020-06-01 18:46:28 +00:00
2020-05-31 18:24:35 +00:00
}
componentWillUnmount() {
this.setState({});
document.removeEventListener('scroll', this.trackScrolling);
2020-05-31 18:24:35 +00:00
}
render() {
return (
<div>
<div><h1>Home page</h1></div>
<div className='sideinfo'>
2020-06-01 18:46:28 +00:00
Infos:
<div>Total Number of Videos: {this.state.sideinfo.videonr}</div>
<div>HD Videos: {this.state.sideinfo.hdvideonr}</div>
<div>SD Videos: {this.state.sideinfo.sdvideonr}</div>
<div>Total Number of Categories: {this.state.sideinfo.categorynr}</div>
</div>
<div className='maincontent'>
{this.state.loadeditems.map(elem => (
<Preview
key={elem.movie_id}
name={elem.movie_name}
movie_id={elem.movie_id}
viewbinding={this.props.viewbinding}/>
))}
</div>
<div className='rightinfo'>
</div>
2020-05-31 18:24:35 +00:00
</div>
);
}
loadPreviewBlock(nr) {
console.log("loadpreviewblock called ...")
2020-05-31 18:24:35 +00:00
let ret = [];
for (let i = 0; i < nr; i++) {
// only add if not end
if (this.data.length > this.loadindex + i) {
ret.push(this.data[this.loadindex + i]);
}
}
this.setState({
loadeditems: [
...this.state.loadeditems,
...ret
]
});
this.loadindex += nr;
}
trackScrolling = (e) => {
if (window.innerHeight + document.documentElement.scrollTop === document.documentElement.offsetHeight) {
this.loadPreviewBlock(6);
2020-05-31 18:24:35 +00:00
}
}
}
export default HomePage;