Skip to content

Commit 244dc2e

Browse files
authored
Merge pull request #30 from oslabs-beta/denton/iframe
Denton/iframe
2 parents 81f6c8e + 0f5492f commit 244dc2e

File tree

10 files changed

+77
-64
lines changed

10 files changed

+77
-64
lines changed

app/src/components/App.tsx

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { saveProject } from '../helperFunctions/projectGetSaveDel';
1919
// Intermediary component to wrap main App component with higher order provider components
2020
export const App = (): JSX.Element => {
2121
const state = useSelector((store: RootState) => store.appState);
22+
2223
const [toggleAttempt, setToggleAttempt] = useState(false);
2324
const dispatch = useDispatch();
2425
// checks if user is signed in as guest or actual user and changes loggedIn boolean accordingly
@@ -34,33 +35,33 @@ export const App = (): JSX.Element => {
3435
console.log('state.isLoggedIn', state.isLoggedIn)
3536
// console.log('cookies.get in App', Cookies.get())
3637
// if user is a guest, see if a project exists in localforage and retrieve it
37-
if (!state.isLoggedIn) {
38-
localforage.getItem('guestProject').then((project) => {
39-
// if project exists, use dispatch to set initial state to that project
40-
if (project) {
41-
dispatch(setInitialState(project));
42-
}
43-
});
44-
} else {
45-
// otherwise if a user is logged in, use a fetch request to load user's projects from DB
38+
// if (!state.isLoggedIn) {
39+
// localforage.getItem('guestProject').then((project) => {
40+
// // if project exists, use dispatch to set initial state to that project
41+
// if (project) {
42+
// dispatch(setInitialState(project));
43+
// }
44+
// });
45+
// } else {
46+
// // otherwise if a user is logged in, use a fetch request to load user's projects from DB
4647

47-
let userId;
48-
if (Cookies.get('ssid')) {
49-
userId = Cookies.get('ssid');
50-
} else {
51-
userId = window.localStorage.getItem('ssid');
52-
}
53-
//also load user's last project, which was saved in localforage on logout
54-
localforage.getItem(userId).then((project) => {
55-
if (project) {
56-
dispatch(setInitialState(project));
57-
} else {
58-
console.log(
59-
'No user project found in localforage, setting initial state blank'
60-
);
61-
}
62-
});
63-
}
48+
// let userId;
49+
// if (Cookies.get('ssid')) {
50+
// userId = Cookies.get('ssid');
51+
// } else {
52+
// userId = window.localStorage.getItem('ssid');
53+
// }
54+
// also load user's last project, which was saved in localforage on logout
55+
// localforage.getItem(userId).then((project) => {
56+
// if (project) {
57+
// dispatch(setInitialState(project));
58+
// } else {
59+
// console.log(
60+
// 'No user project found in localforage, setting initial state blank'
61+
// );
62+
// }
63+
// });
64+
// }
6465
}, []);
6566
// useEffect(() => {
6667
// // provide config properties to legacy projects so new edits can be auto saved

app/src/components/main/DemoRender.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Link, Route, BrowserRouter as Router, Switch } from 'react-router-dom';
1+
import { Link, Route, BrowserRouter as Router, Switch, useHistory } from 'react-router-dom';
22
import React, { useEffect, useRef } from 'react';
33
import { useDispatch, useSelector } from 'react-redux';
44

