Skip to content

Commit 14bc569

Browse files
committed
adding and setting up server
1 parent d641789 commit 14bc569

File tree

9 files changed

+116
-27
lines changed

9 files changed

+116
-27
lines changed

.babelrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
}
88
],
99
"react",
10-
"stage-0",
10+
"stage-0"
1111
],
1212
"plugins": ["transform-es2015-modules-commonjs"],
1313
"env": {
1414
"test": {
1515
"plugins": ["transform-es2015-modules-commonjs"]
1616
}
17-
}
17+
}
1818
}

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@
7575
"test:update-snap": "cross-env NODE_ENV=test jest --updateSnapshot",
7676
"test:clear": "cross-env NODE_ENV=test jest --clearCache",
7777
"linter": "eslint src",
78-
"develop": "concurrently \"npm run dev\" \"npm run electron\""
78+
"develop": "concurrently \"npm run dev\" \"npm run electron\"",
79+
"server": "cross-env NODE_ENV=development nodemon server/server.js"
7980
},
8081
"bin": {
8182
"reactype": "./index.js"
@@ -121,10 +122,12 @@
121122
"app-root-path": "^3.0.0",
122123
"autoprefixer": "^9.0.1",
123124
"babel-polyfill": "^6.26.0",
125+
"bcryptjs": "^2.4.3",
124126
"classnames": "^2.2.6",
125127
"cli-spinner": "^0.2.8",
126128
"commander": "^2.17.1",
127129
"concurrently": "^5.1.0",
130+
"cookie-parser": "^1.4.5",
128131
"core-js": "^3.6.4",
129132
"csstype": "^2.6.9",
130133
"d3": "^5.9.2",
@@ -184,11 +187,15 @@
184187
"eslint-plugin-jest": "^21.21.0",
185188
"eslint-plugin-jsx-a11y": "^6.1.1",
186189
"eslint-plugin-react": "^7.10.0",
190+
"express": "^4.17.1",
187191
"extract-text-webpack-plugin": "^4.0.0-beta.0",
188192
"html-webpack-plugin": "^3.1.0",
189193
"identity-obj-proxy": "^3.0.0",
190194
"jest": "^25.2.6",
195+
"mongodb": "^3.5.9",
196+
"mongoose": "^5.9.20",
191197
"node-sass": "^4.13.1",
198+
"nodemon": "^2.0.4",
192199
"postcss-loader": "^2.1.6",
193200
"redux-mock-store": "^1.5.4",
194201
"sass-loader": "^7.0.3",

server/controllers/userController.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
const Users = require('../models/userModels');

server/models/sessionModel.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const mongoose = require('mongoose');
2+
const Schema = mongoose.Schema;
3+
4+
const sessionSchema = new Schema({
5+
cookieId: { type: String, required: true, unique: true },
6+
createdAt: { type: Date, expires: 3600, default: Date.now }
7+
});
8+
9+
const Session = mongoose.model('Sessions', sessionSchema);

server/models/userModels.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const mongoose = require('mongoose');
2+
3+
mongoose
4+
.connect(process.env.MONGO_URI, {
5+
// options for the connect method to parse the URI
6+
useNewUrlParser: true,
7+
useUnifiedTopology: true,
8+
// sets the name of the DB that our collections are part of
9+
dbName: 'ReacType'
10+
})
11+
.then(() => console.log('Connected to Mongo DB.'))
12+
.catch(err => console.log(err));
13+
14+
const Schema = mongoose.Schema;
15+
16+
const userSchema = new Schema({
17+
username: { type: String, required: true, unique: true },
18+
password: { type: String, required: true },
19+
projects: Array
20+
});
21+
22+
// salt will go through 10 rounds of hashing
23+
const SALT_WORK_FACTOR = 10;
24+
const bcrypt = require('bcryptjs');
25+
const { session } = require('electron');
26+
27+
// mongoose middleware that will run before the save to collection happens (user gets put into database)
28+
// cannot use arrow function here as context of 'this' is important
29+
userSchema.pre('save', function(next) {
30+
// within this context, 'this' refers to the document (new user) about to be saved, in our case, it should have properties username, password, and projects array
31+
bcrypt.hash(this.password, SALT_WORK_FACTOR, (err, hash) => {
32+
if (err) {
33+
return next({
34+
log: `bcrypt password hashing error: ${err}`,
35+
message: {
36+
err: `bcrypt hash error: check server logs for details`
37+
}
38+
});
39+
}
40+
this.password = hash;
41+
return next();
42+
});
43+
});
44+
45+
module.exports = mongoose.model('Users', userSchema);

server/server.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
const express = require('express');
2+
const path = require('path');
3+
const cookieParser = require('cookie-parser');
4+
const app = express();
5+
const PORT = 8080;
6+
7+
// handle parsing request body
8+
app.use(express.json());
9+
// cookie parser
10+
app.use(cookieParser());
11+
12+
// statically serve everything in build folder
13+
app.use('/', express.statice(path.resolve(__dirname, '../build')));
14+
15+
// catch-all route handler
16+
app.use('*', (req, res) => {
17+
return res.status(404).send('Page not found');
18+
});
19+
20+
// Global error handler
21+
app.use((err, req, res, next) => {
22+
console.log('invoking global error handler');
23+
const defaultErr = {
24+
log: 'Express error handler caught unknown middleware',
25+
status: 500,
26+
message: { err: 'An error occurred' }
27+
};
28+
29+
const errorObj = Object.assign({}, defaultErr, err);
30+
console.log(errorObj.log);
31+
return res.status(errorObj.status).json(errorObj.message);
32+
});
33+
34+
// starts server on PORT
35+
app.listen(PORT, () => {
36+
console.log(`Server listening on port: ${PORT}`);
37+
});

src/actions/actionCreators.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export const addComponent = ({ title }: { title: string }): Action => ({
5959
type: ADD_COMPONENT,
6060
payload: { title }
6161
});
62+
6263
export const addProp = ({
6364
key,
6465
type

src/components/bottom/HtmlAttr.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class HtmlAttr extends Component<HTMLAttrPropsInt, StateInt> {
170170
<p style={{ color: 'black' }}>
171171
{focusChild.HTMLInfo[attr]
172172
? focusChild.HTMLInfo[attr]
173-
: ' no attribute assigned'}
173+
: 'no attribute assigned'}
174174
</p>
175175
</Paper>
176176
</Grid>

src/containers/RightContainer.tsx

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,13 @@ import { compose } from 'redux';
44
import { withStyles, Theme } from '@material-ui/core/styles';
55
import HtmlAttr from '../components/bottom/HtmlAttr';
66
import { PropsInt, ApplicationStateInt } from '../interfaces/Interfaces';
7-
import {
8-
deleteProp,
9-
addProp,
10-
toggleNative,
11-
changeComponentFocusChild
12-
} from '../actions/actionCreators';
137

14-
interface StateInt {
15-
value: number;
16-
translate: { x: number; y: number };
17-
}
18-
19-
const mapDispatchToProps = (dispatch: any) => ({
20-
addProp: (prop: PropInt) => dispatch(addProp(prop)),
21-
deleteProp: (id: number) => dispatch(deleteProp(id)),
22-
toggleNative: () => dispatch(toggleNative()),
23-
toggleCodeEdit: () => dispatch(toggleCodeEdit())
24-
});
8+
// const mapDispatchToProps = (dispatch: any) => ({
9+
// addProp: (prop: PropInt) => dispatch(addProp(prop)),
10+
// deleteProp: (id: number) => dispatch(deleteProp(id)),
11+
// toggleNative: () => dispatch(toggleNative()),
12+
// toggleCodeEdit: () => dispatch(toggleCodeEdit())
13+
// });
2514

2615
const mapStateToProps = (store: { workspace: ApplicationStateInt }) => ({
2716
focusComponent: store.workspace.focusComponent,
@@ -42,14 +31,14 @@ interface BottomTabsPropsInt extends PropsInt {
4231
codeReadOnly: boolean;
4332
}
4433

45-
class RightContainer extends Component<BottomTabsPropsInt, StateInt> {
34+
class RightContainer extends Component<BottomTabsPropsInt> {
4635
constructor(props: BottomTabsPropsInt) {
4736
super(props);
4837
}
4938

5039
render(): JSX.Element {
5140
const { classes, components, focusComponent, focusChild } = this.props;
52-
41+
// looks through the children of currently focused component (based on left bar) and finds the number of child html elements it has
5342
const htmlAttribCount = focusComponent.childrenArray.filter(
5443
child => child.childType === 'HTML'
5544
).length;
@@ -60,16 +49,16 @@ class RightContainer extends Component<BottomTabsPropsInt, StateInt> {
6049
style={{
6150
minWidth: '400px',
6251
color: 'white',
63-
textAlign: 'center',
64-
display: 'flex',
65-
flexDirection: 'column'
52+
textAlign: 'center'
6653
}}
6754
>
6855
<h3>This is the right column everyone!</h3>
6956
<h4>
57+
{/* mimic the bottom tab label that shows how many html elements there are */}
7058
HTML Element Attributes{' '}
7159
{htmlAttribCount ? `(${htmlAttribCount})` : ''}
7260
</h4>
61+
{/* conditional rendering based on which component/element is currently focused */}
7362
{focusChild.childType === 'HTML' && <HtmlAttr />}
7463
{focusChild.childType !== 'HTML' && (
7564
<p>Please select an HTML element to view attributes</p>
@@ -79,4 +68,4 @@ class RightContainer extends Component<BottomTabsPropsInt, StateInt> {
7968
}
8069
}
8170

82-
export default connect(mapStateToProps, mapDispatchToProps)(RightContainer);
71+
export default connect(mapStateToProps)(RightContainer);

0 commit comments

Comments
 (0)