web-base/admin/src/index.js

103 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-06-14 12:38:35 +02:00
import React from 'react';
import ReactDOM from 'react-dom';
import './include/index.css';
import './include/adminlte.min.css';
import API from './api.js';
import Header from './header.js';
import Sidebar from './sidebar.js';
import UserOverview from './users.js';
import Overview from './overview.js'
import Icon from "./icon";
import Dialog from "./dialog";
2020-06-15 14:56:51 +02:00
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'
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 00:00:15 +02:00
onUpdate() {
if (this.state.loaded) {
this.fetchNotifications();
}
}
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 {
this.setState({...this.state, notifications: res.notifications});
}
});
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();
setInterval(this.onUpdate.bind(this), 60000);
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 00:37:29 +02:00
const controlObj = {
notifications: this.state.notifications,
showDialog: this.showDialog.bind(this),
api: this.api
};
2020-06-15 14:56:51 +02:00
const createView = (view) => {
controlObj.currentView = view;
switch (view) {
case "users":
return <UserOverview {...controlObj} />;
case "dashboard":
default:
return <Overview {...controlObj} />;
}
};
return <Router>
2020-06-15 00:37:29 +02:00
<Header {...controlObj} />
<Sidebar {...controlObj} />
2020-06-14 22:35:01 +02:00
<div className={"content-wrapper p-2"}>
<section className={"content"}>
2020-06-15 14:56:51 +02:00
<Route path={"/admin/:view"} component={(obj) => createView(obj.match.params.view)}/>
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(
<AdminDashboard />,
document.getElementById('root')
);