fix updating file to return all file keys

This commit is contained in:
catarak 2016-07-07 13:50:52 -04:00
parent 88531447ba
commit e06c821923
8 changed files with 32 additions and 28 deletions

View file

@ -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 TOGGLE_SKETCH = 'TOGGLE_SKETCH';
export const START_SKETCH = 'START_SKETCH'; export const START_SKETCH = 'START_SKETCH';

View file

@ -1,8 +1,8 @@
import * as ActionTypes from '../../../constants'; import * as ActionTypes from '../../../constants';
export function updateFile(name, content) { export function updateFileContent(name, content) {
return { return {
type: ActionTypes.UPDATE_FILE, type: ActionTypes.UPDATE_FILE_CONTENT,
name, name,
content content
}; };

View file

@ -14,7 +14,7 @@ class Editor extends React.Component {
mode: 'javascript' mode: 'javascript'
}); });
this._cm.on('change', () => { // eslint-disable-line 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`; this._cm.getWrapperElement().style['font-size'] = `${this.props.fontSize}px`;
} }
@ -42,7 +42,7 @@ class Editor extends React.Component {
Editor.propTypes = { Editor.propTypes = {
content: PropTypes.string.isRequired, content: PropTypes.string.isRequired,
updateFile: PropTypes.func.isRequired, updateFileContent: PropTypes.func.isRequired,
fontSize: PropTypes.number.isRequired fontSize: PropTypes.number.isRequired
}; };

View file

@ -5,7 +5,10 @@ function Sidebar(props) {
<section className="sidebar"> <section className="sidebar">
<ul className="sidebar__file-list"> <ul className="sidebar__file-list">
{props.files.map(file => {props.files.map(file =>
<li className="sidebar__file-item">{file.name}</li> <li
className="sidebar__file-item"
key={file.id}
>{file.name}</li>
)} )}
</ul> </ul>
</section> </section>

View file

@ -48,7 +48,7 @@ class IDEView extends React.Component {
<Sidebar files={this.props.files} /> <Sidebar files={this.props.files} />
<Editor <Editor
content={this.props.files[0].content} content={this.props.files[0].content}
updateFile={this.props.updateFile} updateFileContent={this.props.updateFileContent}
fontSize={this.props.preferences.fontSize} fontSize={this.props.preferences.fontSize}
/> />
<PreviewFrame <PreviewFrame
@ -89,7 +89,7 @@ IDEView.propTypes = {
increaseFont: PropTypes.func.isRequired, increaseFont: PropTypes.func.isRequired,
decreaseFont: PropTypes.func.isRequired, decreaseFont: PropTypes.func.isRequired,
files: PropTypes.array.isRequired, files: PropTypes.array.isRequired,
updateFile: PropTypes.func.isRequired updateFileContent: PropTypes.func.isRequired
}; };
function mapStateToProps(state) { function mapStateToProps(state) {

View file

@ -33,16 +33,13 @@ const initialState = [
const files = (state = initialState, action) => { const files = (state = initialState, action) => {
switch (action.type) { switch (action.type) {
case ActionTypes.UPDATE_FILE: case ActionTypes.UPDATE_FILE_CONTENT:
return state.map(file => { return state.map(file => {
if (file.name !== action.name) { if (file.name !== action.name) {
return file; return file;
} }
return { return Object.assign({}, file, { content: action.content });
name: file.name,
content: action.content
};
}); });
case ActionTypes.NEW_PROJECT: case ActionTypes.NEW_PROJECT:
return [...action.files]; return [...action.files];

View file

@ -10,11 +10,7 @@ export function createProject(req, res) {
Project.create(projectValues, (err, newProject) => { Project.create(projectValues, (err, newProject) => {
if (err) { return res.json({ success: false }); } if (err) { return res.json({ success: false }); }
return res.json({ return res.json(newProject);
id: newProject._id, // eslint-disable-line no-underscore-dangle
name: newProject.name,
files: newProject.files
});
}); });
} }
@ -24,11 +20,7 @@ export function updateProject(req, res) {
$set: req.body $set: req.body
}, (err, updatedProject) => { }, (err, updatedProject) => {
if (err) { return res.json({ success: false }); } if (err) { return res.json({ success: false }); }
return res.json({ return res.json(updatedProject);
id: updatedProject._id, // eslint-disable-line no-underscore-dangle
name: updatedProject.name,
file: updatedProject.files
});
}); });
} }
@ -38,11 +30,7 @@ export function getProject(req, res) {
return res.status(404).send({ message: 'Project with that id does not exist' }); return res.status(404).send({ message: 'Project with that id does not exist' });
} }
return res.json({ return res.json(project);
id: project._id, // eslint-disable-line no-underscore-dangle
name: project.name,
files: project.files
});
}); });
} }

View file

@ -28,6 +28,14 @@ const fileSchema = new Schema({
content: { type: String, default: defaultSketch } content: { type: String, default: defaultSketch }
}, { timestamps: true, _id: true }); }, { timestamps: true, _id: true });
fileSchema.virtual('id').get(function(){
return this._id.toHexString();
});
fileSchema.set('toJSON', {
virtuals: true
});
const projectSchema = new Schema({ const projectSchema = new Schema({
name: { type: String, default: "Hello p5.js, it's the server" }, name: { type: String, default: "Hello p5.js, it's the server" },
user: { type: Schema.Types.ObjectId, ref: 'User' }, user: { type: Schema.Types.ObjectId, ref: 'User' },
@ -35,4 +43,12 @@ const projectSchema = new Schema({
_id: { type: String, default: shortid.generate } _id: { type: String, default: shortid.generate }
}, { timestamps: true }); }, { timestamps: true });
projectSchema.virtual('id').get(function(){
return this._id;
});
projectSchema.set('toJSON', {
virtuals: true
});
export default mongoose.model('Project', projectSchema); export default mongoose.model('Project', projectSchema);