Skip to content

Commit 06049b0

Browse files
committed
should merge to different feature branch
1 parent 1301c21 commit 06049b0

File tree

7 files changed

+75
-8
lines changed

7 files changed

+75
-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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ app.get(
8181
sessionController.isLoggedIn,
8282
projectController.getProjects,
8383
(req, res) => {
84-
return res.status(200).json(res.locals.projects);
84+
return res.status(200).json(res.locals.project);
8585
}
8686
);
8787

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 projects 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
@@ -233,6 +233,9 @@ const reducer = (state: State, action: Action) => {
233233
const child = { ...directParent.children[childIndexValue] };
234234
directParent.children.splice(childIndexValue, 1);
235235
}
236+
case 'SET INITIAL STATE': {
237+
return { ...action.payload };
238+
}
236239

237240
default:
238241
return state;

0 commit comments

Comments
 (0)