Skip to content

Commit 89e9716

Browse files
committed
bug fixed
1 parent 64bdb22 commit 89e9716

File tree

4 files changed

+42
-28
lines changed

4 files changed

+42
-28
lines changed

client/modules/IDE/actions/project.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -410,26 +410,42 @@ export function deleteProject(id) {
410410
});
411411
};
412412
}
413-
414413
export function changeVisibility(projectId, projectName, visibility) {
415-
return (dispatch) =>
414+
return (dispatch, getState) => {
415+
const state = getState();
416+
416417
apiClient
417418
.patch('/project/visibility', { projectId, visibility })
418419
.then((response) => {
419-
const { visibility: newVisibility } = response.data;
420+
if (response.status === 200) {
421+
const { visibility: newVisibility } = response.data;
420422

421-
dispatch({
422-
type: ActionTypes.CHANGE_VISIBILITY,
423-
payload: { visibility: response.data.visibility }
424-
});
423+
dispatch({
424+
type: ActionTypes.CHANGE_VISIBILITY,
425+
payload: {
426+
id: response.data.id,
427+
visibility: newVisibility
428+
}
429+
});
425430

426-
dispatch(setToastText(`The ${projectName} is now ${newVisibility}!`));
427-
dispatch(showToast(2000));
431+
if (state.project.id === response.data.id) {
432+
dispatch({
433+
type: ActionTypes.SET_PROJECT_NAME,
434+
name: response.data.name
435+
});
436+
}
437+
438+
dispatch(
439+
setToastText(`${projectName} is now ${newVisibility.toLowerCase()}`)
440+
);
441+
dispatch(showToast(2000));
442+
}
428443
})
429444
.catch((error) => {
430445
dispatch({
431446
type: ActionTypes.ERROR,
432447
error: error?.response?.data
433448
});
434449
});
450+
};
435451
}

client/modules/IDE/components/Header/Toolbar.jsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState } from 'react';
1+
import React from 'react';
22
import classNames from 'classnames';
33
import PropTypes from 'prop-types';
44
import { useTranslation } from 'react-i18next';
@@ -15,14 +15,11 @@ import {
1515
setGridOutput,
1616
setTextOutput
1717
} from '../../actions/preferences';
18-
1918
import PlayIcon from '../../../../images/play.svg';
2019
import StopIcon from '../../../../images/stop.svg';
2120
import PreferencesIcon from '../../../../images/preferences.svg';
2221
import ProjectName from './ProjectName';
2322
import { changeVisibility } from '../../actions/project';
24-
// import IconButton from '../../../../common/IconButton';
25-
// import { LockIcon, UnlockIcon } from '../../../../common/icons';
2623

2724
const Toolbar = (props) => {
2825
const { isPlaying, infiniteLoop, preferencesIsVisible } = useSelector(
@@ -34,8 +31,7 @@ const Toolbar = (props) => {
3431
const dispatch = useDispatch();
3532
const { t } = useTranslation();
3633

37-
const [visibility, setVisibility] = useState(project.visibility);
38-
34+
console.log(project.visibility);
3935
const userIsOwner = user?.username === project.owner?.username;
4036
const toggleVisibility = (e) => {
4137
try {
@@ -47,7 +43,6 @@ const Toolbar = (props) => {
4743
isChecked ? 'Private' : 'Public'
4844
)
4945
);
50-
setVisibility(isChecked ? 'Private' : 'Public');
5146
} catch (error) {
5247
console.log(error);
5348
}
@@ -128,7 +123,7 @@ const Toolbar = (props) => {
128123
<input
129124
type="checkbox"
130125
className="toolbar__togglevisibility"
131-
defaultChecked={visibility === 'Private'}
126+
defaultChecked={project.visibility === 'Private'}
132127
onChange={toggleVisibility}
133128
/>
134129
</main>

client/modules/IDE/reducers/projects.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@ const sketches = (state = [], action) => {
66
return action.projects;
77
case ActionTypes.DELETE_PROJECT:
88
return state.filter((sketch) => sketch.id !== action.id);
9-
case ActionTypes.CHANGE_VISIBILITY:
10-
return state.map((sketch) => ({
11-
...sketch,
12-
visibility: action.payload.visibility
13-
}));
9+
case ActionTypes.CHANGE_VISIBILITY: {
10+
return state.map((sketch) => {
11+
if (sketch.id === action.payload.id) {
12+
return { ...sketch, visibility: action.payload.visibility };
13+
}
14+
return { ...sketch };
15+
});
16+
}
1417
case ActionTypes.RENAME_PROJECT: {
1518
return state.map((sketch) => {
1619
if (sketch.id === action.payload.id) {

server/controllers/project.controller.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,14 @@ export async function changeProjectVisibility(req, res) {
291291
visibility: newVisibility
292292
},
293293
{
294-
new: true
294+
new: true,
295+
runValidators: true
295296
}
296-
);
297-
const updatedProjectWithoutFiles = await Project.findById(
298-
updatedProject._id
299-
).select('-files');
297+
)
298+
.populate('user', 'username')
299+
.exec();
300300

301-
return res.status(200).json(updatedProjectWithoutFiles);
301+
return res.status(200).json(updatedProject);
302302
} catch (error) {
303303
return res.status(500).json(error);
304304
}

0 commit comments

Comments
 (0)