fix updating file to return all file keys
This commit is contained in:
parent
88531447ba
commit
e06c821923
8 changed files with 32 additions and 28 deletions
|
@ -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';
|
||||
|
|
|
@ -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
|
||||
};
|
||||
|
|
|
@ -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
|
||||
};
|
||||
|
||||
|
|
|
@ -5,7 +5,10 @@ function Sidebar(props) {
|
|||
<section className="sidebar">
|
||||
<ul className="sidebar__file-list">
|
||||
{props.files.map(file =>
|
||||
<li className="sidebar__file-item">{file.name}</li>
|
||||
<li
|
||||
className="sidebar__file-item"
|
||||
key={file.id}
|
||||
>{file.name}</li>
|
||||
)}
|
||||
</ul>
|
||||
</section>
|
||||
|
|
|
@ -48,7 +48,7 @@ class IDEView extends React.Component {
|
|||
<Sidebar files={this.props.files} />
|
||||
<Editor
|
||||
content={this.props.files[0].content}
|
||||
updateFile={this.props.updateFile}
|
||||
updateFileContent={this.props.updateFileContent}
|
||||
fontSize={this.props.preferences.fontSize}
|
||||
/>
|
||||
<PreviewFrame
|
||||
|
@ -89,7 +89,7 @@ IDEView.propTypes = {
|
|||
increaseFont: PropTypes.func.isRequired,
|
||||
decreaseFont: PropTypes.func.isRequired,
|
||||
files: PropTypes.array.isRequired,
|
||||
updateFile: PropTypes.func.isRequired
|
||||
updateFileContent: PropTypes.func.isRequired
|
||||
};
|
||||
|
||||
function mapStateToProps(state) {
|
||||
|
|
|
@ -33,16 +33,13 @@ const initialState = [
|
|||
|
||||
const files = (state = initialState, action) => {
|
||||
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];
|
||||
|
|
|
@ -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);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue