fix: return undo state in response

This commit is contained in:
Saumya Pandey committed 2022-02-06 14:30:16 +05:30
1 parent a4e9251eb7
commit da8936e9d8
6 files changed
+42 -8

No files matched your search

@@ -13,6 +13,7 @@ import Fade from "~/components/Fade";
import NudeButton from "~/components/NudeButton";
import useBoolean from "~/hooks/useBoolean";
import useStores from "~/hooks/useStores";
import useToasts from "~/hooks/useToasts";
import DocumentMenu from "~/menus/DocumentMenu";
import { NavigationNode } from "~/types";
import { newDocumentPath } from "~/utils/routeHelpers";
@@ -56,7 +57,7 @@ function DocumentLink(
const document = documents.get(node.id);
const { fetchChildDocuments } = documents;
const [isEditing, setIsEditing] = React.useState(false);
const { showToast } = useToasts();
React.useEffect(() => {
if (isActiveDocument && hasChildDocuments) {
fetchChildDocuments(node.id);
@@ -139,6 +140,7 @@ function DocumentLink(
...node,
depth,
active: isActiveDocument,
parentDocumentId: document?.parentDocumentId,
collectionId: collection?.id || "",
}),
collect: (monitor) => ({
@@ -168,14 +170,28 @@ function DocumentLink(
// Drop to re-parent
const [{ isOverReparent, canDropToReparent }, dropToReparent] = useDrop({
accept: "document",
drop: (item: DragObject, monitor) => {
drop: async (item: DragObject, monitor) => {
if (monitor.didDrop()) {
return;
}
if (!collection) {
return;
}
documents.move(item.id, collection.id, node.id);
const undo = await documents.move(item.id, collection.id, node.id);
showToast(t("Document moved"), {
type: "info",
action: {
text: "undo",
onClick: async () => {
await documents.move(
item.id,
undo.collectionId,
undo.parentDocumentId,
undo.index
);
},
},
});
},
canDrop: (_item, monitor) =>
!isDraft &&
@@ -11,6 +11,7 @@ export type DragObject = NavigationNode & {
depth: number;
active: boolean;
collectionId: string;
parentDocumentId?: string;
};
type Props = Omit<NavLinkProps, "to"> & {
+1
View File
@@ -523,6 +523,7 @@ export default class DocumentsStore extends BaseStore<Document> {
res.data.documents.forEach(this.add);
res.data.collections.forEach(this.rootStore.collections.add);
this.addPolicies(res.policies);
return res.undo;
} finally {
this.movingDocumentId = undefined;
}
+16 -3
View File
@@ -62,6 +62,12 @@ type Result = {
collectionChanged: boolean;
};
type UndoState = {
collectionId: string;
index?: number;
parentDocumentId: string | null;
};
export default async function documentMover({
user,
document,
@@ -70,8 +76,14 @@ export default async function documentMover({
// convert undefined to null so parentId comparison treats them as equal
index,
ip,
}: Props): Promise<Result> {
}: Props): Promise<{ result: Result; undo?: UndoState }> {
let transaction: Transaction | undefined;
const undoState: UndoState = {
collectionId: document.collectionId,
index: undefined,
parentDocumentId: document.parentDocumentId,
};
const collectionChanged = collectionId !== document.collectionId;
const previousCollectionId = document.collectionId;
const result: Result = {
@@ -82,7 +94,7 @@ export default async function documentMover({
if (document.template) {
if (!collectionChanged) {
return result;
return { result };
}
document.collectionId = collectionId;
@@ -107,6 +119,7 @@ export default async function documentMover({
const documentJson = response?.[0];
const fromIndex = response?.[1] || 0;
undoState.index = fromIndex;
// if we're reordering from within the same parent
// the original and destination collection are the same,
@@ -236,5 +249,5 @@ export default async function documentMover({
});
// we need to send all updated models back to the client
return result;
return { result, undo: undoState };
}
+4 -1
View File
@@ -1163,7 +1163,7 @@ router.post("documents.move", auth(), async (ctx) => {
authorize(user, "update", parent);
}
const { documents, collections, collectionChanged } = await documentMover({
const { result, undo } = await documentMover({
user,
document,
collectionId,
@@ -1172,6 +1172,8 @@ router.post("documents.move", auth(), async (ctx) => {
ip: ctx.request.ip,
});
const { documents, collections, collectionChanged } = result;
ctx.body = {
data: {
documents: await Promise.all(
@@ -1182,6 +1184,7 @@ router.post("documents.move", auth(), async (ctx) => {
),
},
policies: collectionChanged ? presentPolicies(user, documents) : [],
undo,
};
});
+1 -1
View File
@@ -132,6 +132,7 @@
"Move document": "Move document",
"Collections could not be loaded, please reload the app": "Collections could not be loaded, please reload the app",
"Collections": "Collections",
"Document moved": "Document moved",
"Untitled": "Untitled",
"New nested document": "New nested document",
"Document not supported – try Markdown, Plain text, HTML, or Word": "Document not supported – try Markdown, Plain text, HTML, or Word",
@@ -399,7 +400,6 @@
"Are you sure about that? Deleting the <em>{{ documentTitle }}</em> document will delete all of its history and any nested documents.": "Are you sure about that? Deleting the <em>{{ documentTitle }}</em> document will delete all of its history and any nested documents.",
"If you’d like the option of referencing or restoring the {{noun}} in the future, consider archiving it instead.": "If you’d like the option of referencing or restoring the {{noun}} in the future, consider archiving it instead.",
"Archiving": "Archiving",
"Document moved": "Document moved",
"Current location": "Current location",
"Choose a new location": "Choose a new location",
"Search collections & documents": "Search collections & documents",