2020-06-14 12:38:35 +02:00
|
|
|
import React from 'react';
|
|
|
|
import ReactDOM from 'react-dom';
|
|
|
|
import './include/adminlte.min.css';
|
2020-06-15 20:07:43 +02:00
|
|
|
import './include/index.css';
|
2020-06-14 12:38:35 +02:00
|
|
|
import API from './api.js';
|
|
|
|
import Header from './header.js';
|
|
|
|
import Sidebar from './sidebar.js';
|
2020-06-15 23:10:14 +02:00
|
|
|
import UserOverview from './views/users.js';
|
|
|
|
import Overview from './views/overview.js'
|
2020-06-17 14:30:37 +02:00
|
|
|
import CreateUser from "./views/adduser";
|
2020-06-15 23:10:14 +02:00
|
|
|
import Icon from "./elements/icon";
|
|
|
|
import Dialog from "./elements/dialog";
|
|
|
|
import {BrowserRouter as Router, Route, Switch} from 'react-router-dom'
|
2020-06-15 20:07:43 +02:00
|
|
|
import View404 from "./404";
|
2020-06-15 23:10:14 +02:00
|
|
|
import Logs from "./views/logs";
|
2020-06-14 12:38:35 +02:00
|
|
|
|
|
|
|
class AdminDashboard extends React.Component {
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.api = new API();
|
|
|
|
this.state = {
|
|
|
|
loaded: false,
|
2020-06-15 00:00:15 +02:00
|
|
|
dialog: { onClose: () => this.hideDialog() },
|
|
|
|
notifications: { }
|
2020-06-14 12:38:35 +02:00
|
|
|
};
|
2020-06-15 20:07:43 +02:00
|
|
|
this.controlObj = {
|
|
|
|
showDialog: this.showDialog.bind(this),
|
|
|
|
api: this.api
|
|
|
|
};
|
2020-06-14 12:38:35 +02:00
|
|
|
}
|
|
|
|
|
2020-06-15 00:00:15 +02:00
|
|
|
onUpdate() {
|
2020-06-15 20:07:43 +02:00
|
|
|
this.fetchNotifications();
|
2020-06-15 00:00:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
showDialog(message, title) {
|
|
|
|
const props = { show: true, message: message, title: title };
|
|
|
|
this.setState({ ...this.state, dialog: { ...this.state.dialog, ...props } });
|
|
|
|
}
|
|
|
|
|
|
|
|
hideDialog() {
|
|
|
|
this.setState({ ...this.state, dialog: { ...this.state.dialog, show: false } });
|
2020-06-14 12:38:35 +02:00
|
|
|
}
|
|
|
|
|
2020-06-15 00:00:15 +02:00
|
|
|
fetchNotifications() {
|
|
|
|
this.api.getNotifications().then((res) => {
|
|
|
|
if (!res.success) {
|
|
|
|
this.showDialog("Error fetching notifications: " + res.msg, "Error fetching notifications");
|
|
|
|
} else {
|
2020-06-15 20:07:43 +02:00
|
|
|
this.setState({...this.state, notifications: res.notifications });
|
2020-06-15 00:00:15 +02:00
|
|
|
}
|
|
|
|
});
|
2020-06-14 12:38:35 +02:00
|
|
|
}
|
|
|
|
|
2020-06-15 14:56:51 +02:00
|
|
|
componentDidMount() {
|
|
|
|
this.api.fetchUser().then(Success => {
|
|
|
|
if (!Success) {
|
|
|
|
document.location = "/admin";
|
|
|
|
} else {
|
|
|
|
this.fetchNotifications();
|
2020-06-15 20:07:43 +02:00
|
|
|
setInterval(this.onUpdate.bind(this), 60*1000);
|
2020-06-15 14:56:51 +02:00
|
|
|
this.setState({...this.state, loaded: true});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-14 12:38:35 +02:00
|
|
|
render() {
|
|
|
|
|
|
|
|
if (!this.state.loaded) {
|
|
|
|
return <b>Loading… <Icon icon={"spinner"} /></b>
|
|
|
|
}
|
|
|
|
|
2020-06-15 14:56:51 +02:00
|
|
|
return <Router>
|
2020-06-15 20:07:43 +02:00
|
|
|
<Header {...this.controlObj} notifications={this.state.notifications} />
|
|
|
|
<Sidebar {...this.controlObj} notifications={this.state.notifications} />
|
2020-06-14 22:35:01 +02:00
|
|
|
<div className={"content-wrapper p-2"}>
|
|
|
|
<section className={"content"}>
|
2020-06-15 20:07:43 +02:00
|
|
|
<Switch>
|
|
|
|
<Route path={"/admin/dashboard"}><Overview {...this.controlObj} /></Route>
|
2020-06-17 14:30:37 +02:00
|
|
|
<Route exact={true} path={"/admin/users"}><UserOverview {...this.controlObj} /></Route>
|
|
|
|
<Route exact={true} path={"/admin/users/adduser"}><CreateUser {...this.controlObj} /></Route>
|
2020-06-15 23:10:14 +02:00
|
|
|
<Route path={"/admin/logs"}><Logs {...this.controlObj} /></Route>
|
2020-06-15 20:07:43 +02:00
|
|
|
<Route path={"*"}><View404 /></Route>
|
|
|
|
</Switch>
|
2020-06-14 22:35:01 +02:00
|
|
|
<Dialog {...this.state.dialog}/>
|
|
|
|
</section>
|
|
|
|
</div>
|
2020-06-15 14:56:51 +02:00
|
|
|
</Router>
|
2020-06-14 12:38:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReactDOM.render(
|
2020-06-15 20:07:43 +02:00
|
|
|
<AdminDashboard />,
|
|
|
|
document.getElementById('root')
|
2020-06-14 12:38:35 +02:00
|
|
|
);
|