@@ -15,6 +15,7 @@ const DemoRender = (): JSX.Element => {
1515
const stylesheet = useSelector(
1616
(store: RootState) => store.appState.stylesheet
1717
);
18+
const backHome = useHistory();
1819
const dispatch = useDispatch();
1920

2021
// Create React ref to inject transpiled code in inframe
@@ -193,7 +194,12 @@ const DemoRender = (): JSX.Element => {
193194

194195
//adds the code into the iframe
195196
useEffect(() => {
197+
//load the current state code when the iframe is loaded and when code changes
198+
iframe.current.addEventListener('load', ()=>{
199+
iframe.current.contentWindow.postMessage(code, '*');
200+
})
196201
iframe.current.contentWindow.postMessage(code, '*');
202+
197203
}, [code]);
198204

199205
return (

app/src/components/marketplace/MarketplaceCard.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ import {
1414
} from '@mui/material';
1515

1616
import { MoreVert } from '@mui/icons-material';
17-
import React from 'react';
17+
import React, { useEffect } from 'react';
1818
import imageSrc from '../../../../resources/marketplace_images/marketplace_image.png';
1919
import { red } from '@mui/material/colors';
2020
import axios from 'axios';
2121
import { useDispatch, useSelector } from 'react-redux'
2222
import { RootState } from '../../redux/store';
2323
import { saveProject } from '../../helperFunctions/projectGetSaveDel';
2424
import { useHistory } from 'react-router-dom';
25-
import { openProject } from '../../redux/reducers/slice/appStateSlice';
25+
import { openProject, resetAllState } from '../../redux/reducers/slice/appStateSlice';
2626

2727
interface Project {
2828
forked: String,
@@ -50,10 +50,11 @@ const MarketplaceCard = ({proj} :{proj: Project}) => {
5050
const handleClone = async () => { // creates a copy of the project
5151
const docId = proj._id;
5252
const response = await axios.get(`/cloneProject/${docId}`, { params: { username: window.localStorage.getItem('username') } });//passing in username as a query param is query params
53-
const project = response.data.project;
53+
const project = response.data;
54+
console.log('handleClone project', response.data)
5455
alert('Project cloned!');
5556
setAnchorEl(null);
56-
return project;
57+
return {_id: project._id, name: project.name, published: project.published, ...project.project};
5758
};
5859

5960
const handleCloneOpen = async() => {
@@ -66,6 +67,8 @@ const MarketplaceCard = ({proj} :{proj: Project}) => {
6667
};
6768

6869

70+
71+
6972
return (
7073
<>
7174
<Card

app/src/components/marketplace/Searchbar.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const SearchBar = ({marketplaceProjects, updateDisplayProjects }):JSX.Element =>
1414

1515

1616
useEffect(()=>{
17-
1817
if(searchText === ''){
1918

2019
updateDisplayProjects(marketplaceProjects)
@@ -34,7 +33,9 @@ const SearchBar = ({marketplaceProjects, updateDisplayProjects }):JSX.Element =>
3433
//test3 and 1test) would both match for string 'test'
3534

3635
const searchResults = marketplaceProjects.reduce((results, curr) => {
37-
if(curr.name.match(patternString) || curr.username.match(patternString2))
36+
const lowName = curr.name.toLowerCase()
37+
const lowUsername = curr.username.toLowerCase()
38+
if(lowName.match(patternString) || lowUsername.match(patternString2))
3839
results.push(curr)
3940
return results;
4041
}, [])

app/src/components/right/DeleteProjects.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ function ProjectsDialog(props: ProjectDialogProps) {
3939
// If new project selected, close and set value to new project name
4040
const handleDelete = (value: string) => {
4141
const selectedProject = projects.filter(
42-
(project: any) => project.name === value
43-
)[0];
42+
(project: any) => project._id === value
43+
)[0];
44+
console.log('deleting this one', selectedProject)
4445
deleteProject(selectedProject);
4546
localforage.removeItem(window.localStorage.getItem('ssid'));
4647
dispatch(setInitialState(initialState))
@@ -59,7 +60,7 @@ function ProjectsDialog(props: ProjectDialogProps) {
5960
{projects.filter((project: any) => project.forked === undefined || project.forked === false).map((project: any, index: number) => (
6061
<ListItem
6162
button
62-
onClick={() => handleDelete(project.name)}
63+
onClick={() => handleDelete(project._id)}
6364
key={index}
6465
>
6566
<ListItemAvatar>
@@ -76,7 +77,7 @@ function ProjectsDialog(props: ProjectDialogProps) {
7677
{projects.filter((project: any) => project.forked === true).map((project: any, index: number) => (
7778
<ListItem
7879
button
79-
onClick={() => handleDelete(project.name)}
80+
onClick={() => handleDelete(project._id)}
8081
key={index}
8182
>
8283
<ListItemAvatar>

app/src/components/right/OpenProjects.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ function ProjectsDialog(props: ProjectDialogProps) {
3232
};
3333
// If new project selected, close and set value to new project name
3434
const handleListItemClick = (value: string) => {
35-
console.log('hadleList value', value)
3635
const selectedProject = projects.filter(
3736
(project: any) => project._id === value
3837
)[0];

app/src/components/top/NavBar.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,14 @@ const NavBar = () => {
6565
};
6666

6767
const handlePublish = () => {
68-
console.log('projectName', projectName)
69-
console.log('state.name', state.name)
7068
if (state.isLoggedIn === true && projectName === '') {
7169
setInvalidProjectName(true);
7270
setPublishModalOpen(true);
7371
return;
7472
}
7573

7674

77-
publishProject(state, projectName)
75+
publishProject(projectName, state)
7876
.then((newProject: State) => {
7977
console.log('Project published successfully', newProject);
8078
setPublishModalOpen(false);

app/src/helperFunctions/projectGetSaveDel.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,19 @@ export const getProjects = (): Promise<any> => {
2626
return data;
2727
})
2828
.catch((err) => console.log(`Error getting project ${err}`));
29-
return projects;
29+
return projects;//returns an array of projects with _id, name, project
3030
};
3131

3232
export const saveProject = (
33-
name: String,
34-
workspace: Object
33+
name: string,
34+
workspace: State
3535
): Promise<Object> => {
3636
const newProject = { ...workspace}
37-
delete newProject['_id']; //deleting the _id from the current state slice. We don't actually want it in the project object in the mongo db document
37+
delete newProject._id;
38+
delete newProject.name; //deleting the _id from the current state slice. We don't actually want it in the project object in the mongo db document
3839
const body = JSON.stringify({
3940
name,
40-
project: { ...newProject, name },
41+
project: { ...newProject},
4142
userId: window.localStorage.getItem('ssid'),
4243
username: window.localStorage.getItem('username'),
4344
comments: []
@@ -52,23 +53,25 @@ export const saveProject = (
5253
})
5354
.then((res) => res.json())
5455
.then((data) => {
55-
return {_id: data._id, published:data.published, ...data.project}; //passing up what is needed for the global appstateslice
56+
return {_id: data._id, name: data.name, published: data.published, ...data.project}; //passing up what is needed for the global appstateslice
5657
})
5758
.catch((err) => console.log(`Error saving project ${err}`));
5859
return project;//returns _id in addition to the project object from the document
5960
};
6061

6162
export const publishProject = (
62-
projectData: State,
63-
projectName: string
63+
name: string,
64+
workspace: State
6465
): Promise<Object> => {
66+
const newProject = { ...workspace}
67+
delete newProject.name;
6568
const body = JSON.stringify({
66-
_id: projectData._id,
67-
project: { ...projectData, name: projectName },
69+
_id: workspace._id,
70+
name: name,
71+
project: { ...newProject},
6872
userId: window.localStorage.getItem('ssid'),
6973
username: window.localStorage.getItem('username'),
7074
comments: [],
71-
name: projectName,
7275
});
7376

7477
const response = fetch(`${serverURL}/publishProject`, {
@@ -83,8 +86,8 @@ export const publishProject = (
8386
const publishedProject = response
8487
.then((res) => res.json())
8588
.then((data) => {
86-
console.log({_id: data._id, published: data.published, ...data.project});
87-
return {_id: data._id, published: data.published, ...data.project};
89+
console.log({_id: data._id, name: data.name, published:data.published, ...data.project});
90+
return {_id: data._id, name: data.name, published:data.published, ...data.project};
8891
})
8992
.catch((err) => {
9093
console.log(`Error publishing project ${err}`);
@@ -114,8 +117,8 @@ export const unpublishProject = (
114117
const unpublishedProject = response
115118
.then((res) => res.json())
116119
.then((data) => {
117-
console.log({_id: data._id, published: data.published, ...data.project});
118-
return {_id: data._id, published: data.published, ...data.project};
120+
console.log({_id: data._id, name: data.name, published:data.published, ...data.project});
121+
return {_id: data._id, name: data.name, published:data.published, ...data.project};
119122
})
120123
.catch((err) => {
121124
console.log(`Error unpublishing project ${err}`);
@@ -127,7 +130,7 @@ export const unpublishProject = (
127130

128131
export const deleteProject = (project: any): Promise<Object> => {
129132
const body = JSON.stringify({
130-
name: project.name,
133+
_id: project._id,
131134
userId: window.localStorage.getItem('ssid')
132135
});
133136
const deletedProject = fetch(`${serverURL}/deleteProject`, {
@@ -140,7 +143,7 @@ export const deleteProject = (project: any): Promise<Object> => {
140143
})
141144
.then((res) => res.json())
142145
.then((data) => {
143-
return data;
146+
return {_id: data._id, name: data.name, published:data.published, ...data.project};
144147
})
145148
.catch((err) => console.log(`Error deleting project ${err}`));
146149
return deletedProject;

server/controllers/marketplaceController.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ const marketplaceController: MarketplaceController = {
6363
const noId = {...project};
6464
delete noId._id; //removing the empty string _id from project
6565
delete noId.published;
66-
const publishedProject = await Projects.create( { project: noId, createdAt, published: true, comments, name, userId, username });
67-
res.locals.publishedProject = publishedProject.toObject({ minimize: false });
66+
const publishedProject = await Projects.create( { project: noId, createdAt, published: true, comments, name, userId, username })
67+
res.locals.publishedProject = publishedProject.toObject({ minimize: false });
6868
console.log('published backend new', res.locals.publishedProject)
6969
return next();
7070
}
@@ -101,7 +101,7 @@ const marketplaceController: MarketplaceController = {
101101
}
102102
});
103103
}
104-
res.locals.unpublishedProject = result;
104+
res.locals.unpublishedProject = result.toObject({ minimize: false });
105105
return next();
106106
});
107107
}

server/controllers/projectController.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ const projectController: ProjectController = {
5454
});
5555
}
5656
// so it returns each project like it is in state, not the whole object in DB
57-
res.locals.projects = projects.map((elem: {_id: string; published: boolean; project: object } ) =>({
57+
res.locals.projects = projects.map((elem: {_id: string; name: string; published: boolean; project: object } ) =>({
5858
_id: elem._id,
59+
name: elem.name,
5960
published: elem.published,
6061
...elem.project
6162
}));
@@ -67,8 +68,8 @@ const projectController: ProjectController = {
6768
// delete project from database **currently not integrated into app**
6869
deleteProject: (req, res, next) => {
6970
// pull project name and userId from req.body
70-
const { name, userId } = req.body;
71-
Projects.findOneAndDelete({ name, userId }, null, (err, deleted) => {
71+
const { _id, userId } = req.body;
72+
Projects.findOneAndDelete({ _id, userId }, null, (err, deleted) => {
7273
if (err) {
7374
return next({
7475
log: `Error in projectController.deleteProject: ${err}`,

0 commit comments

Comments
 (0)