Fixes to atlas document deletion
This commit is contained in:
@ -149,22 +149,26 @@ router.post('documents.delete', auth(), async (ctx) => {
|
|||||||
|
|
||||||
if (!document) throw httpErrors.BadRequest();
|
if (!document) throw httpErrors.BadRequest();
|
||||||
|
|
||||||
// TODO: handle sub documents
|
if (atlas.type === 'atlas') {
|
||||||
|
|
||||||
// Don't allow deletion of root docs
|
// Don't allow deletion of root docs
|
||||||
if (atlas.type === 'atlas' && !document.parentDocumentId) {
|
if(!document.parentDocumentId) {
|
||||||
throw httpErrors.BadRequest('Unable to delete atlas\'s root document');
|
throw httpErrors.BadRequest('Unable to delete atlas\'s root document');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delete all chilren
|
||||||
try {
|
try {
|
||||||
await document.destroy();
|
await atlas.deleteDocument(document);
|
||||||
|
await atlas.save();
|
||||||
if (atlas.type === 'atlas') {
|
|
||||||
await atlas.updateNavigationTree();
|
|
||||||
}
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
throw httpErrors.BadRequest('Error while deleting');
|
throw httpErrors.BadRequest('Error while deleting');
|
||||||
};
|
};
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
await document.destroy();
|
||||||
|
} catch (e) {
|
||||||
|
throw httpErrors.BadRequest('Error while deleting');
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
ok: true,
|
ok: true,
|
||||||
|
@ -148,6 +148,30 @@ const Atlas = sequelize.define('atlas', {
|
|||||||
};
|
};
|
||||||
|
|
||||||
this.navigationTree = insertNode(this.navigationTree);
|
this.navigationTree = insertNode(this.navigationTree);
|
||||||
|
},
|
||||||
|
async deleteDocument(document) {
|
||||||
|
const deleteNodeAndDocument = async (node, documentId, shouldDelete = false) => {
|
||||||
|
if (document.id === node.id) {
|
||||||
|
shouldDelete = true;
|
||||||
|
}
|
||||||
|
const newChildren = [];
|
||||||
|
node.children.map(async childNode => {
|
||||||
|
const child = await deleteNodeAndDocument(childNode, documentId, shouldDelete);
|
||||||
|
if (child) {
|
||||||
|
newChildren.push(child);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
node.children = newChildren;
|
||||||
|
|
||||||
|
if (shouldDelete) {
|
||||||
|
const document = await Document.findById(node.id);
|
||||||
|
await document.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
return shouldDelete ? null : node;
|
||||||
|
};
|
||||||
|
|
||||||
|
this.navigationTree = await deleteNodeAndDocument(this.navigationTree, document.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -66,7 +66,14 @@ class DocumentScene extends React.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onDelete = () => {
|
onDelete = () => {
|
||||||
if (confirm("Are you sure you want to delete this document?")) {
|
let msg;
|
||||||
|
if (this.store.document.atlas.type === 'atlas') {
|
||||||
|
msg = 'Are you sure you want to delete this document and all it\'s child documents (if any)?'
|
||||||
|
} else {
|
||||||
|
msg = "Are you sure you want to delete this document?";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (confirm(msg)) {
|
||||||
this.store.deleteDocument();
|
this.store.deleteDocument();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user