Skip to content

Commit 9051c69

Browse files
authored
Merge pull request #22 from oslabs-beta/staging-faast
Project management
2 parents ac1bfd1 + fe71bea commit 9051c69

File tree

7 files changed

+78
-8
lines changed

7 files changed

+78
-8
lines changed

server/controllers/projectController.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ projectController.saveProject = (req, res, next) => {
1414
{ name, userId },
1515
// update or insert the project
1616
{ project },
17-
// Options: upsert: true - if none found, inserts new project, if found, updates project
17+
// Options:
18+
// upsert: true - if none found, inserts new project, if found, updates project
1819
// new: true - returns updated document not the original one
1920
{ upsert: true, new: true },
2021
(err, result) => {
@@ -39,7 +40,7 @@ projectController.saveProject = (req, res, next) => {
3940
projectController.getProjects = (req, res, next) => {
4041
console.log('Inside projectController.getProjects...');
4142
const userId = req.cookies.ssid;
42-
Projects.find({ userId }, (err, projects) => {
43+
Projects.findOne({ userId }, (err, project) => {
4344
if (err) {
4445
return next({
4546
log: `Error in projectController.getProjects: ${err}`,
@@ -49,7 +50,7 @@ projectController.getProjects = (req, res, next) => {
4950
});
5051
} else {
5152
console.log('Successful getProjects');
52-
res.locals.projects = projects;
53+
res.locals.project = project;
5354
return next();
5455
}
5556
});

server/controllers/sessionController.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ sessionController.isLoggedIn = (req, res, next) => {
1717
// no session found, redirect to signup page
1818
} else if (!session) {
1919
console.log('No session found, redirecting to signup page');
20-
return res.redirect('/signup');
20+
return res.redirect('/');
2121
} else {
2222
// session found, move onto next middleware
2323
console.log('Session found, moving onto next middleware');

server/models/reactypeModels.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ const sessionSchema = new Schema({
5252
});
5353

5454
const projectSchema = new Schema({
55-
name: { type: String, required: true },
56-
project: Object,
55+
name: String,
56+
project: { type: Object, required: true },
5757
userId: {
5858
type: Schema.Types.ObjectId,
5959
ref: 'Users'

server/server.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,14 @@ app.use(cookieParser());
3838

3939
// statically serve everything in build folder
4040
app.use('/', express.static(path.resolve(__dirname, '../build')));
41+
// statically serve images
4142
app.use(
4243
'/images',
4344
express.static(path.resolve(__dirname, '..src/public/images'))
4445
);
4546

47+
// serves main app, we are going to serve main app to electron locally, this will be deprecated
48+
// this backend server will only be used for authentication and project management services
4649
app.get('/', (req, res) => {
4750
res.status(200).sendFile(path.resolve(__dirname, '../build/index.html'));
4851
});
@@ -81,7 +84,7 @@ app.get(
8184
sessionController.isLoggedIn,
8285
projectController.getProjects,
8386
(req, res) => {
84-
return res.status(200).json(res.locals.projects);
87+
return res.status(200).json(res.locals.project);
8588
}
8689
);
8790

src/components/AppNew.tsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useReducer } from 'react';
1+
import React, { useState, useReducer, useEffect } from 'react';
22
import '../public/styles/style.css';
33
import '../public/styles/styleNew.css';
44
import { DndProvider } from 'react-dnd';
@@ -7,12 +7,41 @@ import AppContainer from '../containers/AppContainer';
77
import { stateContext } from '../context/context';
88
import initialState from '../context/initialState';
99
import reducer from '../reducers/componentReducerNew';
10+
import { getProjects } from '../helperFunctions/projectGetSave';
11+
import { saveProject } from '../helperFunctions/projectGetSave';
12+
import { loadInitData } from '../actions/actionCreators';
1013
// import { Context, State } from '../interfaces/InterfacesNew';
1114

1215
// Intermediary component to wrap main App component with higher order provider components
1316
export const App = (): JSX.Element => {
1417
// const [context, setContext] = useState(initialState);
18+
//let initialStateLoaded = false;
19+
// retrieves user's project (if it exists) from DB on component load
1520
const [state, dispatch] = useReducer(reducer, initialState);
21+
22+
// gets projects from DB for current user on mount
23+
useEffect(() => {
24+
// getProjects returns a promise which is thenable
25+
getProjects().then(project => {
26+
if (project) {
27+
// if user has project we run a dispatch to update state with received project
28+
dispatch({
29+
type: 'SET INITIAL STATE',
30+
payload: project
31+
});
32+
}
33+
});
34+
}, []);
35+
36+
// saves project to DB whenever there are changes to the state via this canvas component
37+
useEffect(() => {
38+
console.log('useEffect in CanvasNew ran');
39+
// setTimeout is necessary so the saveProjects method does not fire and save an empty project before the initial getProjects in AppNew
40+
setTimeout(() => {
41+
saveProject(state);
42+
}, 1000);
43+
}, [state]);
44+
1645
return (
1746
<div className="app">
1847
<DndProvider backend={HTML5Backend}>

src/helperFunctions/projectGetSave.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export const getProjects = (): Promise<Object> => {
2+
console.log("Loading user's projects...");
3+
const project = fetch('https://localhost:8080/getProjects', {
4+
credentials: 'include'
5+
})
6+
.then(res => res.json())
7+
.then(data => {
8+
console.log("User's project is", data.project);
9+
return data.project;
10+
})
11+
.catch(err => console.log(`Error getting project ${err}`));
12+
return project;
13+
};
14+
15+
export const saveProject = (workspace: Object): Promise<Object> => {
16+
console.log("Saving user's project...");
17+
18+
const body = JSON.stringify({ name: 'Andrew project', project: workspace });
19+
const project = fetch('https://localhost:8080/saveProject', {
20+
method: 'POST',
21+
headers: {
22+
'content-type': 'application/json'
23+
},
24+
credentials: 'include',
25+
body
26+
})
27+
.then(res => res.json())
28+
.then(data => {
29+
console.log('Saved project is', data.project);
30+
return data.project;
31+
})
32+
.catch(err => console.log(`Error saving project ${err}`));
33+
return project;
34+
};

src/reducers/componentReducerNew.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,9 @@ const reducer = (state: State, action: Action) => {
248248
component.code = generateCode(components, state.canvasFocus.componentId);
249249
return { ...state, components };
250250
}
251+
case 'SET INITIAL STATE': {
252+
return { ...action.payload };
253+
}
251254

252255
default:
253256
return state;

0 commit comments

Comments
 (0)