Skip to content

Commit 1b478f7

Browse files
authored
Merge pull request #26 from oslabs-beta/Ahnafkhvn/shareFunctionality
Built out publish functionality
2 parents 69688f4 + 2bd5917 commit 1b478f7

File tree

6 files changed

+40
-11
lines changed

6 files changed

+40
-11
lines changed

app/src/components/top/NavBar.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import logo from '../../public/icons/win/logo.png';
1010
import { useSelector, useDispatch } from 'react-redux';
1111
import { publishProject } from '../../helperFunctions/projectGetSaveDel';
1212
import PublishModal from './PublishModal';
13+
import { updateProjectId, updateProjectName, updateProjectPublished } from '../../redux/reducers/slice/appStateSlice';
14+
import { State } from '../../interfaces/Interfaces';
1315

1416

1517
const NavBar = () => {
@@ -23,6 +25,8 @@ const NavBar = () => {
2325
(state: RootState) => state.darkMode.isDarkMode
2426
);
2527

28+
const dispatch = useDispatch();
29+
2630
useEffect(()=>{
2731
setProjectName(state.name)
2832
}, [state.name])//update the ProjectName after state.name changes due to loading projects
@@ -71,15 +75,23 @@ const NavBar = () => {
7175

7276

7377
publishProject(state, projectName)
74-
.then((promise) => {
75-
console.log('Project published successfully', promise);
78+
.then((newProject: State) => {
79+
console.log('Project published successfully', newProject);
7680
setPublishModalOpen(false);
81+
dispatch(updateProjectId(newProject._id))
82+
dispatch(updateProjectName(newProject.name))
83+
dispatch(updateProjectPublished(newProject.published))
7784
})
7885
.catch((error) => {
7986
console.error('Error publishing project:', error.message);
8087
});
8188

82-
};
89+
};
90+
91+
useEffect(()=>{
92+
console.log('stateName = ',state.name);
93+
console.log('published =', state.published);
94+
}, [state.name, state.published])
8395

8496
return (
8597
<nav

app/src/helperFunctions/projectGetSaveDel.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export const saveProject = (
5252
})
5353
.then((res) => res.json())
5454
.then((data) => {
55-
return {_id: data._id, ...data.project}; //passing up what is needed for the global appstateslice
55+
return {_id: data._id, published:data.published, ...data.project}; //passing up what is needed for the global appstateslice
5656
})
5757
.catch((err) => console.log(`Error saving project ${err}`));
5858
return project;//returns _id in addition to the project object from the document
@@ -83,7 +83,8 @@ export const publishProject = (
8383
const publishedProject = response
8484
.then((res) => res.json())
8585
.then((data) => {
86-
return {_id: data._id, ...data.project};
86+
console.log({_id: data._id, published: data.published, ...data.project});
87+
return {_id: data._id, published: data.published, ...data.project};
8788
})
8889
.catch((err) => {
8990
console.log(`Error publishing project ${err}`);

app/src/interfaces/Interfaces.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export interface State {
44
name: string;
55
_id: string;
66
forked: boolean;
7-
_id: string;
7+
published: boolean;
88
isLoggedIn: boolean;
99
components: Component[];
1010
rootComponents: number[];

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export const initialState: State = {
1515
name: '',
1616
_id: '',
1717
forked: false,
18+
published: false,
1819
isLoggedIn: false,
1920
// config: { saveFlag: true, saveTimer: false },
2021
components: [
@@ -775,6 +776,10 @@ const appStateSlice = createSlice({
775776
const projectId = action.payload; //updates the slice with new _id
776777
state._id = projectId;
777778
},
779+
updateProjectPublished: (state, action) => {
780+
const projectPublished = action.payload;
781+
state.published = projectPublished;
782+
},
778783
deleteElement: (state, action) => {
779784
let name: string = '';
780785
const HTMLTypes: HTMLType[] = [...state.HTMLTypes].filter((el) => {
@@ -1298,6 +1303,7 @@ export const {
12981303
resetState,
12991304
updateProjectName,
13001305
updateProjectId,
1306+
updateProjectPublished,
13011307
deleteElement,
13021308
updateAttributes,
13031309
deleteChild,

server/controllers/marketplaceController.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,14 @@ const marketplaceController: MarketplaceController = {
4444
if (userId === req.cookies.ssid) {
4545

4646
if (mongoose.isValidObjectId(_id)) {
47+
48+
const noPub = {...project}
49+
delete noPub.published;
4750
const publishedProject = await Projects.findOneAndUpdate
4851
( // looks in projects collection for project by Mongo id
4952
{ _id },
5053
// update or insert the project
51-
{ project, createdAt, published: true, comments, name, userId, username },
54+
{ project: noPub, createdAt, published: true, comments, name, userId, username },
5255
// Options:
5356
// upsert: true - if none found, inserts new project, otherwise updates it
5457
// new: true - returns updated document not the original one
@@ -58,7 +61,8 @@ const marketplaceController: MarketplaceController = {
5861
return next();
5962
}else{
6063
const noId = {...project};
61-
delete noId._id; //removing the empty string _id from project
64+
delete noId._id; //removing the empty string _id from project
65+
delete noId.published;
6266
const publishedProject = await Projects.create( { project: noId, createdAt, published: true, comments, name, userId, username });
6367
res.locals.publishedProject = publishedProject.toObject({ minimize: false });
6468
console.log('published backend new', res.locals.publishedProject)

server/controllers/projectController.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
import { ProjectController } from '../interfaces';
22
import { Projects } from '../models/reactypeModels';
3+
import { State} from '../../app/src/interfaces/Interfaces'
34

45
// array of objects, objects inside
56
type Projects = { project: {} }[];
67

78
const projectController: ProjectController = {
89
// saveProject saves current workspace to database
910
saveProject: (req, res, next) => {
11+
1012
// pull project name and project itself from body
1113
const { name, project, userId, username, comments } = req.body;
14+
//deleted published from project
15+
const noPub = {...project};
16+
delete noPub.published;
1217
// create createdBy field for the document
1318
const createdAt = Date.now();
1419
// pull ssid from cookies for user id
1520
Projects.findOneAndUpdate(
1621
// looks in projects collection for project by user and name
1722
{ name, userId, username},
1823
// update or insert the project
19-
{ project, createdAt, comments },
24+
{ project: noPub, createdAt, published: false, comments },
2025
// Options:
2126
// upsert: true - if none found, inserts new project, if found, updates project
2227
// new: true - returns updated document not the original one
@@ -39,7 +44,7 @@ const projectController: ProjectController = {
3944
// gets all of current user's projects
4045
getProjects: (req, res, next) => {
4146
const { userId } = req.body;
42-
Projects.find({ userId }, (err, projects: Array<{_id: string; project: any }>) => {
47+
Projects.find({ userId }, (err, projects: Array<{_id: string; published: boolean; project: object }>) => {
4348
if (err) {
4449
return next({
4550
log: `Error in projectController.getProjects: ${err}`,
@@ -49,8 +54,9 @@ const projectController: ProjectController = {
4954
});
5055
}
5156
// so it returns each project like it is in state, not the whole object in DB
52-
res.locals.projects = projects.map((elem) =>({
57+
res.locals.projects = projects.map((elem: {_id: string; published: boolean; project: object } ) =>({
5358
_id: elem._id,
59+
published: elem.published,
5460
...elem.project
5561
}));
5662
return next();

0 commit comments

Comments
 (0)