Skip to content

Commit 225de92

Browse files
committed
Worked on publishing features
1 parent 2bd5917 commit 225de92

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

app/src/components/top/NavBar.tsx

Lines changed: 31 additions & 4 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';
@@ -92,6 +92,27 @@ const NavBar = () => {
9292
console.log('stateName = ',state.name);
9393
console.log('published =', state.published);
9494
}, [state.name, state.published])
95+
96+
// handleUnpublish = () => {
97+
// .then((project:State) => {
98+
// dispatch(updateProjectPublished(project.published(false));
99+
// })
100+
// }
101+
102+
const handleUnpublish = () => {
103+
unpublishProject(state)
104+
.then((unpublishedProject: State) => {
105+
console.log('Project unpublished successfully', unpublishedProject);
106+
dispatch(updateProjectPublished(false));
107+
})
108+
.catch((error) => {
109+
console.error('Error unpublishing project:', error.message);
110+
});
111+
};
112+
113+
// In handlePublish pass in state
114+
//check to see if user is logged the same way as in publish project for the most part
115+
// then somehow do dispatch(updateProjectPublished(newProject.published === false))
95116

96117
return (
97118
<nav
@@ -111,9 +132,15 @@ const NavBar = () => {
111132
</div>
112133
</Link>
113134
<div style={buttonContainerStyle}>
114-
<button style={buttonStyle} onClick={handlePublish}>
115-
Publish
116-
</button>
135+
{state.published ? (
136+
<button style={buttonStyle} onClick={handleUnpublish}>
137+
Unpublish
138+
</button>
139+
) : (
140+
<button style={buttonStyle} onClick={handlePublish}>
141+
Publish
142+
</button>
143+
)}
117144
<NewExportButton />
118145
<Button
119146
style={moreVertButtonStyle}

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({

0 commit comments

Comments
 (0)