From e06c82192300a215ebd259668c6f1ad12863f785 Mon Sep 17 00:00:00 2001 From: catarak Date: Thu, 7 Jul 2016 13:50:52 -0400 Subject: [PATCH] fix updating file to return all file keys --- client/constants.js | 2 +- client/modules/IDE/actions/files.js | 4 ++-- client/modules/IDE/components/Editor.js | 4 ++-- client/modules/IDE/components/Sidebar.js | 5 ++++- client/modules/IDE/pages/IDEView.js | 4 ++-- client/modules/IDE/reducers/files.js | 7 ++----- server/controllers/project.controller.js | 18 +++--------------- server/models/project.js | 16 ++++++++++++++++ 8 files changed, 32 insertions(+), 28 deletions(-) diff --git a/client/constants.js b/client/constants.js index 29d9f701..b69e22b2 100644 --- a/client/constants.js +++ b/client/constants.js @@ -1,4 +1,4 @@ -export const UPDATE_FILE = 'UPDATE_FILE'; +export const UPDATE_FILE_CONTENT = 'UPDATE_FILE_CONTENT'; export const TOGGLE_SKETCH = 'TOGGLE_SKETCH'; export const START_SKETCH = 'START_SKETCH'; diff --git a/client/modules/IDE/actions/files.js b/client/modules/IDE/actions/files.js index 3ceca1e1..f7ebd8f3 100644 --- a/client/modules/IDE/actions/files.js +++ b/client/modules/IDE/actions/files.js @@ -1,8 +1,8 @@ import * as ActionTypes from '../../../constants'; -export function updateFile(name, content) { +export function updateFileContent(name, content) { return { - type: ActionTypes.UPDATE_FILE, + type: ActionTypes.UPDATE_FILE_CONTENT, name, content }; diff --git a/client/modules/IDE/components/Editor.js b/client/modules/IDE/components/Editor.js index e7cb8647..b028d705 100644 --- a/client/modules/IDE/components/Editor.js +++ b/client/modules/IDE/components/Editor.js @@ -14,7 +14,7 @@ class Editor extends React.Component { mode: 'javascript' }); this._cm.on('change', () => { // eslint-disable-line - this.props.updateFile('sketch.js', this._cm.getValue()); + this.props.updateFileContent('sketch.js', this._cm.getValue()); }); this._cm.getWrapperElement().style['font-size'] = `${this.props.fontSize}px`; } @@ -42,7 +42,7 @@ class Editor extends React.Component { Editor.propTypes = { content: PropTypes.string.isRequired, - updateFile: PropTypes.func.isRequired, + updateFileContent: PropTypes.func.isRequired, fontSize: PropTypes.number.isRequired }; diff --git a/client/modules/IDE/components/Sidebar.js b/client/modules/IDE/components/Sidebar.js index bcf11473..3c5d0fe6 100644 --- a/client/modules/IDE/components/Sidebar.js +++ b/client/modules/IDE/components/Sidebar.js @@ -5,7 +5,10 @@ function Sidebar(props) {
diff --git a/client/modules/IDE/pages/IDEView.js b/client/modules/IDE/pages/IDEView.js index 6b74c737..2278a095 100644 --- a/client/modules/IDE/pages/IDEView.js +++ b/client/modules/IDE/pages/IDEView.js @@ -48,7 +48,7 @@ class IDEView extends React.Component { { switch (action.type) { - case ActionTypes.UPDATE_FILE: + case ActionTypes.UPDATE_FILE_CONTENT: return state.map(file => { if (file.name !== action.name) { return file; } - return { - name: file.name, - content: action.content - }; + return Object.assign({}, file, { content: action.content }); }); case ActionTypes.NEW_PROJECT: return [...action.files]; diff --git a/server/controllers/project.controller.js b/server/controllers/project.controller.js index c4a4887a..03baa4e5 100644 --- a/server/controllers/project.controller.js +++ b/server/controllers/project.controller.js @@ -10,11 +10,7 @@ export function createProject(req, res) { Project.create(projectValues, (err, newProject) => { if (err) { return res.json({ success: false }); } - return res.json({ - id: newProject._id, // eslint-disable-line no-underscore-dangle - name: newProject.name, - files: newProject.files - }); + return res.json(newProject); }); } @@ -24,11 +20,7 @@ export function updateProject(req, res) { $set: req.body }, (err, updatedProject) => { if (err) { return res.json({ success: false }); } - return res.json({ - id: updatedProject._id, // eslint-disable-line no-underscore-dangle - name: updatedProject.name, - file: updatedProject.files - }); + return res.json(updatedProject); }); } @@ -38,11 +30,7 @@ export function getProject(req, res) { return res.status(404).send({ message: 'Project with that id does not exist' }); } - return res.json({ - id: project._id, // eslint-disable-line no-underscore-dangle - name: project.name, - files: project.files - }); + return res.json(project); }); } diff --git a/server/models/project.js b/server/models/project.js index 1ad4b4ee..709ea58d 100644 --- a/server/models/project.js +++ b/server/models/project.js @@ -28,6 +28,14 @@ const fileSchema = new Schema({ content: { type: String, default: defaultSketch } }, { timestamps: true, _id: true }); +fileSchema.virtual('id').get(function(){ + return this._id.toHexString(); +}); + +fileSchema.set('toJSON', { + virtuals: true +}); + const projectSchema = new Schema({ name: { type: String, default: "Hello p5.js, it's the server" }, user: { type: Schema.Types.ObjectId, ref: 'User' }, @@ -35,4 +43,12 @@ const projectSchema = new Schema({ _id: { type: String, default: shortid.generate } }, { timestamps: true }); +projectSchema.virtual('id').get(function(){ + return this._id; +}); + +projectSchema.set('toJSON', { + virtuals: true +}); + export default mongoose.model('Project', projectSchema);