web-base/adminPanel/src/index.js

142 lines
5.1 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/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-19 13:13:13 +02:00
import PageOverview from "./views/pages";
2020-06-22 23:47:07 +02:00
import HelpPage from "./views/help";
import Footer from "./footer";
2020-06-23 16:26:04 +02:00
import EditUser from "./views/edituser";
2020-06-24 01:09:08 +02:00
import CreateGroup from "./views/addgroup";
2020-06-26 01:47:43 +02:00
import Settings from "./views/settings";
2020-06-27 01:32:32 +02:00
import PermissionSettings from "./views/permissions";
2020-07-01 21:10:25 +02:00
import Visitors from "./views/visitors";
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() },
2021-03-30 00:27:15 +02:00
notifications: [ ],
filesPath: null
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, options=["Close"], onOption = null) {
const props = { show: true, message: message, title: title, options: options, onOption: onOption };
2020-06-15 00:00:15 +02:00
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
}
2021-03-30 00:27:15 +02:00
fetchFilesPath() {
this.api.getRoutes().then((res) => {
if (!res.success) {
this.showDialog("Error fetching routes: " + res.msg, "Error fetching routes");
} else {
for (const route of res.routes) {
if (route.target === "\\Documents\\Files") {
// prepare the path patterns, e.g. '/files(/.*)?' => '/files'
let path = route.request;
path = path.replace(/\(.*\)([?*])/g, ''); // remove optional and 0-n groups
path = path.replace(/.\*/g, ''); // remove .*
path = path.replace(/\[.*]\*/g, ''); // remove []*
path = path.replace(/(.*)\+/g, "$1"); // replace 1-n groups with one match
// todo: add some more rules, but we should have most of the cases now
this.setState({...this.state, filesPath: path });
break;
}
}
}
});
}
2020-06-15 14:56:51 +02:00
componentDidMount() {
this.api.fetchUser().then(Success => {
if (!Success) {
document.location = "/admin";
} else {
this.fetchNotifications();
2021-03-30 00:27:15 +02:00
this.fetchFilesPath();
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-17 20:20:31 +02:00
this.controlObj = {
showDialog: this.showDialog.bind(this),
2020-06-26 23:32:45 +02:00
fetchNotifications: this.fetchNotifications.bind(this),
2020-06-17 20:20:31 +02:00
api: this.api
};
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} />
2021-03-30 00:27:15 +02:00
<Sidebar {...this.controlObj} notifications={this.state.notifications} filesPath={this.state.filesPath} />
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>
2020-06-17 20:20:31 +02:00
<Route path={"/admin/dashboard"}><Overview {...this.controlObj} notifications={this.state.notifications} /></Route>
2020-06-17 14:30:37 +02:00
<Route exact={true} path={"/admin/users"}><UserOverview {...this.controlObj} /></Route>
2020-06-23 16:26:04 +02:00
<Route path={"/admin/user/add"}><CreateUser {...this.controlObj} /></Route>
<Route path={"/admin/user/edit/:userId"} render={(props) => {
let newProps = {...props, ...this.controlObj};
return <EditUser {...newProps} />
}}/>
2020-06-27 01:32:32 +02:00
<Route path={"/admin/user/permissions"}><PermissionSettings {...this.controlObj}/></Route>
2020-06-24 01:09:08 +02:00
<Route path={"/admin/group/add"}><CreateGroup {...this.controlObj} /></Route>
2020-07-01 21:10:25 +02:00
<Route path={"/admin/visitors"}><Visitors {...this.controlObj} /></Route>
2020-06-26 23:32:45 +02:00
<Route path={"/admin/logs"}><Logs {...this.controlObj} notifications={this.state.notifications} /></Route>
2020-06-26 01:47:43 +02:00
<Route path={"/admin/settings"}><Settings {...this.controlObj} /></Route>
2020-06-19 13:13:13 +02:00
<Route path={"/admin/pages"}><PageOverview {...this.controlObj} /></Route>
2020-06-22 23:47:07 +02:00
<Route path={"/admin/help"}><HelpPage {...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>
2020-06-22 23:47:07 +02:00
</div>
<Footer />
</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
);