From 2eebc4f79ecd0c22d9c57c14788f649ff590f87d Mon Sep 17 00:00:00 2001 From: oitsjustjose Date: Tue, 20 Oct 2020 22:27:36 -0500 Subject: [PATCH] Additional progress towards completion! --- backend/src/app.ts | 4 +- .../src/app/controllers/configPutHandler.ts | 8 +- backend/src/app/middleware.ts | 23 +++++ backend/src/db.ts | 2 +- frontend/src/App.jsx | 2 + frontend/src/axios/Config/GetConfig.js | 4 + frontend/src/axios/Config/GetConfigs.js | 7 ++ frontend/src/axios/Config/PutConfig.js | 8 ++ frontend/src/axios/Generate.js | 6 -- frontend/src/components/modules/NavBar.jsx | 2 +- frontend/src/components/view/ConfigBrowse.jsx | 64 +++++++++++++ frontend/src/components/view/ConfigCreate.jsx | 93 ++++++++++++------- frontend/src/components/view/ConfigRender.jsx | 15 +++ .../src/components/view/ConfigRenderStone.jsx | 1 - frontend/src/css/App.css | 19 +++- frontend/src/redux/reducers.js | 5 + 16 files changed, 213 insertions(+), 50 deletions(-) create mode 100644 frontend/src/axios/Config/GetConfig.js create mode 100644 frontend/src/axios/Config/GetConfigs.js create mode 100644 frontend/src/axios/Config/PutConfig.js delete mode 100644 frontend/src/axios/Generate.js create mode 100644 frontend/src/components/view/ConfigBrowse.jsx create mode 100644 frontend/src/components/view/ConfigRender.jsx diff --git a/backend/src/app.ts b/backend/src/app.ts index ee988eb..bdd4c18 100644 --- a/backend/src/app.ts +++ b/backend/src/app.ts @@ -6,7 +6,7 @@ import { registerPostHandler } from './app/controllers/registerPostHandler' import { configGetHandler, configGetSpecificHandler } from './app/controllers/configGetHandler' import { configPutHandler } from './app/controllers/configPutHandler' import { UserModel } from './app/db/user.schema' -import { auth } from './app/middleware' +import { auth, optAuth } from './app/middleware' interface AuthReq { user: UserModel | null @@ -26,8 +26,8 @@ app.use(bodyParser.urlencoded({ extended: true })) +app.put('/api/configs', optAuth, configPutHandler) app.get('/api/configs', auth, configGetHandler) -app.put('/api/configs', auth, configPutHandler) app.get('/api/configs/:id', configGetSpecificHandler) app.post('/api/users/login', loginPostHandler) app.post('/api/users/register', registerPostHandler) diff --git a/backend/src/app/controllers/configPutHandler.ts b/backend/src/app/controllers/configPutHandler.ts index ed1e9c2..b7cfb6d 100644 --- a/backend/src/app/controllers/configPutHandler.ts +++ b/backend/src/app/controllers/configPutHandler.ts @@ -7,10 +7,12 @@ export const configPutHandler = async (req: Request, res: Response) => { config.json = req.body.json await config.save() - req.user?.configs.push(config._id) - await req.user.save() + if(req.user) { + req.user.configs.push(config._id) + await req.user.save() + } - return res.status(200).json(config.json) + return res.status(200).send(config._id) } catch (ex) { console.error(ex) return res.status(500).send({ error: ex }) diff --git a/backend/src/app/middleware.ts b/backend/src/app/middleware.ts index 65ef482..3979972 100644 --- a/backend/src/app/middleware.ts +++ b/backend/src/app/middleware.ts @@ -3,6 +3,29 @@ import * as jwt from 'jsonwebtoken' import UserSchema from './db/user.schema' import moment from 'moment' +export const optAuth = async (req: Request, res: Response, next: NextFunction) => { + const token = req.header('auth-token') + + if (!token) { + return next() + } + + try { + const { _id, expiresAt } = (jwt.verify(token, process.env.JWT_SECRET as string) as { _id: string, expiresAt: string }) + + if (moment(moment()).isAfter(expiresAt)) { + return next() + } + + const user = await UserSchema.findById(_id) + req.user = user || null + + return next() + } catch (err) { + return res.status(400).send(`Invalid Token: ${err}`) + } +} + export const auth = async (req: Request, res: Response, next: NextFunction) => { const token = req.header('auth-token') diff --git a/backend/src/db.ts b/backend/src/db.ts index 545b480..9995691 100644 --- a/backend/src/db.ts +++ b/backend/src/db.ts @@ -1,7 +1,7 @@ import mongoose from 'mongoose' export default async () => { - await mongoose.connect(`mongodb://${process.env.MONGO_URI}/Blog`, { + await mongoose.connect(`mongodb://${process.env.MONGO_URI}/GeolosysConfigGen`, { useNewUrlParser: true, useUnifiedTopology: true }) diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index bc5dd0c..d35c0b8 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -5,6 +5,7 @@ import { Provider } from 'react-redux'; import { BrowserRouter as Router, Route } from 'react-router-dom'; import LoadAuth from './auth/load'; import NavBar from './components/modules/NavBar'; +import ConfigBrowse from './components/view/ConfigBrowse'; import ConfigCreate from './components/view/ConfigCreate'; import Login from './components/view/Login'; import Register from './components/view/Register'; @@ -20,6 +21,7 @@ export default () => {
+ , diff --git a/frontend/src/axios/Config/GetConfig.js b/frontend/src/axios/Config/GetConfig.js new file mode 100644 index 0000000..48da236 --- /dev/null +++ b/frontend/src/axios/Config/GetConfig.js @@ -0,0 +1,4 @@ +import axios from 'axios'; + +export default (id) => axios.get(`/api/configs/${id}`) + .then((resp) => resp.data); diff --git a/frontend/src/axios/Config/GetConfigs.js b/frontend/src/axios/Config/GetConfigs.js new file mode 100644 index 0000000..3cfb8cb --- /dev/null +++ b/frontend/src/axios/Config/GetConfigs.js @@ -0,0 +1,7 @@ +import axios from 'axios'; +import store from '../../redux/store'; + +export default () => axios.get('/api/configs/') + .then((resp) => { + store.dispatch({ type: 'SET_CONFIGS', configs: resp.data || [] }); + }); diff --git a/frontend/src/axios/Config/PutConfig.js b/frontend/src/axios/Config/PutConfig.js new file mode 100644 index 0000000..05663c2 --- /dev/null +++ b/frontend/src/axios/Config/PutConfig.js @@ -0,0 +1,8 @@ +import axios from 'axios'; +import GetConfigs from './GetConfigs'; + +export default (data) => axios.put('/api/configs', data) + .then((resp) => { + GetConfigs(); + return resp.data; + }); diff --git a/frontend/src/axios/Generate.js b/frontend/src/axios/Generate.js deleted file mode 100644 index 783fe9b..0000000 --- a/frontend/src/axios/Generate.js +++ /dev/null @@ -1,6 +0,0 @@ -import axios from 'axios'; - -export default (data) => { - console.log(data); - axios.post('/api/configs', data); -}; diff --git a/frontend/src/components/modules/NavBar.jsx b/frontend/src/components/modules/NavBar.jsx index 2638243..9ae19c5 100644 --- a/frontend/src/components/modules/NavBar.jsx +++ b/frontend/src/components/modules/NavBar.jsx @@ -28,7 +28,7 @@ const NavBarComponent = ({ {store.getState().isAuthenticated && ( - + )} {store.getState().isAuthenticated && ( diff --git a/frontend/src/components/view/ConfigBrowse.jsx b/frontend/src/components/view/ConfigBrowse.jsx new file mode 100644 index 0000000..72c62f2 --- /dev/null +++ b/frontend/src/components/view/ConfigBrowse.jsx @@ -0,0 +1,64 @@ +import React from 'react'; +import { connect } from 'react-redux'; +import { Table, Container } from 'react-bootstrap'; +import { CSSTransition } from 'react-transition-group'; +import GetConfigs from '../../axios/Config/GetConfigs'; + +const ConfigViewComponent = ({ + configs, +}) => { + if (!Array.isArray(configs)) { + GetConfigs(); + return ( + +

Loading...

+
+ ); + } + + if (!configs.length) { + return ( + +
+

+ No Configs Found + {' '} + 🙁 +

+
+
+ ); + } + + return ( + +
+

Your Configs

+ + + + + + + + + + {configs.map((slug, idx) => ( + + + + + ))} + +
#URL
{idx + 1}{slug}
+
+
+
+ ); +}; + +const mapStateToProps = (state) => ({ + configs: state.configs, +}); + +export default connect(mapStateToProps)(ConfigViewComponent); diff --git a/frontend/src/components/view/ConfigCreate.jsx b/frontend/src/components/view/ConfigCreate.jsx index a8b2980..c8404ed 100644 --- a/frontend/src/components/view/ConfigCreate.jsx +++ b/frontend/src/components/view/ConfigCreate.jsx @@ -1,13 +1,19 @@ import React, { useState } from 'react'; -import { Accordion, Container, Button } from 'react-bootstrap'; +import { Accordion, Button, Container } from 'react-bootstrap'; import { connect } from 'react-redux'; +import { CSSTransition } from 'react-transition-group'; +import PutConfig from '../../axios/Config/PutConfig'; +import GetConfig from '../../axios/Config/GetConfig'; +import ConfigRender from './ConfigRender'; import ConfigRenderOre from './ConfigRenderOre'; import ConfigRenderStone from './ConfigRenderStone'; -import PostGenerateJson from '../../axios/Generate'; const Configurator = ({ mc114, }) => { + const params = new URLSearchParams(window.location.search); + const [postRender, setPostRender] = useState(null); + const [oreConfigs, setOreConfigs] = useState([true]); const [stoneConfigs, setStoneConfigs] = useState([true]); const [oreState, setOreState] = useState([{ @@ -32,6 +38,21 @@ const Configurator = ({ dimBlacklist: [], }]); + if (params.has('id')) { + if (postRender) { + return (); + } + + GetConfig(params.get('id')).then((data) => ( + setPostRender(data) + )); + return ( + +

Loading...

+
+ ); + } + const addOreEntry = () => { setOreConfigs([...oreConfigs, true]); }; @@ -45,8 +66,6 @@ const Configurator = ({ const onOreConfigChange = (idx, oreConfigState) => { const newState = [...oreState]; newState[idx] = oreConfigState; - console.log('Ore State Change', idx, oreConfigState); - console.log(newState); setOreState(newState); }; @@ -63,22 +82,21 @@ const Configurator = ({ const onStoneConfigChange = (idx, stoneConfigState) => { const newState = [...stoneState]; newState[idx] = stoneConfigState; - console.log('Stone State Change', idx, stoneConfigState); - console.log(newState); setStoneState(newState); }; return ( -
-
- Geolosys Logo -

Geolosys Deposit Configurator

-
+ +
+
+ Geolosys Logo +

Geolosys Deposit Configurator

+
- -

Ores

- - {oreConfigs.map((enabled, idx) => enabled && ( + +

Ores

+ + {oreConfigs.map((enabled, idx) => enabled && ( - ))} - -
+ ))} +
+
- -

Stones

- - {stoneConfigs.map((enabled, idx) => enabled && ( + +

Stones

+ + {stoneConfigs.map((enabled, idx) => enabled && ( - ))} - -
+ ))} +
+
- -
+ +
+ ); }; diff --git a/frontend/src/components/view/ConfigRender.jsx b/frontend/src/components/view/ConfigRender.jsx new file mode 100644 index 0000000..203735e --- /dev/null +++ b/frontend/src/components/view/ConfigRender.jsx @@ -0,0 +1,15 @@ +import React from 'react'; +import { Container } from 'react-bootstrap'; + +export default ({ + jsonData, +}) => ( +
+

Config View

+ +
+        {JSON.stringify(jsonData.json, null, 2) }
+      
+
+
+); diff --git a/frontend/src/components/view/ConfigRenderStone.jsx b/frontend/src/components/view/ConfigRenderStone.jsx index 6690e53..c4807d0 100644 --- a/frontend/src/components/view/ConfigRenderStone.jsx +++ b/frontend/src/components/view/ConfigRenderStone.jsx @@ -19,7 +19,6 @@ export default ({ }); const setStateWithPropogation = (newState) => { - console.log(newState); setState(newState); onConfigChange(idx, newState); }; diff --git a/frontend/src/css/App.css b/frontend/src/css/App.css index 54f6101..22b9cec 100644 --- a/frontend/src/css/App.css +++ b/frontend/src/css/App.css @@ -1,3 +1,20 @@ +@font-face { + font-family: 'JetBrainsMono'; + src: url('https://cdn.jsdelivr.net/gh/JetBrains/JetBrainsMono/web/woff2/JetBrainsMono-Regular.woff2') format('woff2'), url('https://cdn.jsdelivr.net/gh/JetBrains/JetBrainsMono/web/woff/JetBrainsMono-Regular.woff') format('woff'), url('https://cdn.jsdelivr.net/gh/JetBrains/JetBrainsMono/ttf/JetBrainsMono-Regular.ttf') format('truetype'); + font-weight: 400; + font-style: normal; +} + +pre { + font-family: 'JetBrainsMono'; + font-variant-ligatures: common-ligatures; +} + +.container.perfect-width { + max-width: 768px; + width: 98%; +} + .perfect-width { max-width: 384px; width: 85%; @@ -37,4 +54,4 @@ img.header { .switch-nav-item, .switch-nav-item * { cursor: pointer !important; color: #fff; -} +} \ No newline at end of file diff --git a/frontend/src/redux/reducers.js b/frontend/src/redux/reducers.js index c729c78..383e6fd 100644 --- a/frontend/src/redux/reducers.js +++ b/frontend/src/redux/reducers.js @@ -28,6 +28,11 @@ export default function reducer( ...state, mc114: !state.mc114, }; + case 'SET_CONFIGS': + return { + ...state, + configs: action.configs, + }; default: return state; }