From f399b14ffa3d874ffcaf1206012d44bef6de452b Mon Sep 17 00:00:00 2001 From: Markus Schubert Date: Sun, 29 Mar 2020 18:21:36 +0200 Subject: [PATCH] add admin section - basic skeleton and helper methods --- app/controllers/AdminController.js | 19 +++++++++++ app/controllers/PublicStateController.js | 1 + app/routes/api.js | 19 +++++++++++ .../PublicStateList/PublicStateList.js | 15 +++++++++ client/src/components/index.js | 2 ++ .../CreatePublicState/CreatePublicState.js | 15 ++++++++- .../AdminDashboardPage/AdminDashboardPage.js | 17 ++++++++++ client/src/pages/index.js | 2 ++ client/src/router/AppRouter.js | 32 +++++++++++++++---- 9 files changed, 115 insertions(+), 7 deletions(-) create mode 100644 app/controllers/AdminController.js create mode 100644 client/src/components/PublicStateList/PublicStateList.js create mode 100644 client/src/pages/AdminDashboardPage/AdminDashboardPage.js diff --git a/app/controllers/AdminController.js b/app/controllers/AdminController.js new file mode 100644 index 0000000..e87bac9 --- /dev/null +++ b/app/controllers/AdminController.js @@ -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; \ No newline at end of file diff --git a/app/controllers/PublicStateController.js b/app/controllers/PublicStateController.js index 225835c..ac2ccd3 100644 --- a/app/controllers/PublicStateController.js +++ b/app/controllers/PublicStateController.js @@ -45,6 +45,7 @@ class PublicStateController { } }); }; + }; module.exports = PublicStateController; diff --git a/app/routes/api.js b/app/routes/api.js index 67474b6..f983344 100644 --- a/app/routes/api.js +++ b/app/routes/api.js @@ -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; diff --git a/client/src/components/PublicStateList/PublicStateList.js b/client/src/components/PublicStateList/PublicStateList.js new file mode 100644 index 0000000..2041cf9 --- /dev/null +++ b/client/src/components/PublicStateList/PublicStateList.js @@ -0,0 +1,15 @@ +import React, { Component } from 'react'; + +class PublicStateList extends Component { + + render() { + return ( +
+

PublicStateList

+
+ ) + } + +}; + +export default PublicStateList; \ No newline at end of file diff --git a/client/src/components/index.js b/client/src/components/index.js index f859b8a..1701703 100644 --- a/client/src/components/index.js +++ b/client/src/components/index.js @@ -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 }; diff --git a/client/src/components/state/CreatePublicState/CreatePublicState.js b/client/src/components/state/CreatePublicState/CreatePublicState.js index 5f82bd6..986cdd7 100644 --- a/client/src/components/state/CreatePublicState/CreatePublicState.js +++ b/client/src/components/state/CreatePublicState/CreatePublicState.js @@ -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 { + + + + +

Admin Dashboard

+ + + ) + } +}; + +export default AdminDashboardPage; diff --git a/client/src/pages/index.js b/client/src/pages/index.js index b6f5a43..2c6b7e9 100644 --- a/client/src/pages/index.js +++ b/client/src/pages/index.js @@ -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, diff --git a/client/src/router/AppRouter.js b/client/src/router/AppRouter.js index 91d90bb..6d02db2 100644 --- a/client/src/router/AppRouter.js +++ b/client/src/router/AppRouter.js @@ -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 that redirects to the login -// screen if user is not yet authenticated. -export const PrivateRoute = ({ children, ...rest }) => { +export const AuthenticatedRoute = ({ children, ...rest }) => { return ( { ); }; +export const AdminRoute = ({ children, ...rest }) => { + return ( + + (rest.user && rest.user.role === 'admin') ? ( + children + ) : ( + + ) + } + /> + ); +}; + class AppRouter extends Component { @@ -66,7 +85,6 @@ class AppRouter extends Component { render() { if (this.state.identityChecked) { - console.log('user: ' + this.props.user); return ( @@ -77,8 +95,10 @@ class AppRouter extends Component { - - +
not an admin
+ + +