Skip to content

Commit e02c063

Browse files
committed
publish button works
1 parent e8cc276 commit e02c063

File tree

4 files changed

+46
-37
lines changed

4 files changed

+46
-37
lines changed

app/src/components/top/NavBar.tsx

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import { Link } from 'react-router-dom';
33
import Avatar from '@mui/material/Avatar';
44
import Button from '@mui/material/Button';
@@ -23,6 +23,10 @@ const NavBar = () => {
2323
(state: RootState) => state.darkMode.isDarkMode
2424
);
2525

26+
useEffect(()=>{
27+
setProjectName(state.name)
28+
}, [state.name])//update the ProjectName after state.name changes due to loading projects
29+
2630
const buttonContainerStyle = {
2731
display: 'flex',
2832
alignItems: 'center',
@@ -57,22 +61,24 @@ const NavBar = () => {
5761
};
5862

5963
const handlePublish = () => {
64+
console.log('projectName', projectName)
65+
console.log('state.name', state.name)
6066
if (state.isLoggedIn === true && projectName === '') {
6167
setInvalidProjectName(true);
6268
setPublishModalOpen(true);
6369
return;
6470
}
6571

66-
if (state.name === '') {
67-
publishProject(state, projectName)
68-
.then(() => {
69-
console.log('Project published successfully');
70-
setPublishModalOpen(false);
71-
})
72-
.catch((error) => {
73-
console.error('Error publishing project:', error.message);
74-
});
75-
}
72+
73+
publishProject(state, projectName)
74+
.then((promise) => {
75+
console.log('Project published successfully', promise);
76+
setPublishModalOpen(false);
77+
})
78+
.catch((error) => {
79+
console.error('Error publishing project:', error.message);
80+
});
81+
7682
};
7783

7884
return (

app/src/helperFunctions/projectGetSaveDel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export const publishProject = (
8383
const publishedProject = response
8484
.then((res) => res.json())
8585
.then((data) => {
86-
return data.project;
86+
return {_id: data._id, ...data.project};
8787
})
8888
.catch((err) => {
8989
console.log(`Error publishing project ${err}`);

app/src/redux/reducers/slice/appStateSlice.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ export const initialState: State = {
1515
name: '',
1616
_id: '',
1717
forked: false,
18-
_id: '',
1918
isLoggedIn: false,
2019
// config: { saveFlag: true, saveTimer: false },
2120
components: [

server/controllers/marketplaceController.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Project from '../graphQL/resolvers/query';
22
import { MarketplaceController } from '../interfaces';
33
import { Projects, Users } from '../models/reactypeModels';
4+
import mongoose from 'mongoose';
45

56
// array of objects, objects inside
67
type Projects = { project: {} }[];
@@ -33,36 +34,39 @@ const marketplaceController: MarketplaceController = {
3334
/**
3435
*
3536
* Middleware function that publishes (and saves) a project to the database
36-
* @return sends the updated project to the frontend
37+
* @return sends the updated entire project document to the frontend
3738
*/
38-
publishProject: (req, res, next) => {
39+
publishProject: async (req, res, next) => {
3940
const { _id, project, comments, userId, username, name } = req.body;
4041
const createdAt = Date.now();
42+
console.log('Publish Project', _id, project, comments, userId, username, name )
43+
4144
if (userId === req.cookies.ssid) {
42-
Projects.findOneAndUpdate(
43-
// looks in projects collection for project by Mongo id
44-
{ _id },
45-
// update or insert the project
46-
{ project, createdAt, published: true, comments, name, userId, username },
47-
// Options:
48-
// upsert: true - if none found, inserts new project, otherwise updates it
49-
// new: true - returns updated document not the original one
50-
{ upsert: true, new: true },
51-
(err, result) => {
52-
if (err) {
53-
return next({
54-
log: `Error in marketplaceController.publishProject: ${err}`,
55-
message: {
56-
err: 'Error in marketplaceController.publishProject, check server logs for details'
57-
}
58-
});
59-
}
60-
res.locals.publishedProject = result; //returns the entire document
61-
return next();
62-
}
63-
);
45+
46+
if (mongoose.isValidObjectId(_id)) {
47+
const publishedProject = await Projects.findOneAndUpdate
48+
( // looks in projects collection for project by Mongo id
49+
{ _id },
50+
// update or insert the project
51+
{ project, createdAt, published: true, comments, name, userId, username },
52+
// Options:
53+
// upsert: true - if none found, inserts new project, otherwise updates it
54+
// new: true - returns updated document not the original one
55+
{ upsert: true, new: true }
56+
);
57+
res.locals.publishedProject = publishedProject;
58+
return next();
59+
}else{
60+
const noId = {...project};
61+
delete noId._id; //removing the empty string _id from project
62+
const publishedProject = await Projects.create( { project: noId, createdAt, published: true, comments, name, userId, username });
63+
res.locals.publishedProject = publishedProject.toObject({ minimize: false });
64+
console.log('published backend new', res.locals.publishedProject)
65+
return next();
66+
}
6467
}
6568
else {
69+
console.log('userId did not match')
6670
// we should not expect a user to be able to access another user's id, but included error handling for unexpected errors
6771
return next({
6872
log: 'Error in marketplaceController.publishProject',

0 commit comments

Comments
 (0)