From ac6585e713dd879d44396a3ed40a5047c70dc2e4 Mon Sep 17 00:00:00 2001 From: catarak Date: Tue, 23 Aug 2016 19:40:47 -0400 Subject: [PATCH] add file works on unsaved project --- client/modules/IDE/actions/files.js | 4 ++++ client/modules/IDE/reducers/files.js | 12 ++++++++++-- client/styles/components/_sidebar.scss | 9 ++++++--- server/controllers/file.controller.js | 15 ++++++++++++++- server/models/project.js | 3 ++- 5 files changed, 36 insertions(+), 7 deletions(-) diff --git a/client/modules/IDE/actions/files.js b/client/modules/IDE/actions/files.js index 96bb5ce5..bc0749e6 100644 --- a/client/modules/IDE/actions/files.js +++ b/client/modules/IDE/actions/files.js @@ -73,6 +73,8 @@ export function createFile(formProps) { name: createUniqueName(formProps.name, state.files), url: formProps.url, content: formProps.content || '' + // TODO pass parent id to API, once there are folders + parentId: '0' }; axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true }) .then(response => { @@ -107,6 +109,8 @@ export function createFile(formProps) { id: `${maxFileId + 1}`, url: formProps.url, content: formProps.content || '' + // TODO pass parent id from File Tree + parentId: '0' }); dispatch({ type: ActionTypes.HIDE_MODAL diff --git a/client/modules/IDE/reducers/files.js b/client/modules/IDE/reducers/files.js index b12fac71..d3f6040b 100644 --- a/client/modules/IDE/reducers/files.js +++ b/client/modules/IDE/reducers/files.js @@ -78,8 +78,16 @@ const files = (state = initialState, action) => { return [...action.files]; case ActionTypes.RESET_PROJECT: return initialState; - case ActionTypes.CREATE_FILE: - return [...state, { name: action.name, id: action.id, content: action.content, url: action.url }]; + case ActionTypes.CREATE_FILE: // eslint-disable-line + const newState = state.map((file) => { + if (file.id === action.parentId) { + const newFile = Object.assign({}, file); + newFile.children = [...newFile.children, action.id]; + return newFile; + } + return file; + }); + return [...newState, { name: action.name, id: action.id, content: action.content, url: action.url }]; case ActionTypes.SHOW_FILE_OPTIONS: return state.map(file => { if (file.id !== action.id) { diff --git a/client/styles/components/_sidebar.scss b/client/styles/components/_sidebar.scss index 94248be3..b109dd29 100644 --- a/client/styles/components/_sidebar.scss +++ b/client/styles/components/_sidebar.scss @@ -40,9 +40,6 @@ padding: #{8 / $base-font-size}rem #{20 / $base-font-size}rem; color: $light-inactive-text-color; cursor: pointer; - display: flex; - justify-content: space-between; - position: relative; &--selected { background-color: $ide-border-color; } @@ -51,6 +48,12 @@ } } +.file-item__content { + display: flex; + justify-content: space-between; + position: relative; +} + .sidebar__file-item-name { .sidebar__file-item--editing & { display: none; diff --git a/server/controllers/file.controller.js b/server/controllers/file.controller.js index a2071ba5..6e3dd053 100644 --- a/server/controllers/file.controller.js +++ b/server/controllers/file.controller.js @@ -14,6 +14,19 @@ export function createFile(req, res) { new: true }, (err, updatedProject) => { if (err) { return res.json({ success: false }); } - return res.json(updatedProject.files[updatedProject.files.length - 1]); + const newFile = updatedProject.files[updatedProject.files.length - 1]; + Project.findByIdAndUpdate( + {"_id": req.params.project_id, "files._id": req.params.parentId}, + { + $push: { + 'files.$.children': newFile.id + } + }, + { + new: true + }, (errAgain, updatedProjectAgain) => { + if (errAgain) { return res.json({ success: false }); } + return res.json(updatedProject.files[updatedProject.files.length - 1]); + }); }); } \ No newline at end of file diff --git a/server/models/project.js b/server/models/project.js index 520ef75c..7ee038c3 100644 --- a/server/models/project.js +++ b/server/models/project.js @@ -54,7 +54,8 @@ const projectSchema = new Schema({ user: { type: Schema.Types.ObjectId, ref: 'User' }, files: { type: [ fileSchema ], default: [{ name: 'sketch.js', content: defaultSketch, _id: new ObjectId() }, { name: 'index.html', content: defaultHTML, _id: new ObjectId() }, - { name: 'style.css', content: defaultCSS, _id: new ObjectId() }]}, + { name: 'style.css', content: defaultCSS, _id: new ObjectId() }, + { name: 'root', _id: '0'}]}, _id: { type: String, default: shortid.generate }, selectedFile: Schema.Types.ObjectId }, { timestamps: true });