Skip to content

Commit 2fdcafd

Browse files
committed
[WIP] Updating rename functionality
1 parent cf2ee4f commit 2fdcafd

File tree

6 files changed

+48
-2
lines changed

6 files changed

+48
-2
lines changed

client/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export const AUTH_ERROR = 'AUTH_ERROR';
2828
export const SETTINGS_UPDATED = 'SETTINGS_UPDATED';
2929

3030
export const SET_PROJECT_NAME = 'SET_PROJECT_NAME';
31+
export const RENAME_PROJECT = 'RENAME_PROJECT';
3132

3233
export const PROJECT_SAVE_SUCCESS = 'PROJECT_SAVE_SUCCESS';
3334
export const PROJECT_SAVE_FAIL = 'PROJECT_SAVE_FAIL';

client/modules/IDE/actions/project.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,31 @@ export function hideEditProjectName() {
277277
type: ActionTypes.HIDE_EDIT_PROJECT_NAME
278278
};
279279
}
280+
281+
export function changeProjectName(id, newName) {
282+
return (dispatch, getState) => {
283+
const state = getState();
284+
axios.put(`${ROOT_URL}/projects/${id}`, { name: newName }, { withCredentials: true })
285+
.then((response) => {
286+
if (response.status === 200) {
287+
dispatch({
288+
type: ActionTypes.RENAME_PROJECT,
289+
payload: { id: response.data.id, name: response.data.name }
290+
});
291+
if (state.project.id === response.data.id) {
292+
dispatch({
293+
type: ActionTypes.SET_PROJECT_NAME,
294+
name: response.data.name
295+
});
296+
}
297+
}
298+
})
299+
.catch((response) => {
300+
console.log(response);
301+
dispatch({
302+
type: ActionTypes.PROJECT_SAVE_FAIL,
303+
error: response.data
304+
});
305+
});
306+
};
307+
}

client/modules/IDE/components/SketchList.jsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ class SketchList extends React.Component {
3030
this.restoreRenameBoxContent = this.restoreRenameBoxContent.bind(this);
3131
}
3232

33+
componentWillReceiveProps(props) {
34+
this.setState({
35+
renameBoxContent: props.sketches.map(({ name }) => name)
36+
});
37+
}
38+
3339
getSketchesTitle() {
3440
if (this.props.username === this.props.user.username) {
3541
return 'p5.js Web Editor | My sketches';
@@ -118,6 +124,7 @@ class SketchList extends React.Component {
118124
onKeyUp={(e) => {
119125
// Enter pressed
120126
if (e.key === 'Enter') {
127+
this.props.changeProjectName(sketch.id, this.state.renameBoxContent[i]);
121128
this.restoreRenameBoxContent();
122129
this.closeAllRenameBoxes();
123130
}
@@ -184,12 +191,12 @@ class SketchList extends React.Component {
184191
className="sketch-list__action-option"
185192
onClick={() => {
186193
this.closeAllRenameBoxes();
194+
this.closeAllDropdowns();
187195
const renameBoxDisplayed = new Array(this.props.sketches.length).fill(false);
188196
renameBoxDisplayed[i] = true;
189197
this.setState({
190198
renameBoxDisplayed
191199
});
192-
this.closeAllDropdowns();
193200
}}
194201
>
195202
Rename
@@ -272,6 +279,7 @@ SketchList.propTypes = {
272279
cloneProject: PropTypes.func.isRequired,
273280
autosaveProject: PropTypes.func.isRequired,
274281
exportProjectAsZip: PropTypes.func.isRequired,
282+
changeProjectName: PropTypes.func.isRequired
275283
};
276284

277285
SketchList.defaultProps = {

client/modules/IDE/pages/IDEView.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ class IDEView extends React.Component {
427427
<Feedback previousPath={this.props.ide.previousPath} />
428428
</Overlay>
429429
}
430+
aa
430431
{ this.props.ide.shareModalVisible &&
431432
<Overlay
432433
title="Share This Sketch"

client/modules/IDE/reducers/projects.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ const sketches = (state = [], action) => {
77
case ActionTypes.DELETE_PROJECT:
88
return state.filter(sketch =>
99
sketch.id !== action.id);
10+
case ActionTypes.RENAME_PROJECT: {
11+
return state.map((sketch) => {
12+
if (sketch.id === action.payload.id) {
13+
return { ...sketch, name: action.payload.name };
14+
}
15+
return { ...sketch };
16+
});
17+
}
1018
default:
1119
return state;
1220
}

server/controllers/project.controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export function updateProject(req, res) {
6262
res.json({ success: false });
6363
return;
6464
}
65-
if (updatedProject.files.length !== req.body.files.length) {
65+
if (req.body.files && updatedProject.files.length !== req.body.files.length) {
6666
const oldFileIds = updatedProject.files.map(file => file.id);
6767
const newFileIds = req.body.files.map(file => file.id);
6868
const staleIds = oldFileIds.filter(id => newFileIds.indexOf(id) === -1);

0 commit comments

Comments
 (0)