add admin section - basic skeleton and helper methods
This commit is contained in:
parent
6c0daa1e3c
commit
f399b14ffa
9 changed files with 115 additions and 7 deletions
19
app/controllers/AdminController.js
Normal file
19
app/controllers/AdminController.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
|
||||
class AdminController {
|
||||
|
||||
constructor(database) {
|
||||
this.database = database;
|
||||
};
|
||||
|
||||
getAll(onSuccess, onError) {
|
||||
let collection = this.database.collection('publicStates');
|
||||
let cursor = collection.find({}, {});
|
||||
cursor.toArray((item) => {
|
||||
console.log('item: ' + item);
|
||||
});
|
||||
onSuccess({});
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
module.exports = AdminController;
|
|
@ -45,6 +45,7 @@ class PublicStateController {
|
|||
}
|
||||
});
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
module.exports = PublicStateController;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const Authenticator = require('../Authenticator');
|
||||
const AdminController = require('../controllers/AdminController');
|
||||
const UserController = require('../controllers/UserController');
|
||||
const PublicStateController = require('../controllers/PublicStateController');
|
||||
|
||||
|
@ -27,6 +28,24 @@ router.post('/state', (req, res, next) => {
|
|||
authenticator.withUser(req, res, (user) => ctrl.create(req.body, user, scb, ecb));
|
||||
});
|
||||
|
||||
router.get('/state', (req, res, next) => {
|
||||
app = req.app;
|
||||
db = app.locals.database;
|
||||
|
||||
let authenticator = new Authenticator(db);
|
||||
let ctrl = new AdminController(db);
|
||||
|
||||
let scb = (data) => {
|
||||
res.json(data);
|
||||
};
|
||||
|
||||
let ecb = (error) => {
|
||||
res.status(400).send(error);
|
||||
};
|
||||
|
||||
authenticator.withUser(req, res, (user) => ctrl.getAll(scb, ecb));
|
||||
});
|
||||
|
||||
router.get('/user', function (req, res, next) {
|
||||
app = req.app;
|
||||
db = app.locals.database;
|
||||
|
|
15
client/src/components/PublicStateList/PublicStateList.js
Normal file
15
client/src/components/PublicStateList/PublicStateList.js
Normal file
|
@ -0,0 +1,15 @@
|
|||
import React, { Component } from 'react';
|
||||
|
||||
class PublicStateList extends Component {
|
||||
|
||||
render() {
|
||||
return (
|
||||
<div>
|
||||
<h1>PublicStateList</h1>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
export default PublicStateList;
|
|
@ -1,9 +1,11 @@
|
|||
import AppHeader from './AppHeader/AppHeader';
|
||||
import Login from './Login/Login';
|
||||
import PublicStateList from './PublicStateList/PublicStateList';
|
||||
import Register from './Register/Register';
|
||||
|
||||
export {
|
||||
AppHeader,
|
||||
PublicStateList,
|
||||
Login,
|
||||
Register
|
||||
};
|
||||
|
|
|
@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
|
|||
import { connect } from 'react-redux';
|
||||
import { Redirect } from 'react-router-dom';
|
||||
import { createPublicState } from '../../../actions/gameActions';
|
||||
import { Form, Input, Button, Typography } from 'antd';
|
||||
import { Form, Input, Button, Typography, InputNumber } from 'antd';
|
||||
|
||||
const layout = {
|
||||
labelCol: {
|
||||
|
@ -107,6 +107,19 @@ class CreatePublicState extends Component {
|
|||
<Input />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item
|
||||
label="Minimum number of Citizen per private state"
|
||||
name="privateStateMinCitizen"
|
||||
rules={[
|
||||
{
|
||||
required: false,
|
||||
message: 'Please input the minimum number of citizen that can constitute a private state',
|
||||
},
|
||||
]}
|
||||
>
|
||||
<InputNumber defaultValue={3} min={2} />
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item
|
||||
style={errorMessageStyle}
|
||||
label=" "
|
||||
|
|
17
client/src/pages/AdminDashboardPage/AdminDashboardPage.js
Normal file
17
client/src/pages/AdminDashboardPage/AdminDashboardPage.js
Normal file
|
@ -0,0 +1,17 @@
|
|||
import React, { Component } from 'react';
|
||||
import { PublicStateList } from '../../components';
|
||||
|
||||
class AdminDashboardPage extends Component {
|
||||
|
||||
|
||||
render() {
|
||||
return (
|
||||
<div>
|
||||
<h1>Admin Dashboard</h1>
|
||||
<PublicStateList />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
};
|
||||
|
||||
export default AdminDashboardPage;
|
|
@ -1,3 +1,4 @@
|
|||
import AdminDashboardPage from './AdminDashboardPage/AdminDashboardPage';
|
||||
import DashboardPage from './DashboardPage/DashboardPage';
|
||||
import LoginPage from './LoginPage/LoginPage';
|
||||
import PublicStateOverviewPage from './PublicStateOverviewPage/PublicStateOverviewPage';
|
||||
|
@ -6,6 +7,7 @@ import SplashScreenPage from './SplashScreenPage/SplashScreenPage';
|
|||
import WelcomePage from './WelcomePage/WelcomePage';
|
||||
|
||||
export {
|
||||
AdminDashboardPage,
|
||||
DashboardPage,
|
||||
LoginPage,
|
||||
PublicStateOverviewPage,
|
||||
|
|
|
@ -10,6 +10,7 @@ import {
|
|||
} from 'react-router-dom';
|
||||
import { Layout } from 'antd';
|
||||
import {
|
||||
AdminDashboardPage,
|
||||
DashboardPage,
|
||||
LoginPage,
|
||||
PublicStateOverviewPage,
|
||||
|
@ -22,9 +23,7 @@ import 'antd/dist/antd.css';
|
|||
|
||||
const { Content } = Layout;
|
||||
|
||||
// A wrapper for <Route> that redirects to the login
|
||||
// screen if user is not yet authenticated.
|
||||
export const PrivateRoute = ({ children, ...rest }) => {
|
||||
export const AuthenticatedRoute = ({ children, ...rest }) => {
|
||||
return (
|
||||
<Route
|
||||
{...rest}
|
||||
|
@ -44,6 +43,26 @@ export const PrivateRoute = ({ children, ...rest }) => {
|
|||
);
|
||||
};
|
||||
|
||||
export const AdminRoute = ({ children, ...rest }) => {
|
||||
return (
|
||||
<Route
|
||||
{...rest}
|
||||
render={({ location }) =>
|
||||
(rest.user && rest.user.role === 'admin') ? (
|
||||
children
|
||||
) : (
|
||||
<Redirect
|
||||
to={{
|
||||
pathname: "/admin/autherror",
|
||||
state: { from: location }
|
||||
}}
|
||||
/>
|
||||
)
|
||||
}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
class AppRouter extends Component {
|
||||
|
||||
|
@ -66,7 +85,6 @@ class AppRouter extends Component {
|
|||
render() {
|
||||
|
||||
if (this.state.identityChecked) {
|
||||
console.log('user: ' + this.props.user);
|
||||
return (
|
||||
<Router>
|
||||
<Layout style={{ height: "100%" }}>
|
||||
|
@ -77,8 +95,10 @@ class AppRouter extends Component {
|
|||
<Route exact path='/'><WelcomePage /></Route>
|
||||
<Route exact path='/login'><LoginPage /></Route>
|
||||
<Route exact path='/register'><RegisterPage /></Route>
|
||||
<PrivateRoute exact path='/start' user={this.props.user}><DashboardPage /></PrivateRoute>
|
||||
<PrivateRoute exact path='/state/:name' user={this.props.user}><PublicStateOverviewPage /></PrivateRoute>
|
||||
<Route exact path='/admin/autherror'><div>not an admin</div></Route>
|
||||
<AuthenticatedRoute exact path='/start' user={this.props.user}><DashboardPage /></AuthenticatedRoute>
|
||||
<AuthenticatedRoute exact path='/state/:name' user={this.props.user}><PublicStateOverviewPage /></AuthenticatedRoute>
|
||||
<AdminRoute exact path='/admin' user={this.props.user}><AdminDashboardPage /></AdminRoute>
|
||||
</Switch>
|
||||
</div>
|
||||
</Content>
|
||||
|
|
Loading…
Reference in a new issue