feat: Allow moving templates between collections (#1454)

- Allow template move in document policy
- fix: Ensure that document is not added to collection structure in documentMover command
- fix: Moving a template should now show nested documents as options
- fix: Hitting 'm' should not allow moving a draft
- fix: Styling of seperators on move screen
This commit is contained in:
Tom Moor
2020-08-20 19:46:29 -07:00
committed by GitHub
parent 0555fd2caa
commit 6dd6768f07
5 changed files with 118 additions and 82 deletions

View File

@ -63,7 +63,7 @@ const Title = styled.span`
`;
const StyledGoToIcon = styled(GoToIcon)`
opacity: 0.25;
fill: ${(props) => props.theme.divider};
`;
const ResultWrapper = styled.div`

View File

@ -133,7 +133,7 @@ class DocumentScene extends React.Component<Props> {
ev.preventDefault();
const { document, abilities } = this.props;
if (abilities.update) {
if (abilities.move) {
this.props.history.push(documentMoveUrl(document));
}
}

View File

@ -52,6 +52,7 @@ class DocumentMove extends React.Component<Props> {
@computed
get results(): DocumentPath[] {
const { document, collections } = this.props;
const onlyShowCollections = document.isTemplate;
let results = [];
if (collections.isLoaded) {
@ -62,9 +63,14 @@ class DocumentMove extends React.Component<Props> {
}
}
if (onlyShowCollections) {
results = results.filter((result) => result.type === "collection");
} else {
// Exclude root from search results if document is already at the root
if (!document.parentDocumentId) {
results = results.filter((result) => result.id !== document.collectionId);
results = results.filter(
(result) => result.id !== document.collectionId
);
}
// Exclude document if on the path to result, or the same result
@ -73,6 +79,7 @@ class DocumentMove extends React.Component<Props> {
!result.path.map((doc) => doc.id).includes(document.id) &&
last(result.path.map((doc) => doc.id)) !== document.parentDocumentId
);
}
return results;
}

View File

@ -22,14 +22,28 @@ export default async function documentMover({
const result = { collections: [], documents: [] };
const collectionChanged = collectionId !== document.collectionId;
if (document.template) {
if (!collectionChanged) {
return result;
}
document.collectionId = collectionId;
document.parentDocumentId = null;
await document.save();
result.documents.push(document);
} else {
try {
transaction = await sequelize.transaction();
// remove from original collection
const collection = await document.getCollection({ transaction });
const documentJson = await collection.removeDocumentInStructure(document, {
const documentJson = await collection.removeDocumentInStructure(
document,
{
save: false,
});
}
);
// if the collection is the same then it will get saved below, this
// line prevents a pointless intermediate save from occurring.
@ -75,6 +89,13 @@ export default async function documentMover({
result.documents.push(document);
await transaction.commit();
} catch (err) {
if (transaction) {
await transaction.rollback();
}
throw err;
}
}
await Event.create({
name: "documents.move",
@ -89,12 +110,6 @@ export default async function documentMover({
},
ip,
});
} catch (err) {
if (transaction) {
await transaction.rollback();
}
throw err;
}
// we need to send all updated models back to the client
return result;

View File

@ -71,7 +71,21 @@ allow(User, "createChildDocument", Document, (user, document) => {
return user.teamId === document.teamId;
});
allow(User, ["move", "pin", "unpin"], Document, (user, document) => {
allow(User, "move", Document, (user, document) => {
if (document.archivedAt) return false;
if (document.deletedAt) return false;
if (!document.publishedAt) return false;
invariant(
document.collection,
"collection is missing, did you forget to include in the query scope?"
);
if (cannot(user, "update", document.collection)) return false;
return user.teamId === document.teamId;
});
allow(User, ["pin", "unpin"], Document, (user, document) => {
if (document.archivedAt) return false;
if (document.deletedAt) return false;
if (document.template) return false;
@ -112,16 +126,16 @@ allow(User, "restore", Document, (user, document) => {
});
allow(User, "archive", Document, (user, document) => {
if (!document.publishedAt) return false;
if (document.archivedAt) return false;
if (document.deletedAt) return false;
invariant(
document.collection,
"collection is missing, did you forget to include in the query scope?"
);
if (cannot(user, "update", document.collection)) return false;
if (!document.publishedAt) return false;
if (document.archivedAt) return false;
if (document.deletedAt) return false;
return user.teamId === document.teamId;
});