Skip to content

Commit 2dd463e

Browse files
committed
changed saveProject to update with _id from mongo
1 parent f73e6d6 commit 2dd463e

File tree

4 files changed

+16
-4
lines changed

4 files changed

+16
-4
lines changed

app/src/components/right/SaveProjectButton.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import DialogTitle from '@mui/material/DialogTitle';
88
import SaveOutlinedIcon from '@mui/icons-material/SaveOutlined';
99
import { saveProject } from '../../helperFunctions/projectGetSaveDel';
1010
import {useDispatch, useSelector} from 'react-redux'
11-
import {updateProjectName} from '../../redux/reducers/slice/appStateSlice';
11+
import {updateProjectName, updateProjectId} from '../../redux/reducers/slice/appStateSlice';
1212
import { RootState } from '../../redux/store';
13+
import { State } from '../../interfaces/Interfaces';
1314

1415
export default function FormDialog() {
1516
const [open, setOpen] = useState(false);
@@ -34,7 +35,7 @@ const dispatch = useDispatch();
3435
// If errors occur on the backend, the project name still gets updated
3536

3637
dispatch(updateProjectName(projectName))
37-
saveProject(projectName, state).then((project) => {console.log(project)})
38+
saveProject(projectName, state).then((project: State) => dispatch(updateProjectId(project._id)))//updates the slice with new _id from mongo
3839
setOpen(false);
3940
} else {
4041
setInvalidProjectName(true);

app/src/helperFunctions/projectGetSaveDel.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { State } from "../interfaces/Interfaces";
2+
13
const isDev = process.env.NODE_ENV === 'development';
24
const { DEV_PORT, API_BASE_URL } = require('../../../config.js');
35
let serverURL = API_BASE_URL;
@@ -31,9 +33,11 @@ export const saveProject = (
3133
name: String,
3234
workspace: Object
3335
): Promise<Object> => {
36+
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
3438
const body = JSON.stringify({
3539
name,
36-
project: { ...workspace, name },
40+
project: { ...newProject, name },
3741
userId: window.localStorage.getItem('ssid'),
3842
username: window.localStorage.getItem('username'),
3943
comments: []
@@ -48,7 +52,7 @@ export const saveProject = (
4852
})
4953
.then((res) => res.json())
5054
.then((data) => {
51-
return {_id: data._id, ...data.project};
55+
return {_id: data._id, ...data.project}; //passing up what is needed for the global appstateslice
5256
})
5357
.catch((err) => console.log(`Error saving project ${err}`));
5458
return project;//returns _id in addition to the project object from the document

app/src/interfaces/Interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { DragObjectWithType } from 'react-dnd';
22

33
export interface State {
44
name: string;
5+
_id: string;
56
forked: boolean;
67
isLoggedIn: boolean;
78
components: Component[];

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import manageSeparators from '../../../helperFunctions/manageSeparators';
1313

1414
export const initialState: State = {
1515
name: '',
16+
_id: '',
1617
forked: false,
1718
isLoggedIn: false,
1819
// config: { saveFlag: true, saveTimer: false },
@@ -770,6 +771,10 @@ const appStateSlice = createSlice({
770771
const projectName = action.payload;
771772
state.name = projectName;
772773
},
774+
updateProjectId: (state, action) => {
775+
const projectId = action.payload; //updates the slice with new _id
776+
state._id = projectId;
777+
},
773778
deleteElement: (state, action) => {
774779
let name: string = '';
775780
const HTMLTypes: HTMLType[] = [...state.HTMLTypes].filter((el) => {
@@ -1292,6 +1297,7 @@ export const {
12921297
changeProjectType,
12931298
resetState,
12941299
updateProjectName,
1300+
updateProjectId,
12951301
deleteElement,
12961302
updateAttributes,
12971303
deleteChild,

0 commit comments

Comments
 (0)