create file works server side
This commit is contained in:
parent
5f694329db
commit
34fe78d734
3 changed files with 52 additions and 17 deletions
|
@ -85,7 +85,8 @@ export function createFile(formProps) {
|
||||||
}
|
}
|
||||||
dispatch({
|
dispatch({
|
||||||
type: ActionTypes.CREATE_FILE,
|
type: ActionTypes.CREATE_FILE,
|
||||||
...response.data
|
...response.data,
|
||||||
|
parentId: rootFile.id
|
||||||
});
|
});
|
||||||
dispatch({
|
dispatch({
|
||||||
type: ActionTypes.HIDE_MODAL
|
type: ActionTypes.HIDE_MODAL
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import Project from '../models/project'
|
import Project from '../models/project';
|
||||||
|
|
||||||
// Bug -> timestamps don't get created, but it seems like this will
|
// Bug -> timestamps don't get created, but it seems like this will
|
||||||
// be fixed in mongoose soon
|
// be fixed in mongoose soon
|
||||||
|
@ -13,20 +13,36 @@ export function createFile(req, res) {
|
||||||
{
|
{
|
||||||
new: true
|
new: true
|
||||||
}, (err, updatedProject) => {
|
}, (err, updatedProject) => {
|
||||||
if (err) { return res.json({ success: false }); }
|
if (err) {
|
||||||
|
console.log(err);
|
||||||
|
return res.json({ success: false });
|
||||||
|
}
|
||||||
const newFile = updatedProject.files[updatedProject.files.length - 1];
|
const newFile = updatedProject.files[updatedProject.files.length - 1];
|
||||||
Project.findByIdAndUpdate(
|
updatedProject.files.id(req.body.parentId).children.push(newFile.id);
|
||||||
{"_id": req.params.project_id, "files._id": req.params.parentId},
|
updatedProject.save(innerErr => {
|
||||||
{
|
if (innerErr) {
|
||||||
$push: {
|
console.log(innerErr);
|
||||||
'files.$.children': newFile.id
|
return res.json({ success: false });
|
||||||
}
|
}
|
||||||
},
|
return res.json(updatedProject.files[updatedProject.files.length - 1]);
|
||||||
{
|
});
|
||||||
new: true
|
// console.log(updatedProject.files);
|
||||||
}, (errAgain, updatedProjectAgain) => {
|
// console.log(req.body.parentId);
|
||||||
if (errAgain) { return res.json({ success: false }); }
|
// Project.findByIdAndUpdate(
|
||||||
return res.json(updatedProject.files[updatedProject.files.length - 1]);
|
// {"_id": req.params.project_id, "files._id": ObjectId(req.body.parentId)},
|
||||||
});
|
// {
|
||||||
|
// $push: {
|
||||||
|
// 'files.$.children': newFile._id
|
||||||
|
// }
|
||||||
|
// },
|
||||||
|
// {
|
||||||
|
// new: true
|
||||||
|
// }, (errAgain, updatedProjectAgain) => {
|
||||||
|
// if (errAgain) {
|
||||||
|
// console.log(errAgain);
|
||||||
|
// return res.json({ success: false });
|
||||||
|
// }
|
||||||
|
// return res.json(updatedProject.files[updatedProject.files.length - 1]);
|
||||||
|
// });
|
||||||
});
|
});
|
||||||
}
|
}
|
|
@ -26,7 +26,25 @@ export function updateProject(req, res) {
|
||||||
})
|
})
|
||||||
.populate('user', 'username')
|
.populate('user', 'username')
|
||||||
.exec((err, updatedProject) => {
|
.exec((err, updatedProject) => {
|
||||||
if (err) { return res.json({ success: false }); }
|
if (err) {
|
||||||
|
console.log(err);
|
||||||
|
return res.json({ success: false });
|
||||||
|
}
|
||||||
|
if (updatedProject.files.length !== req.body.files.length) {
|
||||||
|
const oldFileIds = updatedProject.files.map(file => file.id);
|
||||||
|
const newFileIds = req.body.files.map(file => file.id);
|
||||||
|
const staleIds = oldFileIds.filter(id => newFileIds.indexOf(id) === -1);
|
||||||
|
staleIds.forEach(staleId => {
|
||||||
|
updatedProject.files.id(staleId).remove();
|
||||||
|
});
|
||||||
|
updatedProject.save((innerErr) => {
|
||||||
|
if (innerErr) {
|
||||||
|
console.log(innerErr);
|
||||||
|
return res.json({ success: false });
|
||||||
|
}
|
||||||
|
return res.json(updatedProject);
|
||||||
|
});
|
||||||
|
}
|
||||||
return res.json(updatedProject);
|
return res.json(updatedProject);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue