OpenMediaCenter/src/App.js

145 lines
5.2 KiB
JavaScript
Raw Normal View History

2020-05-31 11:47:57 +00:00
import React from 'react';
2020-06-12 15:57:30 +00:00
import HomePage from "./pages/HomePage/HomePage";
import RandomPage from "./pages/RandomPage/RandomPage";
import GlobalInfos from "./GlobalInfos";
2020-05-31 18:24:35 +00:00
2020-06-01 18:46:28 +00:00
// include bootstraps css
2020-05-31 11:47:57 +00:00
import 'bootstrap/dist/css/bootstrap.min.css';
import style from './App.module.css'
2020-06-12 15:57:30 +00:00
import SettingsPage from "./pages/SettingsPage/SettingsPage";
import CategoryPage from "./pages/CategoryPage/CategoryPage";
2020-05-31 11:47:57 +00:00
/**
* The main App handles the main tabs and which content to show
*/
2020-05-31 11:47:57 +00:00
class App extends React.Component {
newElement = null;
2020-05-31 18:24:35 +00:00
constructor(props, context) {
super(props, context);
this.state = {
page: "default",
generalSettingsLoaded: false,
passwordsupport: null,
mediacentername: "OpenMediaCenter"
};
// bind this to the method for being able to call methods such as this.setstate
2020-06-21 21:08:46 +00:00
this.changeRootElement = this.changeRootElement.bind(this);
this.returnToLastElement = this.returnToLastElement.bind(this);
2020-05-31 11:47:57 +00:00
}
componentDidMount() {
const updateRequest = new FormData();
updateRequest.append('action', 'loadInitialData');
fetch('/api/settings.php', {method: 'POST', body: updateRequest})
.then((response) => response.json()
.then((result) => {
// set theme
GlobalInfos.enableDarkTheme(result.DarkMode);
this.setState({
generalSettingsLoaded: true,
passwordsupport: result.passwordEnabled,
mediacentername: result.mediacenter_name
});
2020-07-26 12:27:56 +00:00
// set tab title to received mediacenter name
document.title = result.mediacenter_name;
}));
}
/**
* create a viewbinding to call APP functions from child elements
* @returns a set of callback functions
*/
2020-07-16 17:13:54 +00:00
constructViewBinding() {
return {
changeRootElement: this.changeRootElement,
returnToLastElement: this.returnToLastElement
};
}
/**
* load the selected component into the main view
* @returns {JSX.Element} body element of selected page
*/
MainBody() {
let page;
if (this.state.page === "default") {
page = <HomePage viewbinding={this.constructViewBinding()}/>;
this.mypage = page;
} else if (this.state.page === "random") {
page = <RandomPage viewbinding={this.constructViewBinding()}/>;
this.mypage = page;
} else if (this.state.page === "settings") {
page = <SettingsPage/>;
this.mypage = page;
} else if (this.state.page === "categories") {
page = <CategoryPage viewbinding={this.constructViewBinding()}/>;
this.mypage = page;
} else if (this.state.page === "video") {
// show videoelement if neccessary
2020-06-21 21:08:46 +00:00
page = this.newElement;
console.log(page);
} else if (this.state.page === "lastpage") {
// return back to last page
page = this.mypage;
} else {
page = <div>unimplemented yet!</div>;
}
return (page);
}
2020-05-31 11:47:57 +00:00
render() {
const themeStyle = GlobalInfos.getThemeStyle();
2020-07-26 16:17:29 +00:00
// add the main theme to the page body
document.body.className = themeStyle.backgroundcolor;
2020-05-31 11:47:57 +00:00
return (
<div className={style.app}>
2020-07-26 16:17:29 +00:00
<div className={[style.navcontainer, themeStyle.backgroundcolor, themeStyle.textcolor, themeStyle.hrcolor].join(' ')}>
<div className={style.navbrand}>{this.state.mediacentername}</div>
2020-07-26 16:17:29 +00:00
<div className={[style.navitem, themeStyle.navitem, this.state.page === "default" ? style.navitemselected : {}].join(' ')}
onClick={() => this.setState({page: "default"})}>Home
</div>
2020-07-26 16:17:29 +00:00
<div className={[style.navitem, themeStyle.navitem, this.state.page === "random" ? style.navitemselected : {}].join(' ')}
onClick={() => this.setState({page: "random"})}>Random Video
</div>
2020-07-26 16:17:29 +00:00
<div className={[style.navitem, themeStyle.navitem, this.state.page === "categories" ? style.navitemselected : {}].join(' ')}
onClick={() => this.setState({page: "categories"})}>Categories
</div>
2020-07-26 16:17:29 +00:00
<div className={[style.navitem, themeStyle.navitem, this.state.page === "settings" ? style.navitemselected : {}].join(' ')}
onClick={() => this.setState({page: "settings"})}>Settings
</div>
</div>
{this.state.generalSettingsLoaded ? this.MainBody() : "loading"}
2020-05-31 11:47:57 +00:00
</div>
);
}
/**
* render a new root element into the main body
*/
2020-06-21 21:08:46 +00:00
changeRootElement(element) {
this.newElement = element;
this.setState({
page: "video"
});
}
/**
* return from page to the previous page before a change
*/
2020-06-21 21:08:46 +00:00
returnToLastElement() {
this.setState({
page: "lastpage"
});
}
2020-05-31 11:47:57 +00:00
}
export default App;