delete files works, server side

This commit is contained in:
catarak 2016-08-24 17:59:15 -04:00
parent 34fe78d734
commit 3dfed2a11e
4 changed files with 33 additions and 5 deletions

View file

@ -155,9 +155,25 @@ export function updateFileName(id, name) {
}
export function deleteFile(id, parentId) {
return {
type: ActionTypes.DELETE_FILE,
id,
parentId
return (dispatch, getState) => {
const state = getState();
const deleteConfig = {
params: {
parentId
}
};
axios.delete(`${ROOT_URL}/projects/${state.project.id}/files/${id}`, deleteConfig, { withCredentials: true })
.then(() => {
dispatch({
type: ActionTypes.DELETE_FILE,
id,
parentId
});
})
.catch(response => dispatch({
type: ActionTypes.ERROR,
error: response.data
})
);
};
}

View file

@ -45,4 +45,15 @@ export function createFile(req, res) {
// return res.json(updatedProject.files[updatedProject.files.length - 1]);
// });
});
}
export function deleteFile(req, res) {
Project.findById(req.params.project_id, (err, project) => {
project.files.id(req.params.file_id).remove();
const childrenArray = project.files.id(req.query.parentId).children;
project.files.id(req.query.parentId).children = childrenArray.filter(id => id !== req.params.file_id);
project.save(innerErr => {
res.json(project.files);
})
});
}

View file

@ -35,7 +35,7 @@ const defaultCSS =
const fileSchema = new Schema({
name: { type: String, default: 'sketch.js' },
content: { type: String },
content: { type: String, default: '' },
url: { type: String },
children: { type: [ String ], default: [] },
fileType: { type: String },

View file

@ -4,5 +4,6 @@ import * as FileController from '../controllers/file.controller';
const router = new Router();
router.route('/projects/:project_id/files').post(FileController.createFile);
router.route('/projects/:project_id/files/:file_id').delete(FileController.deleteFile);
export default router;