2020-12-29 19:39:30 +00:00
|
|
|
import React from 'react';
|
|
|
|
import HomePage from './pages/HomePage/HomePage';
|
|
|
|
import RandomPage from './pages/RandomPage/RandomPage';
|
|
|
|
import GlobalInfos from './utils/GlobalInfos';
|
|
|
|
|
|
|
|
// include bootstraps css
|
|
|
|
import 'bootstrap/dist/css/bootstrap.min.css';
|
|
|
|
import style from './App.module.css';
|
|
|
|
|
|
|
|
import SettingsPage from './pages/SettingsPage/SettingsPage';
|
|
|
|
import CategoryPage from './pages/CategoryPage/CategoryPage';
|
2021-03-14 12:49:24 +00:00
|
|
|
import {APINode, apiTokenValid, callApiUnsafe, refreshAPIToken} from './utils/Api';
|
2020-12-29 19:39:30 +00:00
|
|
|
|
|
|
|
import {BrowserRouter as Router, NavLink, Route, Switch} from 'react-router-dom';
|
|
|
|
import Player from './pages/Player/Player';
|
|
|
|
import ActorOverviewPage from './pages/ActorOverviewPage/ActorOverviewPage';
|
|
|
|
import ActorPage from './pages/ActorPage/ActorPage';
|
2021-01-22 21:05:21 +00:00
|
|
|
import {SettingsTypes} from './types/ApiTypes';
|
2021-03-14 14:51:53 +00:00
|
|
|
import AuthenticationPage from './pages/AuthenticationPage/AuthenticationPage';
|
2020-12-29 19:39:30 +00:00
|
|
|
|
|
|
|
interface state {
|
2021-03-14 12:49:24 +00:00
|
|
|
password: boolean | null; // null if uninitialized - true if pwd needed false if not needed
|
2020-12-29 19:39:30 +00:00
|
|
|
mediacentername: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The main App handles the main tabs and which content to show
|
|
|
|
*/
|
|
|
|
class App extends React.Component<{}, state> {
|
|
|
|
constructor(props: {}) {
|
|
|
|
super(props);
|
2021-03-14 12:49:24 +00:00
|
|
|
|
|
|
|
let pwdneeded: boolean | null = null;
|
|
|
|
|
|
|
|
if (apiTokenValid()) {
|
|
|
|
pwdneeded = false;
|
|
|
|
} else {
|
|
|
|
refreshAPIToken((err) => {
|
|
|
|
if (err === 'invalid_client') {
|
2021-03-14 14:51:53 +00:00
|
|
|
this.setState({password: true});
|
2021-03-14 12:49:24 +00:00
|
|
|
} else if (err === '') {
|
2021-03-14 14:51:53 +00:00
|
|
|
this.setState({password: false});
|
2021-03-14 12:49:24 +00:00
|
|
|
} else {
|
2021-03-14 14:51:53 +00:00
|
|
|
console.log('unimplemented token error: ' + err);
|
2021-03-14 12:49:24 +00:00
|
|
|
}
|
2021-03-14 14:51:53 +00:00
|
|
|
});
|
2021-03-14 12:49:24 +00:00
|
|
|
}
|
|
|
|
|
2020-12-29 19:39:30 +00:00
|
|
|
this.state = {
|
|
|
|
mediacentername: 'OpenMediaCenter',
|
2021-03-14 12:49:24 +00:00
|
|
|
password: pwdneeded
|
2020-12-29 19:39:30 +00:00
|
|
|
};
|
2021-03-14 12:49:24 +00:00
|
|
|
|
2021-03-16 19:13:12 +00:00
|
|
|
// force an update on theme change
|
2021-03-14 12:49:24 +00:00
|
|
|
GlobalInfos.onThemeChange(() => {
|
|
|
|
this.forceUpdate();
|
2021-03-14 14:51:53 +00:00
|
|
|
});
|
2021-03-16 19:13:12 +00:00
|
|
|
|
|
|
|
// set the hook to load passwordfield on global func call
|
2021-03-19 18:10:13 +00:00
|
|
|
GlobalInfos.loadPasswordPage = (callback?: () => void): void => {
|
2021-03-16 19:13:12 +00:00
|
|
|
// try refreshing the token
|
|
|
|
refreshAPIToken((err) => {
|
|
|
|
if (err !== '') {
|
|
|
|
this.setState({password: true});
|
|
|
|
} else {
|
|
|
|
// call callback if request was successful
|
|
|
|
if (callback) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, true);
|
|
|
|
};
|
2020-12-29 19:39:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
initialAPICall(): void {
|
|
|
|
// this is the first api call so if it fails we know there is no connection to backend
|
2021-04-02 17:04:15 +00:00
|
|
|
callApiUnsafe(APINode.Init, {action: 'loadInitialData'}, (result: SettingsTypes.initialApiCallData) => {
|
|
|
|
// set theme
|
|
|
|
GlobalInfos.enableDarkTheme(result.DarkMode);
|
|
|
|
|
|
|
|
GlobalInfos.setVideoPath(result.VideoPath);
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
mediacentername: result.MediacenterName
|
|
|
|
});
|
|
|
|
// set tab title to received mediacenter name
|
|
|
|
document.title = result.MediacenterName;
|
|
|
|
});
|
2020-12-29 19:39:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount(): void {
|
|
|
|
this.initialAPICall();
|
|
|
|
}
|
|
|
|
|
|
|
|
render(): JSX.Element {
|
|
|
|
const themeStyle = GlobalInfos.getThemeStyle();
|
|
|
|
// add the main theme to the page body
|
|
|
|
document.body.className = themeStyle.backgroundcolor;
|
|
|
|
|
2021-03-14 12:49:24 +00:00
|
|
|
if (this.state.password === true) {
|
2021-03-22 19:07:32 +00:00
|
|
|
// render authentication page if auth is neccessary
|
|
|
|
return <AuthenticationPage onSuccessLogin={(): void => this.setState({password: false})} />;
|
2021-03-14 12:49:24 +00:00
|
|
|
} else if (this.state.password === false) {
|
|
|
|
return (
|
|
|
|
<Router>
|
|
|
|
<div className={style.app}>
|
|
|
|
<div
|
2021-03-14 14:51:53 +00:00
|
|
|
className={[style.navcontainer, themeStyle.backgroundcolor, themeStyle.textcolor, themeStyle.hrcolor].join(
|
|
|
|
' '
|
|
|
|
)}>
|
2021-03-14 12:49:24 +00:00
|
|
|
<div className={style.navbrand}>{this.state.mediacentername}</div>
|
2021-03-14 14:51:53 +00:00
|
|
|
<NavLink
|
|
|
|
className={[style.navitem, themeStyle.navitem].join(' ')}
|
|
|
|
to={'/'}
|
|
|
|
activeStyle={{opacity: '0.85'}}>
|
|
|
|
Home
|
|
|
|
</NavLink>
|
|
|
|
<NavLink
|
|
|
|
className={[style.navitem, themeStyle.navitem].join(' ')}
|
|
|
|
to={'/random'}
|
|
|
|
activeStyle={{opacity: '0.85'}}>
|
|
|
|
Random Video
|
|
|
|
</NavLink>
|
|
|
|
|
|
|
|
<NavLink
|
|
|
|
className={[style.navitem, themeStyle.navitem].join(' ')}
|
|
|
|
to={'/categories'}
|
|
|
|
activeStyle={{opacity: '0.85'}}>
|
|
|
|
Categories
|
|
|
|
</NavLink>
|
|
|
|
<NavLink
|
|
|
|
className={[style.navitem, themeStyle.navitem].join(' ')}
|
|
|
|
to={'/settings'}
|
|
|
|
activeStyle={{opacity: '0.85'}}>
|
|
|
|
Settings
|
|
|
|
</NavLink>
|
2021-03-14 12:49:24 +00:00
|
|
|
</div>
|
|
|
|
{this.routing()}
|
2020-12-29 19:39:30 +00:00
|
|
|
</div>
|
2021-03-14 12:49:24 +00:00
|
|
|
</Router>
|
|
|
|
);
|
|
|
|
} else {
|
2021-03-14 14:51:53 +00:00
|
|
|
return <>still loading...</>;
|
2021-03-14 12:49:24 +00:00
|
|
|
}
|
2020-12-29 19:39:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
routing(): JSX.Element {
|
|
|
|
return (
|
|
|
|
<Switch>
|
2021-03-14 14:51:53 +00:00
|
|
|
<Route path='/random'>
|
|
|
|
<RandomPage />
|
2020-12-29 19:39:30 +00:00
|
|
|
</Route>
|
2021-03-14 14:51:53 +00:00
|
|
|
<Route path='/categories'>
|
|
|
|
<CategoryPage />
|
2020-12-29 19:39:30 +00:00
|
|
|
</Route>
|
2021-03-14 14:51:53 +00:00
|
|
|
<Route path='/settings'>
|
|
|
|
<SettingsPage />
|
2020-12-29 19:39:30 +00:00
|
|
|
</Route>
|
2021-03-14 14:51:53 +00:00
|
|
|
<Route exact path='/player/:id'>
|
|
|
|
<Player />
|
2020-12-29 19:39:30 +00:00
|
|
|
</Route>
|
2021-03-14 14:51:53 +00:00
|
|
|
<Route exact path='/actors'>
|
|
|
|
<ActorOverviewPage />
|
2020-12-29 19:39:30 +00:00
|
|
|
</Route>
|
2021-03-14 14:51:53 +00:00
|
|
|
<Route path='/actors/:id'>
|
|
|
|
<ActorPage />
|
2020-12-29 19:39:30 +00:00
|
|
|
</Route>
|
2021-03-14 14:51:53 +00:00
|
|
|
<Route path='/'>
|
|
|
|
<HomePage />
|
2020-12-29 19:39:30 +00:00
|
|
|
</Route>
|
|
|
|
</Switch>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default App;
|