web-base/admin/src/index.js

103 lines
2.5 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";
class AdminDashboard extends React.Component {
constructor(props) {
super(props);
this.api = new API();
this.state = {
currentView: "dashboard",
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();
}
}
2020-06-14 12:38:35 +02:00
onChangeView(view) {
2020-06-15 00:00:15 +02:00
this.setState({ ...this.state, currentView: view || "dashboard" });
}
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
}
render() {
if (!this.state.loaded) {
this.api.fetchUser().then(Success => {
if (!Success) {
document.location = "/admin";
} else {
2020-06-15 00:00:15 +02:00
this.fetchNotifications();
setInterval(this.onUpdate.bind(this), 60000);
2020-06-14 12:38:35 +02:00
this.setState({...this.state, loaded: true});
}
});
return <b>Loading <Icon icon={"spinner"} /></b>
}
2020-06-15 00:37:29 +02:00
const controlObj = {
notifications: this.state.notifications,
currentView: this.state.currentView,
onChangeView: this.onChangeView.bind(this),
showDialog: this.showDialog.bind(this),
api: this.api
};
2020-06-14 22:35:01 +02:00
return <>
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"}>
{this.createContent()}
<Dialog {...this.state.dialog}/>
</section>
</div>
</>
2020-06-14 12:38:35 +02:00
}
createContent() {
if (this.state.currentView === "users") {
return <UserOverview />
} else {
return <Overview />
}
}
}
ReactDOM.render(
<AdminDashboard />,
document.getElementById('root')
);