Skip to content

Commit 81f6c8e

Browse files
authored
Merge pull request #29 from oslabs-beta/denton/share
Denton/share
2 parents d8c48f7 + 73f9ce0 commit 81f6c8e

File tree

6 files changed

+82
-30
lines changed

6 files changed

+82
-30
lines changed

app/src/components/main/DemoRender.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ const DemoRender = (): JSX.Element => {
2323
width: '100%',
2424
backgroundColor: '#FBFBFB',
2525
borderBottom: 'none',
26-
overflow: 'auto'
26+
overflow: 'auto',
27+
2728
};
2829

2930
const html = `
@@ -48,6 +49,10 @@ const DemoRender = (): JSX.Element => {
4849
app.innerHTML = '<div style="color: red;"><h4>Syntax Error</h4>' + err + '</div>';
4950
}
5051
}, false);
52+
const handleClickInsideIframe = () => {
53+
window.parent.postMessage('iframeClicked', '*');
54+
};
55+
window.addEventListener('click', handleClickInsideIframe);
5156
</script>
5257
</body>
5358
</html>
@@ -200,6 +205,7 @@ const DemoRender = (): JSX.Element => {
200205
srcDoc={html}
201206
width="100%"
202207
height="100%"
208+
style = {{zIndex: -30}}
203209
/>
204210
</div>
205211
</>

app/src/components/right/OpenProjects.tsx

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,21 +32,15 @@ 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)
3536
const selectedProject = projects.filter(
36-
(project: any) => project.name === value
37+
(project: any) => project._id === value
3738
)[0];
3839
// dispatch({ type: 'OPEN PROJECT', payload: selectedProject });
3940
console.log(selectedProject);
4041
dispatch(openProject(selectedProject))
4142
onClose();
4243
};
43-
// these two filter between user projects and market projects
44-
// const userProjects = projects.filter((project: any) => {
45-
// project.forked === undefined
46-
// })
47-
// const marketProjects = projects.filter((project: any) => {
48-
// project.forked !== undefined
49-
// })
5044

5145
return (
5246
<Dialog
@@ -60,7 +54,7 @@ function ProjectsDialog(props: ProjectDialogProps) {
6054
{projects.filter((project: any) => project.forked === undefined || project.forked === false).map((project: any, index: number) => (
6155
<ListItem
6256
button
63-
onClick={() => handleListItemClick(project.name)}
57+
onClick={() => handleListItemClick(project._id)}
6458
key={index}
6559
>
6660
<ListItemAvatar>
@@ -76,10 +70,10 @@ function ProjectsDialog(props: ProjectDialogProps) {
7670
{/* this section handles the projects cloned from the marketplace */}
7771
<DialogTitle style={{ color: "#000" }} id="project-dialog-title">MP Projects</DialogTitle>
7872
<List style={{ color: "#000" }}>
79-
{projects.filter((project: any) => project.forked === true).map((project: any, index: number) => (
73+
{projects.filter((project: any) => project.forked === true).map((project: any, index: number) => (
8074
<ListItem
8175
button
82-
onClick={() => handleListItemClick(project.name)}
76+
onClick={() => handleListItemClick(project._id)}
8377
key={index}
8478
>
8579
<ListItemAvatar>

app/src/components/top/NavBar.tsx

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import NewExportButton from './NewExportButton';
88
import { RootState } from '../../redux/store';
99
import logo from '../../public/icons/win/logo.png';
1010
import { useSelector, useDispatch } from 'react-redux';
11-
import { publishProject } from '../../helperFunctions/projectGetSaveDel';
11+
import { publishProject, unpublishProject } from '../../helperFunctions/projectGetSaveDel';
1212
import PublishModal from './PublishModal';
1313
import { updateProjectId, updateProjectName, updateProjectPublished } from '../../redux/reducers/slice/appStateSlice';
1414
import { State } from '../../interfaces/Interfaces';
@@ -88,10 +88,16 @@ const NavBar = () => {
8888

8989
};
9090

91-
useEffect(()=>{
92-
console.log('stateName = ',state.name);
93-
console.log('published =', state.published);
94-
}, [state.name, state.published])
91+
const handleUnpublish = () => {
92+
unpublishProject(state)
93+
.then((unpublishedProject: State) => {
94+
console.log('Project unpublished successfully', unpublishedProject);
95+
dispatch(updateProjectPublished(false));
96+
})
97+
.catch((error) => {
98+
console.error('Error unpublishing project:', error.message);
99+
});
100+
};
95101

96102
return (
97103
<nav
@@ -111,9 +117,15 @@ const NavBar = () => {
111117
</div>
112118
</Link>
113119
<div style={buttonContainerStyle}>
114-
<button style={buttonStyle} onClick={handlePublish}>
115-
Publish
116-
</button>
120+
{state.published ? (
121+
<button style={buttonStyle} onClick={handleUnpublish}>
122+
Unpublish
123+
</button>
124+
) : (
125+
<button style={buttonStyle} onClick={handlePublish}>
126+
Publish
127+
</button>
128+
)}
117129
<NewExportButton />
118130
<Button
119131
style={moreVertButtonStyle}

app/src/components/top/NavBarButtons.tsx

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useRef } from 'react';
1+
import React, { Ref, useEffect, useRef } from 'react';
22
import { useDispatch, useSelector } from 'react-redux';
33

44
import { Button } from '@mui/material';
@@ -295,29 +295,39 @@ function navbarDropDown(props) {
295295
let showMenu = props.dropMenu ? 'navDropDown' : 'hideNavDropDown';
296296

297297
//for closing the menu on clicks outside of it.
298-
const useOutsideClick = (callback) => {
298+
const useOutsideClick = () => {
299299
const dropdownRef = useRef(null);
300300

301301
useEffect(() => {
302302
const handleClick = (event) => {
303-
if (
304-
dropdownRef.current &&
305-
!dropdownRef.current.contains(event.target)
303+
if (event.type === "click" &&
304+
(dropdownRef.current &&
305+
!dropdownRef.current.contains(event.target)) || event.type === "message" && event.data === 'iframeClicked'
306306
) {
307-
callback();
307+
handleClose();
308308
}
309309
};
310-
document.addEventListener('click', handleClick, true);
310+
window.addEventListener('click', handleClick, true);
311+
window.addEventListener('message', handleClick);//to capture clicks in the iframe
311312

312313
return () => {
313-
document.removeEventListener('click', handleClick, true); //cleanup for memory purposes. ensures handleclick isn't called after the component is no longer rendered
314+
window.removeEventListener('click', handleClick, true);
315+
window.addEventListener('message', handleClick); //cleanup for memory purposes. ensures handleclick isn't called after the component is no longer rendered
314316
};
315317
}, [dropdownRef]);
316318

317319
return dropdownRef;
318320
};
319321

320-
const ref = useOutsideClick(handleClose);
322+
const ref = useOutsideClick();
323+
324+
// const handleMessage = (event) => {
325+
// if (event.data === 'iframeClicked') {
326+
// // Handle the click event here, e.g., close the dropdown menu
327+
// handleClose();
328+
// }
329+
// };
330+
// window.addEventListener('message', handleMessage);
321331

322332
return (
323333
// <div ref={dropdownRef} className={showMenu}> dropdownRef making the menu fly off and anchorel messingup

app/src/helperFunctions/projectGetSaveDel.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,36 @@ export const publishProject = (
9494
return publishedProject;
9595
};
9696

97+
export const unpublishProject = (
98+
projectData: State
99+
): Promise<Object> => {
100+
const body = JSON.stringify({
101+
_id: projectData._id,
102+
userId: window.localStorage.getItem('ssid'),
103+
});
104+
105+
const response = fetch(`${serverURL}/unpublishProject`, {
106+
method: 'PATCH',
107+
headers: {
108+
'Content-Type': 'application/json',
109+
},
110+
credentials: 'include',
111+
body,
112+
});
113+
114+
const unpublishedProject = response
115+
.then((res) => res.json())
116+
.then((data) => {
117+
console.log({_id: data._id, published: data.published, ...data.project});
118+
return {_id: data._id, published: data.published, ...data.project};
119+
})
120+
.catch((err) => {
121+
console.log(`Error unpublishing project ${err}`);
122+
throw err;
123+
});
124+
125+
return unpublishedProject;
126+
};
97127

98128
export const deleteProject = (project: any): Promise<Object> => {
99129
const body = JSON.stringify({

server/controllers/marketplaceController.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ const marketplaceController: MarketplaceController = {
3939
publishProject: async (req, res, next) => {
4040
const { _id, project, comments, userId, username, name } = req.body;
4141
const createdAt = Date.now();
42-
console.log('Publish Project', _id, project, comments, userId, username, name )
4342

4443
if (userId === req.cookies.ssid) {
4544

4645
if (mongoose.isValidObjectId(_id)) {
4746

4847
const noPub = {...project}
4948
delete noPub.published;
49+
delete noPub._id;
5050
const publishedProject = await Projects.findOneAndUpdate
5151
( // looks in projects collection for project by Mongo id
5252
{ _id },

0 commit comments

Comments
 (0)